1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/v2/model/impl/ReferencePropertyInfoImpl.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,394 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.bind.v2.model.impl; 1.30 + 1.31 +import java.util.Collections; 1.32 +import java.util.LinkedHashSet; 1.33 +import java.util.Set; 1.34 + 1.35 +import javax.xml.bind.JAXBElement; 1.36 +import javax.xml.bind.annotation.XmlAnyElement; 1.37 +import javax.xml.bind.annotation.XmlElementRef; 1.38 +import javax.xml.bind.annotation.XmlElementRefs; 1.39 +import javax.xml.bind.annotation.XmlMixed; 1.40 +import javax.xml.bind.annotation.XmlSchema; 1.41 +import javax.xml.bind.annotation.XmlNsForm; 1.42 +import javax.xml.namespace.QName; 1.43 + 1.44 +import com.sun.xml.internal.bind.v2.model.annotation.AnnotationReader; 1.45 +import com.sun.xml.internal.bind.v2.model.core.ClassInfo; 1.46 +import com.sun.xml.internal.bind.v2.model.core.Element; 1.47 +import com.sun.xml.internal.bind.v2.model.core.ElementInfo; 1.48 +import com.sun.xml.internal.bind.v2.model.core.NonElement; 1.49 +import com.sun.xml.internal.bind.v2.model.core.PropertyKind; 1.50 +import com.sun.xml.internal.bind.v2.model.core.ReferencePropertyInfo; 1.51 +import com.sun.xml.internal.bind.v2.model.core.WildcardMode; 1.52 +import com.sun.xml.internal.bind.v2.model.nav.Navigator; 1.53 +import com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationException; 1.54 +import java.util.Iterator; 1.55 + 1.56 +/** 1.57 + * Implementation of {@link ReferencePropertyInfo}. 1.58 + * 1.59 + * @author Kohsuke Kawaguchi 1.60 + */ 1.61 +class ReferencePropertyInfoImpl<T,C,F,M> 1.62 + extends ERPropertyInfoImpl<T,C,F,M> 1.63 + implements ReferencePropertyInfo<T,C>, DummyPropertyInfo<T, C, F, M> 1.64 +{ 1.65 + /** 1.66 + * Lazily computed. 1.67 + * @see #getElements() 1.68 + */ 1.69 + private Set<Element<T,C>> types; 1.70 + private Set<PropertyInfoImpl<T,C,F,M>> subTypes = new LinkedHashSet<PropertyInfoImpl<T,C,F,M>>(); 1.71 + 1.72 + private final boolean isMixed; 1.73 + 1.74 + private final WildcardMode wildcard; 1.75 + private final C domHandler; 1.76 + /** 1.77 + * Lazily computed. 1.78 + * @see #isRequired() 1.79 + */ 1.80 + private Boolean isRequired; 1.81 + 1.82 + public ReferencePropertyInfoImpl( 1.83 + ClassInfoImpl<T,C,F,M> classInfo, 1.84 + PropertySeed<T,C,F,M> seed) { 1.85 + 1.86 + super(classInfo, seed); 1.87 + 1.88 + isMixed = seed.readAnnotation(XmlMixed.class) != null; 1.89 + 1.90 + XmlAnyElement xae = seed.readAnnotation(XmlAnyElement.class); 1.91 + if(xae==null) { 1.92 + wildcard = null; 1.93 + domHandler = null; 1.94 + } else { 1.95 + wildcard = xae.lax()?WildcardMode.LAX:WildcardMode.SKIP; 1.96 + domHandler = nav().asDecl(reader().getClassValue(xae,"value")); 1.97 + } 1.98 + } 1.99 + 1.100 + public Set<? extends Element<T,C>> ref() { 1.101 + return getElements(); 1.102 + } 1.103 + 1.104 + public PropertyKind kind() { 1.105 + return PropertyKind.REFERENCE; 1.106 + } 1.107 + 1.108 + public Set<? extends Element<T,C>> getElements() { 1.109 + if(types==null) 1.110 + calcTypes(false); 1.111 + assert types!=null; 1.112 + return types; 1.113 + } 1.114 + 1.115 + /** 1.116 + * Compute {@link #types}. 1.117 + * 1.118 + * @param last 1.119 + * if true, every {@link XmlElementRef} must yield at least one type. 1.120 + */ 1.121 + private void calcTypes(boolean last) { 1.122 + XmlElementRef[] ann; 1.123 + types = new LinkedHashSet<Element<T,C>>(); 1.124 + XmlElementRefs refs = seed.readAnnotation(XmlElementRefs.class); 1.125 + XmlElementRef ref = seed.readAnnotation(XmlElementRef.class); 1.126 + 1.127 + if(refs!=null && ref!=null) { 1.128 + parent.builder.reportError(new IllegalAnnotationException( 1.129 + Messages.MUTUALLY_EXCLUSIVE_ANNOTATIONS.format( 1.130 + nav().getClassName(parent.getClazz())+'#'+seed.getName(), 1.131 + ref.annotationType().getName(), refs.annotationType().getName()), 1.132 + ref, refs )); 1.133 + } 1.134 + 1.135 + if(refs!=null) 1.136 + ann = refs.value(); 1.137 + else { 1.138 + if(ref!=null) 1.139 + ann = new XmlElementRef[]{ref}; 1.140 + else 1.141 + ann = null; 1.142 + } 1.143 + 1.144 + isRequired = !isCollection(); // this is by default, to remain compatible with 2.1 1.145 + 1.146 + if(ann!=null) { 1.147 + Navigator<T,C,F,M> nav = nav(); 1.148 + AnnotationReader<T,C,F,M> reader = reader(); 1.149 + 1.150 + final T defaultType = nav.ref(XmlElementRef.DEFAULT.class); 1.151 + final C je = nav.asDecl(JAXBElement.class); 1.152 + 1.153 + for( XmlElementRef r : ann ) { 1.154 + boolean yield; 1.155 + T type = reader.getClassValue(r,"type"); 1.156 + if(nav().isSameType(type, defaultType)) 1.157 + type = nav.erasure(getIndividualType()); 1.158 + if(nav.getBaseClass(type,je)!=null) 1.159 + yield = addGenericElement(r); 1.160 + else 1.161 + yield = addAllSubtypes(type); 1.162 + 1.163 + // essentially "isRequired &= isRequired(r)" except that we'd like to skip evaluating isRequird(r) 1.164 + // if the value is already false. 1.165 + if(isRequired && !isRequired(r)) 1.166 + isRequired = false; 1.167 + 1.168 + if(last && !yield) { 1.169 + // a reference didn't produce any type. 1.170 + // diagnose the problem 1.171 + if(nav().isSameType(type, nav.ref(JAXBElement.class))) { 1.172 + // no XmlElementDecl 1.173 + parent.builder.reportError(new IllegalAnnotationException( 1.174 + Messages.NO_XML_ELEMENT_DECL.format( 1.175 + getEffectiveNamespaceFor(r), r.name()), 1.176 + this 1.177 + )); 1.178 + } else { 1.179 + parent.builder.reportError(new IllegalAnnotationException( 1.180 + Messages.INVALID_XML_ELEMENT_REF.format(type),this)); 1.181 + } 1.182 + 1.183 + // reporting one error would do. 1.184 + // often the element ref field is using @XmlElementRefs 1.185 + // to point to multiple JAXBElements. 1.186 + // reporting one error for each @XmlElemetnRef is thus often redundant. 1.187 + return; 1.188 + } 1.189 + } 1.190 + } 1.191 + 1.192 + Iterator<PropertyInfoImpl<T,C,F,M>> i = subTypes.iterator(); 1.193 + while (i.hasNext()) { 1.194 + 1.195 + ReferencePropertyInfoImpl<T,C,F,M> info = (ReferencePropertyInfoImpl<T, C, F, M>) i.next(); 1.196 + PropertySeed sd = info.seed; 1.197 + refs = sd.readAnnotation(XmlElementRefs.class); 1.198 + ref = sd.readAnnotation(XmlElementRef.class); 1.199 + 1.200 + if (refs != null && ref != null) { 1.201 + parent.builder.reportError(new IllegalAnnotationException( 1.202 + Messages.MUTUALLY_EXCLUSIVE_ANNOTATIONS.format( 1.203 + nav().getClassName(parent.getClazz())+'#'+seed.getName(), 1.204 + ref.annotationType().getName(), refs.annotationType().getName()), 1.205 + ref, refs )); 1.206 + } 1.207 + 1.208 + if (refs != null) { 1.209 + ann = refs.value(); 1.210 + } else { 1.211 + if (ref != null) { 1.212 + ann = new XmlElementRef[]{ref}; 1.213 + } else { 1.214 + ann = null; 1.215 + } 1.216 + } 1.217 + 1.218 + if (ann != null) { 1.219 + Navigator<T,C,F,M> nav = nav(); 1.220 + AnnotationReader<T,C,F,M> reader = reader(); 1.221 + 1.222 + final T defaultType = nav.ref(XmlElementRef.DEFAULT.class); 1.223 + final C je = nav.asDecl(JAXBElement.class); 1.224 + 1.225 + for( XmlElementRef r : ann ) { 1.226 + boolean yield; 1.227 + T type = reader.getClassValue(r,"type"); 1.228 + if (nav().isSameType(type, defaultType)) { 1.229 + type = nav.erasure(getIndividualType()); 1.230 + } 1.231 + if (nav.getBaseClass(type,je) != null) { 1.232 + yield = addGenericElement(r, info); 1.233 + 1.234 + } else { 1.235 + yield = addAllSubtypes(type); 1.236 + } 1.237 + 1.238 + if(last && !yield) { 1.239 + // a reference didn't produce any type. 1.240 + // diagnose the problem 1.241 + if(nav().isSameType(type, nav.ref(JAXBElement.class))) { 1.242 + // no XmlElementDecl 1.243 + parent.builder.reportError(new IllegalAnnotationException( 1.244 + Messages.NO_XML_ELEMENT_DECL.format( 1.245 + getEffectiveNamespaceFor(r), r.name()), 1.246 + this 1.247 + )); 1.248 + } else { 1.249 + parent.builder.reportError(new IllegalAnnotationException( 1.250 + Messages.INVALID_XML_ELEMENT_REF.format(),this)); 1.251 + } 1.252 + 1.253 + // reporting one error would do. 1.254 + // often the element ref field is using @XmlElementRefs 1.255 + // to point to multiple JAXBElements. 1.256 + // reporting one error for each @XmlElemetnRef is thus often redundant. 1.257 + return; 1.258 + } 1.259 + } 1.260 + } 1.261 + } 1.262 + 1.263 + types = Collections.unmodifiableSet(types); 1.264 + } 1.265 + 1.266 + public boolean isRequired() { 1.267 + if(isRequired==null) 1.268 + calcTypes(false); 1.269 + return isRequired; 1.270 + } 1.271 + 1.272 + /** 1.273 + * If we find out that we are working with 2.1 API, remember the fact so that 1.274 + * we don't waste time generating exceptions every time we call {@link #isRequired(XmlElementRef)}. 1.275 + */ 1.276 + private static boolean is2_2 = true; 1.277 + 1.278 + /** 1.279 + * Reads the value of {@code XmlElementRef.required()}. 1.280 + * 1.281 + * If we are working as 2.1 RI, this defaults to true. 1.282 + */ 1.283 + private boolean isRequired(XmlElementRef ref) { 1.284 + if(!is2_2) return true; 1.285 + 1.286 + try { 1.287 + return ref.required(); 1.288 + } catch(LinkageError e) { 1.289 + is2_2 = false; 1.290 + return true; // the value defaults to true 1.291 + } 1.292 + } 1.293 + 1.294 + /** 1.295 + * @return 1.296 + * true if the reference yields at least one type 1.297 + */ 1.298 + private boolean addGenericElement(XmlElementRef r) { 1.299 + String nsUri = getEffectiveNamespaceFor(r); 1.300 + // TODO: check spec. defaulting of localName. 1.301 + return addGenericElement(parent.owner.getElementInfo(parent.getClazz(),new QName(nsUri,r.name()))); 1.302 + } 1.303 + 1.304 + private boolean addGenericElement(XmlElementRef r, ReferencePropertyInfoImpl<T,C,F,M> info) { 1.305 + String nsUri = info.getEffectiveNamespaceFor(r); 1.306 + ElementInfo ei = parent.owner.getElementInfo(info.parent.getClazz(), new QName(nsUri, r.name())); 1.307 + types.add(ei); 1.308 + return true; 1.309 + } 1.310 + 1.311 + private String getEffectiveNamespaceFor(XmlElementRef r) { 1.312 + String nsUri = r.namespace(); 1.313 + 1.314 + XmlSchema xs = reader().getPackageAnnotation( XmlSchema.class, parent.getClazz(), this ); 1.315 + if(xs!=null && xs.attributeFormDefault()== XmlNsForm.QUALIFIED) { 1.316 + // JAX-RPC doesn't want the default namespace URI swapping to take effect to 1.317 + // local "unqualified" elements. UGLY. 1.318 + if(nsUri.length()==0) 1.319 + nsUri = parent.builder.defaultNsUri; 1.320 + } 1.321 + 1.322 + return nsUri; 1.323 + } 1.324 + 1.325 + private boolean addGenericElement(ElementInfo<T,C> ei) { 1.326 + if(ei==null) 1.327 + return false; 1.328 + types.add(ei); 1.329 + for( ElementInfo<T,C> subst : ei.getSubstitutionMembers() ) 1.330 + addGenericElement(subst); 1.331 + return true; 1.332 + } 1.333 + 1.334 + private boolean addAllSubtypes(T type) { 1.335 + Navigator<T,C,F,M> nav = nav(); 1.336 + 1.337 + // this allows the explicitly referenced type to be sucked in to the model 1.338 + NonElement<T,C> t = parent.builder.getClassInfo(nav.asDecl(type),this); 1.339 + if(!(t instanceof ClassInfo)) 1.340 + // this is leaf. 1.341 + return false; 1.342 + 1.343 + boolean result = false; 1.344 + 1.345 + ClassInfo<T,C> c = (ClassInfo<T,C>) t; 1.346 + if(c.isElement()) { 1.347 + types.add(c.asElement()); 1.348 + result = true; 1.349 + } 1.350 + 1.351 + // look for other possible types 1.352 + for( ClassInfo<T,C> ci : parent.owner.beans().values() ) { 1.353 + if(ci.isElement() && nav.isSubClassOf(ci.getType(),type)) { 1.354 + types.add(ci.asElement()); 1.355 + result = true; 1.356 + } 1.357 + } 1.358 + 1.359 + // don't allow local elements to substitute. 1.360 + for( ElementInfo<T,C> ei : parent.owner.getElementMappings(null).values()) { 1.361 + if(nav.isSubClassOf(ei.getType(),type)) { 1.362 + types.add(ei); 1.363 + result = true; 1.364 + } 1.365 + } 1.366 + 1.367 + return result; 1.368 + } 1.369 + 1.370 + 1.371 + @Override 1.372 + protected void link() { 1.373 + super.link(); 1.374 + 1.375 + // until we get the whole thing into TypeInfoSet, 1.376 + // we never really know what are all the possible types that can be assigned on this field. 1.377 + // so recompute this value when we have all the information. 1.378 + calcTypes(true); 1.379 + 1.380 + } 1.381 + 1.382 + public final void addType(PropertyInfoImpl<T,C,F,M> info) { 1.383 + subTypes.add(info); 1.384 + } 1.385 + 1.386 + public final boolean isMixed() { 1.387 + return isMixed; 1.388 + } 1.389 + 1.390 + public final WildcardMode getWildcard() { 1.391 + return wildcard; 1.392 + } 1.393 + 1.394 + public final C getDOMHandler() { 1.395 + return domHandler; 1.396 + } 1.397 +}