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 java.util.Collections;
040: import java.util.LinkedHashSet;
041: import java.util.Set;
042:
043: import javax.xml.bind.JAXBElement;
044: import javax.xml.bind.annotation.XmlAnyElement;
045: import javax.xml.bind.annotation.XmlElementRef;
046: import javax.xml.bind.annotation.XmlElementRefs;
047: import javax.xml.bind.annotation.XmlMixed;
048: import javax.xml.bind.annotation.XmlSchema;
049: import javax.xml.bind.annotation.XmlNsForm;
050: import javax.xml.namespace.QName;
051:
052: import com.sun.xml.bind.v2.model.annotation.AnnotationReader;
053: import com.sun.xml.bind.v2.model.core.ClassInfo;
054: import com.sun.xml.bind.v2.model.core.Element;
055: import com.sun.xml.bind.v2.model.core.ElementInfo;
056: import com.sun.xml.bind.v2.model.core.NonElement;
057: import com.sun.xml.bind.v2.model.core.PropertyKind;
058: import com.sun.xml.bind.v2.model.core.ReferencePropertyInfo;
059: import com.sun.xml.bind.v2.model.core.WildcardMode;
060: import com.sun.xml.bind.v2.model.nav.Navigator;
061: import com.sun.xml.bind.v2.runtime.IllegalAnnotationException;
062:
063: /**
064: * Implementation of {@link ReferencePropertyInfo}.
065: *
066: * @author Kohsuke Kawaguchi
067: */
068: class ReferencePropertyInfoImpl<T, C, F, M> extends
069: ERPropertyInfoImpl<T, C, F, M> implements
070: ReferencePropertyInfo<T, C> {
071: /**
072: * Lazily computed.
073: * @see #getElements()
074: */
075: private Set<Element<T, C>> types;
076:
077: private final boolean isMixed;
078:
079: private final WildcardMode wildcard;
080: private final C domHandler;
081:
082: public ReferencePropertyInfoImpl(
083: ClassInfoImpl<T, C, F, M> classInfo,
084: PropertySeed<T, C, F, M> seed) {
085:
086: super (classInfo, seed);
087:
088: isMixed = seed.readAnnotation(XmlMixed.class) != null;
089:
090: XmlAnyElement xae = seed.readAnnotation(XmlAnyElement.class);
091: if (xae == null) {
092: wildcard = null;
093: domHandler = null;
094: } else {
095: wildcard = xae.lax() ? WildcardMode.LAX : WildcardMode.SKIP;
096: domHandler = nav().asDecl(
097: reader().getClassValue(xae, "value"));
098: }
099: }
100:
101: public Set<? extends Element<T, C>> ref() {
102: return getElements();
103: }
104:
105: public PropertyKind kind() {
106: return PropertyKind.REFERENCE;
107: }
108:
109: public Set<? extends Element<T, C>> getElements() {
110: if (types == null)
111: calcTypes(false);
112: assert types != null;
113: return types;
114: }
115:
116: /**
117: * Compute {@link #types}.
118: *
119: * @param last
120: * if true, every {@link XmlElementRef} must yield at least one type.
121: */
122: private void calcTypes(boolean last) {
123: XmlElementRef[] ann;
124: types = new LinkedHashSet<Element<T, C>>();
125: XmlElementRefs refs = seed.readAnnotation(XmlElementRefs.class);
126: XmlElementRef ref = seed.readAnnotation(XmlElementRef.class);
127:
128: if (refs != null && ref != null) {
129: parent.builder.reportError(new IllegalAnnotationException(
130: Messages.MUTUALLY_EXCLUSIVE_ANNOTATIONS.format(
131: nav().getClassName(parent.getClazz()) + '#'
132: + seed.getName(), ref
133: .annotationType().getName(), refs
134: .annotationType().getName()), ref,
135: refs));
136: }
137:
138: if (refs != null)
139: ann = refs.value();
140: else {
141: if (ref != null)
142: ann = new XmlElementRef[] { ref };
143: else
144: ann = null;
145: }
146:
147: if (ann != null) {
148: Navigator<T, C, F, M> nav = nav();
149: AnnotationReader<T, C, F, M> reader = reader();
150:
151: final T defaultType = nav.ref(XmlElementRef.DEFAULT.class);
152: final C je = nav.asDecl(JAXBElement.class);
153:
154: for (XmlElementRef r : ann) {
155: boolean yield;
156: T type = reader.getClassValue(r, "type");
157: if (type.equals(defaultType))
158: type = nav.erasure(getIndividualType());
159: if (nav.getBaseClass(type, je) != null)
160: yield = addGenericElement(r);
161: else
162: yield = addAllSubtypes(type);
163:
164: if (last && !yield) {
165: // a reference didn't produce any type.
166: // diagnose the problem
167: if (type.equals(nav.ref(JAXBElement.class))) {
168: // no XmlElementDecl
169: parent.builder
170: .reportError(new IllegalAnnotationException(
171: Messages.NO_XML_ELEMENT_DECL
172: .format(
173: getEffectiveNamespaceFor(r),
174: r.name()), this ));
175: } else {
176: parent.builder
177: .reportError(new IllegalAnnotationException(
178: Messages.INVALID_XML_ELEMENT_REF
179: .format(), this ));
180: }
181:
182: // reporting one error would do.
183: // often the element ref field is using @XmlElementRefs
184: // to point to multiple JAXBElements.
185: // reporting one error for each @XmlElemetnRef is thus often redundant.
186: return;
187: }
188: }
189: }
190:
191: types = Collections.unmodifiableSet(types);
192: }
193:
194: /**
195: * @return
196: * true if the reference yields at least one type
197: */
198: private boolean addGenericElement(XmlElementRef r) {
199: String nsUri = getEffectiveNamespaceFor(r);
200: // TODO: check spec. defaulting of localName.
201: return addGenericElement(parent.owner.getElementInfo(parent
202: .getClazz(), new QName(nsUri, r.name())));
203: }
204:
205: private String getEffectiveNamespaceFor(XmlElementRef r) {
206: String nsUri = r.namespace();
207:
208: XmlSchema xs = reader().getPackageAnnotation(XmlSchema.class,
209: parent.getClazz(), this );
210: if (xs != null
211: && xs.attributeFormDefault() == XmlNsForm.QUALIFIED) {
212: // JAX-RPC doesn't want the default namespace URI swapping to take effect to
213: // local "unqualified" elements. UGLY.
214: if (nsUri.length() == 0)
215: nsUri = parent.builder.defaultNsUri;
216: }
217:
218: return nsUri;
219: }
220:
221: private boolean addGenericElement(ElementInfo<T, C> ei) {
222: if (ei == null)
223: return false;
224: types.add(ei);
225: for (ElementInfo<T, C> subst : ei.getSubstitutionMembers())
226: addGenericElement(subst);
227: return true;
228: }
229:
230: private boolean addAllSubtypes(T type) {
231: Navigator<T, C, F, M> nav = nav();
232:
233: // this allows the explicitly referenced type to be sucked in to the model
234: NonElement<T, C> t = parent.builder.getClassInfo(nav
235: .asDecl(type), this );
236: if (!(t instanceof ClassInfo))
237: // this is leaf.
238: return false;
239:
240: boolean result = false;
241:
242: ClassInfo<T, C> c = (ClassInfo<T, C>) t;
243: if (c.isElement()) {
244: types.add(c.asElement());
245: result = true;
246: }
247:
248: // look for other possible types
249: for (ClassInfo<T, C> ci : parent.owner.beans().values()) {
250: if (ci.isElement() && nav.isSubClassOf(ci.getType(), type)) {
251: types.add(ci.asElement());
252: result = true;
253: }
254: }
255:
256: // don't allow local elements to substitute.
257: for (ElementInfo<T, C> ei : parent.owner.getElementMappings(
258: null).values()) {
259: if (nav.isSubClassOf(ei.getType(), type)) {
260: types.add(ei);
261: result = true;
262: }
263: }
264:
265: return result;
266: }
267:
268: protected void link() {
269: super .link();
270:
271: // until we get the whole thing into TypeInfoSet,
272: // we never really know what are all the possible types that can be assigned on this field.
273: // so recompute this value when we have all the information.
274: calcTypes(true);
275:
276: }
277:
278: public final boolean isMixed() {
279: return isMixed;
280: }
281:
282: public final WildcardMode getWildcard() {
283: return wildcard;
284: }
285:
286: public final C getDOMHandler() {
287: return domHandler;
288: }
289: }
|