001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.bind.v2.model.impl;
038:
039: import javax.xml.bind.annotation.XmlRootElement;
040: import javax.xml.bind.annotation.XmlSchema;
041: import javax.xml.bind.annotation.XmlType;
042: import javax.xml.namespace.QName;
043:
044: import com.sun.xml.bind.api.impl.NameConverter;
045: import com.sun.xml.bind.v2.model.annotation.AnnotationReader;
046: import com.sun.xml.bind.v2.model.annotation.Locatable;
047: import com.sun.xml.bind.v2.model.core.TypeInfo;
048: import com.sun.xml.bind.v2.model.core.TypeInfoSet;
049: import com.sun.xml.bind.v2.model.nav.Navigator;
050:
051: /**
052: * Common implementation between {@link ClassInfoImpl} and {@link ElementInfoImpl}.
053: *
054: * @author Kohsuke Kawaguchi
055: */
056: abstract class TypeInfoImpl<TypeT, ClassDeclT, FieldT, MethodT>
057: implements TypeInfo<TypeT, ClassDeclT>, Locatable {
058:
059: /**
060: * The Java class that caused this Java class to be a part of the JAXB processing.
061: *
062: * null if it's specified explicitly by the user.
063: */
064: private final Locatable upstream;
065:
066: /**
067: * {@link TypeInfoSet} to which this class belongs.
068: */
069: protected final TypeInfoSetImpl<TypeT, ClassDeclT, FieldT, MethodT> owner;
070:
071: /**
072: * Reference to the {@link ModelBuilder}, only until we link {@link TypeInfo}s all together,
073: * because we don't want to keep {@link ModelBuilder} too long.
074: */
075: protected ModelBuilder<TypeT, ClassDeclT, FieldT, MethodT> builder;
076:
077: protected TypeInfoImpl(
078: ModelBuilder<TypeT, ClassDeclT, FieldT, MethodT> builder,
079: Locatable upstream) {
080:
081: this .builder = builder;
082: this .owner = builder.typeInfoSet;
083: this .upstream = upstream;
084: }
085:
086: public Locatable getUpstream() {
087: return upstream;
088: }
089:
090: /*package*/void link() {
091: builder = null;
092: }
093:
094: protected final Navigator<TypeT, ClassDeclT, FieldT, MethodT> nav() {
095: return owner.nav;
096: }
097:
098: protected final AnnotationReader<TypeT, ClassDeclT, FieldT, MethodT> reader() {
099: return owner.reader;
100: }
101:
102: /**
103: * Parses an {@link XmlRootElement} annotation on a class
104: * and determine the element name.
105: *
106: * @return null
107: * if none was found.
108: */
109: protected final QName parseElementName(ClassDeclT clazz) {
110: XmlRootElement e = reader().getClassAnnotation(
111: XmlRootElement.class, clazz, this );
112: if (e == null)
113: return null;
114:
115: String local = e.name();
116: if (local.equals("##default")) {
117: // if defaulted...
118: local = NameConverter.standard.toVariableName(nav()
119: .getClassShortName(clazz));
120: }
121: String nsUri = e.namespace();
122: if (nsUri.equals("##default")) {
123: // if defaulted ...
124: XmlSchema xs = reader().getPackageAnnotation(
125: XmlSchema.class, clazz, this );
126: if (xs != null)
127: nsUri = xs.namespace();
128: else {
129: nsUri = builder.defaultNsUri;
130: }
131: }
132:
133: return new QName(nsUri.intern(), local.intern());
134: }
135:
136: protected final QName parseTypeName(ClassDeclT clazz) {
137: return parseTypeName(clazz, reader().getClassAnnotation(
138: XmlType.class, clazz, this ));
139: }
140:
141: /**
142: * Parses a (potentially-null) {@link XmlType} annotation on a class
143: * and determine the actual value.
144: *
145: * @param clazz
146: * The class on which the XmlType annotation is checked.
147: * @param t
148: * The {@link XmlType} annotation on the clazz. This value
149: * is taken as a parameter to improve the performance for the case where
150: * 't' is pre-computed.
151: */
152: protected final QName parseTypeName(ClassDeclT clazz, XmlType t) {
153: String nsUri = "##default";
154: String local = "##default";
155: if (t != null) {
156: nsUri = t.namespace();
157: local = t.name();
158: }
159:
160: if (local.length() == 0)
161: return null; // anonymous
162:
163: if (local.equals("##default"))
164: // if defaulted ...
165: local = NameConverter.standard.toVariableName(nav()
166: .getClassShortName(clazz));
167:
168: if (nsUri.equals("##default")) {
169: // if defaulted ...
170: XmlSchema xs = reader().getPackageAnnotation(
171: XmlSchema.class, clazz, this );
172: if (xs != null)
173: nsUri = xs.namespace();
174: else {
175: nsUri = builder.defaultNsUri;
176: }
177: }
178:
179: return new QName(nsUri.intern(), local.intern());
180: }
181: }
|