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/TypeInfoSetImpl.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,391 @@ 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.HashMap; 1.33 +import java.util.Iterator; 1.34 +import java.util.LinkedHashMap; 1.35 +import java.util.Map; 1.36 + 1.37 +import javax.xml.bind.JAXBContext; 1.38 +import javax.xml.bind.JAXBException; 1.39 +import javax.xml.bind.Marshaller; 1.40 +import javax.xml.bind.annotation.XmlNs; 1.41 +import javax.xml.bind.annotation.XmlNsForm; 1.42 +import javax.xml.bind.annotation.XmlRegistry; 1.43 +import javax.xml.bind.annotation.XmlSchema; 1.44 +import javax.xml.bind.annotation.XmlTransient; 1.45 +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; 1.46 +import javax.xml.namespace.QName; 1.47 +import javax.xml.transform.Result; 1.48 + 1.49 +import com.sun.xml.internal.bind.v2.model.annotation.AnnotationReader; 1.50 +import com.sun.xml.internal.bind.v2.model.core.BuiltinLeafInfo; 1.51 +import com.sun.xml.internal.bind.v2.model.core.ClassInfo; 1.52 +import com.sun.xml.internal.bind.v2.model.core.LeafInfo; 1.53 +import com.sun.xml.internal.bind.v2.model.core.NonElement; 1.54 +import com.sun.xml.internal.bind.v2.model.core.Ref; 1.55 +import com.sun.xml.internal.bind.v2.model.core.TypeInfo; 1.56 +import com.sun.xml.internal.bind.v2.model.core.TypeInfoSet; 1.57 +import com.sun.xml.internal.bind.v2.model.nav.Navigator; 1.58 +import com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationException; 1.59 +import com.sun.xml.internal.bind.v2.runtime.RuntimeUtil; 1.60 +import com.sun.xml.internal.bind.v2.util.FlattenIterator; 1.61 + 1.62 +/** 1.63 + * Set of {@link TypeInfo}s. 1.64 + * 1.65 + * <p> 1.66 + * This contains a fixed set of {@link LeafInfo}s and arbitrary set of {@link ClassInfo}s. 1.67 + * 1.68 + * <p> 1.69 + * Members are annotated with JAXB annotations so that we can dump it easily. 1.70 + * 1.71 + * @author Kohsuke Kawaguchi 1.72 + */ 1.73 +class TypeInfoSetImpl<T,C,F,M> implements TypeInfoSet<T,C,F,M> { 1.74 + 1.75 + @XmlTransient 1.76 + public final Navigator<T,C,F,M> nav; 1.77 + 1.78 + @XmlTransient 1.79 + public final AnnotationReader<T,C,F,M> reader; 1.80 + 1.81 + /** 1.82 + * All the leaves. 1.83 + */ 1.84 + private final Map<T,BuiltinLeafInfo<T,C>> builtins = 1.85 + new LinkedHashMap<T,BuiltinLeafInfo<T,C>>(); 1.86 + 1.87 + /** All {@link EnumLeafInfoImpl}s. */ 1.88 + private final Map<C,EnumLeafInfoImpl<T,C,F,M>> enums = 1.89 + new LinkedHashMap<C,EnumLeafInfoImpl<T,C,F,M>>(); 1.90 + 1.91 + /** All {@link ArrayInfoImpl}s. */ 1.92 + private final Map<T,ArrayInfoImpl<T,C,F,M>> arrays = 1.93 + new LinkedHashMap<T,ArrayInfoImpl<T,C,F,M>>(); 1.94 + 1.95 + /** 1.96 + * All the user-defined classes. 1.97 + * 1.98 + * Using {@link LinkedHashMap} allows us to process classes 1.99 + * in the order they are given to us. When the user incorrectly 1.100 + * puts an unexpected class into a reference graph, this causes 1.101 + * an error to be reported on a class closer to the user's code. 1.102 + */ 1.103 + @XmlJavaTypeAdapter(RuntimeUtil.ToStringAdapter.class) 1.104 + private final Map<C,ClassInfoImpl<T,C,F,M>> beans 1.105 + = new LinkedHashMap<C,ClassInfoImpl<T,C,F,M>>(); 1.106 + 1.107 + @XmlTransient 1.108 + private final Map<C,ClassInfoImpl<T,C,F,M>> beansView = 1.109 + Collections.unmodifiableMap(beans); 1.110 + 1.111 + /** 1.112 + * The element mapping. 1.113 + */ 1.114 + private final Map<C,Map<QName,ElementInfoImpl<T,C,F,M>>> elementMappings = 1.115 + new LinkedHashMap<C,Map<QName,ElementInfoImpl<T,C,F,M>>>(); 1.116 + 1.117 + private final Iterable<? extends ElementInfoImpl<T,C,F,M>> allElements = 1.118 + new Iterable<ElementInfoImpl<T,C,F,M>>() { 1.119 + public Iterator<ElementInfoImpl<T,C,F,M>> iterator() { 1.120 + return new FlattenIterator<ElementInfoImpl<T,C,F,M>>(elementMappings.values()); 1.121 + } 1.122 + }; 1.123 + 1.124 + /** 1.125 + * {@link TypeInfo} for <tt>xs:anyType</tt>. 1.126 + * 1.127 + * anyType is the only {@link TypeInfo} that works with an interface, 1.128 + * and accordingly it requires a lot of special casing. 1.129 + */ 1.130 + private final NonElement<T,C> anyType; 1.131 + 1.132 + /** 1.133 + * Lazily parsed set of {@link XmlNs}s. 1.134 + * 1.135 + * @see #getXmlNs(String) 1.136 + */ 1.137 + private Map<String,Map<String,String>> xmlNsCache; 1.138 + 1.139 + public TypeInfoSetImpl(Navigator<T,C,F,M> nav, 1.140 + AnnotationReader<T,C,F,M> reader, 1.141 + Map<T,? extends BuiltinLeafInfoImpl<T,C>> leaves) { 1.142 + this.nav = nav; 1.143 + this.reader = reader; 1.144 + this.builtins.putAll(leaves); 1.145 + 1.146 + this.anyType = createAnyType(); 1.147 + 1.148 + // register primitive types. 1.149 + for (Map.Entry<Class, Class> e : RuntimeUtil.primitiveToBox.entrySet()) { 1.150 + this.builtins.put( nav.getPrimitive(e.getKey()), leaves.get(nav.ref(e.getValue())) ); 1.151 + } 1.152 + 1.153 + // make sure at lease we got a map for global ones. 1.154 + elementMappings.put(null,new LinkedHashMap<QName,ElementInfoImpl<T,C,F,M>>()); 1.155 + } 1.156 + 1.157 + protected NonElement<T,C> createAnyType() { 1.158 + return new AnyTypeImpl<T,C>(nav); 1.159 + } 1.160 + 1.161 + public Navigator<T,C,F,M> getNavigator() { 1.162 + return nav; 1.163 + } 1.164 + 1.165 + /** 1.166 + * Adds a new {@link ClassInfo} to the set. 1.167 + */ 1.168 + public void add( ClassInfoImpl<T,C,F,M> ci ) { 1.169 + beans.put( ci.getClazz(), ci ); 1.170 + } 1.171 + 1.172 + /** 1.173 + * Adds a new {@link LeafInfo} to the set. 1.174 + */ 1.175 + public void add( EnumLeafInfoImpl<T,C,F,M> li ) { 1.176 + enums.put( li.clazz, li ); 1.177 + } 1.178 + 1.179 + public void add(ArrayInfoImpl<T, C, F, M> ai) { 1.180 + arrays.put( ai.getType(), ai ); 1.181 + } 1.182 + 1.183 + /** 1.184 + * Returns a {@link TypeInfo} for the given type. 1.185 + * 1.186 + * @return 1.187 + * null if the specified type cannot be bound by JAXB, or 1.188 + * not known to this set. 1.189 + */ 1.190 + public NonElement<T,C> getTypeInfo( T type ) { 1.191 + type = nav.erasure(type); // replace type variables by their bounds 1.192 + 1.193 + LeafInfo<T,C> l = builtins.get(type); 1.194 + if(l!=null) return l; 1.195 + 1.196 + if( nav.isArray(type) ) { 1.197 + return arrays.get(type); 1.198 + } 1.199 + 1.200 + C d = nav.asDecl(type); 1.201 + if(d==null) return null; 1.202 + return getClassInfo(d); 1.203 + } 1.204 + 1.205 + public NonElement<T,C> getAnyTypeInfo() { 1.206 + return anyType; 1.207 + } 1.208 + 1.209 + /** 1.210 + * This method is used to add a root reference to a model. 1.211 + */ 1.212 + public NonElement<T,C> getTypeInfo(Ref<T,C> ref) { 1.213 + // TODO: handle XmlValueList 1.214 + assert !ref.valueList; 1.215 + C c = nav.asDecl(ref.type); 1.216 + if(c!=null && reader.getClassAnnotation(XmlRegistry.class,c,null/*TODO: is this right?*/)!=null) { 1.217 + return null; // TODO: is this correct? 1.218 + } else 1.219 + return getTypeInfo(ref.type); 1.220 + } 1.221 + 1.222 + /** 1.223 + * Returns all the {@link ClassInfo}s known to this set. 1.224 + */ 1.225 + public Map<C,? extends ClassInfoImpl<T,C,F,M>> beans() { 1.226 + return beansView; 1.227 + } 1.228 + 1.229 + public Map<T, ? extends BuiltinLeafInfo<T,C>> builtins() { 1.230 + return builtins; 1.231 + } 1.232 + 1.233 + public Map<C, ? extends EnumLeafInfoImpl<T,C,F,M>> enums() { 1.234 + return enums; 1.235 + } 1.236 + 1.237 + public Map<? extends T, ? extends ArrayInfoImpl<T,C,F,M>> arrays() { 1.238 + return arrays; 1.239 + } 1.240 + 1.241 + /** 1.242 + * Returns a {@link ClassInfo} for the given bean. 1.243 + * 1.244 + * <p> 1.245 + * This method is almost like refinement of {@link #getTypeInfo(Object)} except 1.246 + * our C cannot derive from T. 1.247 + * 1.248 + * @return 1.249 + * null if the specified type is not bound by JAXB or otherwise 1.250 + * unknown to this set. 1.251 + */ 1.252 + public NonElement<T,C> getClassInfo( C type ) { 1.253 + LeafInfo<T,C> l = builtins.get(nav.use(type)); 1.254 + if(l!=null) return l; 1.255 + 1.256 + l = enums.get(type); 1.257 + if(l!=null) return l; 1.258 + 1.259 + if(nav.asDecl(Object.class).equals(type)) 1.260 + return anyType; 1.261 + 1.262 + return beans.get(type); 1.263 + } 1.264 + 1.265 + public ElementInfoImpl<T,C,F,M> getElementInfo( C scope, QName name ) { 1.266 + while(scope!=null) { 1.267 + Map<QName,ElementInfoImpl<T,C,F,M>> m = elementMappings.get(scope); 1.268 + if(m!=null) { 1.269 + ElementInfoImpl<T,C,F,M> r = m.get(name); 1.270 + if(r!=null) return r; 1.271 + } 1.272 + scope = nav.getSuperClass(scope); 1.273 + } 1.274 + return elementMappings.get(null).get(name); 1.275 + } 1.276 + 1.277 + /** 1.278 + * @param builder 1.279 + * used for reporting errors. 1.280 + */ 1.281 + public final void add( ElementInfoImpl<T,C,F,M> ei, ModelBuilder<T,C,F,M> builder ) { 1.282 + C scope = null; 1.283 + if(ei.getScope()!=null) 1.284 + scope = ei.getScope().getClazz(); 1.285 + 1.286 + Map<QName,ElementInfoImpl<T,C,F,M>> m = elementMappings.get(scope); 1.287 + if(m==null) 1.288 + elementMappings.put(scope,m=new LinkedHashMap<QName,ElementInfoImpl<T,C,F,M>>()); 1.289 + 1.290 + ElementInfoImpl<T,C,F,M> existing = m.put(ei.getElementName(),ei); 1.291 + 1.292 + if(existing!=null) { 1.293 + QName en = ei.getElementName(); 1.294 + builder.reportError( 1.295 + new IllegalAnnotationException( 1.296 + Messages.CONFLICTING_XML_ELEMENT_MAPPING.format(en.getNamespaceURI(),en.getLocalPart()), 1.297 + ei, existing )); 1.298 + } 1.299 + } 1.300 + 1.301 + public Map<QName,? extends ElementInfoImpl<T,C,F,M>> getElementMappings( C scope ) { 1.302 + return elementMappings.get(scope); 1.303 + } 1.304 + 1.305 + public Iterable<? extends ElementInfoImpl<T,C,F,M>> getAllElements() { 1.306 + return allElements; 1.307 + } 1.308 + 1.309 + public Map<String,String> getXmlNs(String namespaceUri) { 1.310 + if(xmlNsCache==null) { 1.311 + xmlNsCache = new HashMap<String,Map<String,String>>(); 1.312 + 1.313 + for (ClassInfoImpl<T, C, F, M> ci : beans().values()) { 1.314 + XmlSchema xs = reader.getPackageAnnotation( XmlSchema.class, ci.getClazz(), null ); 1.315 + if(xs==null) 1.316 + continue; 1.317 + 1.318 + String uri = xs.namespace(); 1.319 + Map<String,String> m = xmlNsCache.get(uri); 1.320 + if(m==null) 1.321 + xmlNsCache.put(uri,m=new HashMap<String, String>()); 1.322 + 1.323 + for( XmlNs xns : xs.xmlns() ) { 1.324 + m.put(xns.prefix(),xns.namespaceURI()); 1.325 + } 1.326 + } 1.327 + } 1.328 + 1.329 + Map<String,String> r = xmlNsCache.get(namespaceUri); 1.330 + if(r!=null) return r; 1.331 + else return Collections.emptyMap(); 1.332 + } 1.333 + 1.334 + public Map<String,String> getSchemaLocations() { 1.335 + Map<String, String> r = new HashMap<String,String>(); 1.336 + for (ClassInfoImpl<T, C, F, M> ci : beans().values()) { 1.337 + XmlSchema xs = reader.getPackageAnnotation( XmlSchema.class, ci.getClazz(), null ); 1.338 + if(xs==null) 1.339 + continue; 1.340 + 1.341 + String loc = xs.location(); 1.342 + if(loc.equals(XmlSchema.NO_LOCATION)) 1.343 + continue; // unspecified 1.344 + 1.345 + r.put(xs.namespace(),loc); 1.346 + } 1.347 + return r; 1.348 + } 1.349 + 1.350 + public final XmlNsForm getElementFormDefault(String nsUri) { 1.351 + for (ClassInfoImpl<T, C, F, M> ci : beans().values()) { 1.352 + XmlSchema xs = reader.getPackageAnnotation( XmlSchema.class, ci.getClazz(), null ); 1.353 + if(xs==null) 1.354 + continue; 1.355 + 1.356 + if(!xs.namespace().equals(nsUri)) 1.357 + continue; 1.358 + 1.359 + XmlNsForm xnf = xs.elementFormDefault(); 1.360 + if(xnf!=XmlNsForm.UNSET) 1.361 + return xnf; 1.362 + } 1.363 + return XmlNsForm.UNSET; 1.364 + } 1.365 + 1.366 + public final XmlNsForm getAttributeFormDefault(String nsUri) { 1.367 + for (ClassInfoImpl<T,C,F,M> ci : beans().values()) { 1.368 + XmlSchema xs = reader.getPackageAnnotation( XmlSchema.class, ci.getClazz(), null ); 1.369 + if(xs==null) 1.370 + continue; 1.371 + 1.372 + if(!xs.namespace().equals(nsUri)) 1.373 + continue; 1.374 + 1.375 + XmlNsForm xnf = xs.attributeFormDefault(); 1.376 + if(xnf!=XmlNsForm.UNSET) 1.377 + return xnf; 1.378 + } 1.379 + return XmlNsForm.UNSET; 1.380 + } 1.381 + 1.382 + /** 1.383 + * Dumps this model into XML. 1.384 + * 1.385 + * For debug only. 1.386 + * 1.387 + * TODO: not sure if this actually works. We don't really know what are T,C. 1.388 + */ 1.389 + public void dump( Result out ) throws JAXBException { 1.390 + JAXBContext context = JAXBContext.newInstance(this.getClass()); 1.391 + Marshaller m = context.createMarshaller(); 1.392 + m.marshal(this,out); 1.393 + } 1.394 +}