src/share/jaxws_classes/com/sun/xml/internal/bind/v2/model/impl/TypeInfoSetImpl.java

Fri, 22 Nov 2013 21:11:19 +0100

author
mkos
date
Fri, 22 Nov 2013 21:11:19 +0100
changeset 450
b0c2840e2513
parent 0
373ffda63c9a
permissions
-rw-r--r--

8010935: Better XML handling
8027378: Two closed/javax/xml/8005432 fails with jdk7u51b04
8028382: Two javax/xml/8005433 tests still fail after the fix JDK-8028147
Summary: base fix + fixes for test regressions; fix also reviewed by Maxim Soloviev, Alexander Fomin
Reviewed-by: mchung, mgrebac, mullan

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.xml.internal.bind.v2.model.impl;
aoqi@0 27
aoqi@0 28 import java.util.Collections;
aoqi@0 29 import java.util.HashMap;
aoqi@0 30 import java.util.Iterator;
aoqi@0 31 import java.util.LinkedHashMap;
aoqi@0 32 import java.util.Map;
aoqi@0 33
aoqi@0 34 import javax.xml.bind.JAXBContext;
aoqi@0 35 import javax.xml.bind.JAXBException;
aoqi@0 36 import javax.xml.bind.Marshaller;
aoqi@0 37 import javax.xml.bind.annotation.XmlNs;
aoqi@0 38 import javax.xml.bind.annotation.XmlNsForm;
aoqi@0 39 import javax.xml.bind.annotation.XmlRegistry;
aoqi@0 40 import javax.xml.bind.annotation.XmlSchema;
aoqi@0 41 import javax.xml.bind.annotation.XmlTransient;
aoqi@0 42 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
aoqi@0 43 import javax.xml.namespace.QName;
aoqi@0 44 import javax.xml.transform.Result;
aoqi@0 45
aoqi@0 46 import com.sun.xml.internal.bind.v2.model.annotation.AnnotationReader;
aoqi@0 47 import com.sun.xml.internal.bind.v2.model.core.BuiltinLeafInfo;
aoqi@0 48 import com.sun.xml.internal.bind.v2.model.core.ClassInfo;
aoqi@0 49 import com.sun.xml.internal.bind.v2.model.core.LeafInfo;
aoqi@0 50 import com.sun.xml.internal.bind.v2.model.core.NonElement;
aoqi@0 51 import com.sun.xml.internal.bind.v2.model.core.Ref;
aoqi@0 52 import com.sun.xml.internal.bind.v2.model.core.TypeInfo;
aoqi@0 53 import com.sun.xml.internal.bind.v2.model.core.TypeInfoSet;
aoqi@0 54 import com.sun.xml.internal.bind.v2.model.nav.Navigator;
aoqi@0 55 import com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationException;
aoqi@0 56 import com.sun.xml.internal.bind.v2.runtime.RuntimeUtil;
aoqi@0 57 import com.sun.xml.internal.bind.v2.util.FlattenIterator;
aoqi@0 58
aoqi@0 59 /**
aoqi@0 60 * Set of {@link TypeInfo}s.
aoqi@0 61 *
aoqi@0 62 * <p>
aoqi@0 63 * This contains a fixed set of {@link LeafInfo}s and arbitrary set of {@link ClassInfo}s.
aoqi@0 64 *
aoqi@0 65 * <p>
aoqi@0 66 * Members are annotated with JAXB annotations so that we can dump it easily.
aoqi@0 67 *
aoqi@0 68 * @author Kohsuke Kawaguchi
aoqi@0 69 */
aoqi@0 70 class TypeInfoSetImpl<T,C,F,M> implements TypeInfoSet<T,C,F,M> {
aoqi@0 71
aoqi@0 72 @XmlTransient
aoqi@0 73 public final Navigator<T,C,F,M> nav;
aoqi@0 74
aoqi@0 75 @XmlTransient
aoqi@0 76 public final AnnotationReader<T,C,F,M> reader;
aoqi@0 77
aoqi@0 78 /**
aoqi@0 79 * All the leaves.
aoqi@0 80 */
aoqi@0 81 private final Map<T,BuiltinLeafInfo<T,C>> builtins =
aoqi@0 82 new LinkedHashMap<T,BuiltinLeafInfo<T,C>>();
aoqi@0 83
aoqi@0 84 /** All {@link EnumLeafInfoImpl}s. */
aoqi@0 85 private final Map<C,EnumLeafInfoImpl<T,C,F,M>> enums =
aoqi@0 86 new LinkedHashMap<C,EnumLeafInfoImpl<T,C,F,M>>();
aoqi@0 87
aoqi@0 88 /** All {@link ArrayInfoImpl}s. */
aoqi@0 89 private final Map<T,ArrayInfoImpl<T,C,F,M>> arrays =
aoqi@0 90 new LinkedHashMap<T,ArrayInfoImpl<T,C,F,M>>();
aoqi@0 91
aoqi@0 92 /**
aoqi@0 93 * All the user-defined classes.
aoqi@0 94 *
aoqi@0 95 * Using {@link LinkedHashMap} allows us to process classes
aoqi@0 96 * in the order they are given to us. When the user incorrectly
aoqi@0 97 * puts an unexpected class into a reference graph, this causes
aoqi@0 98 * an error to be reported on a class closer to the user's code.
aoqi@0 99 */
aoqi@0 100 @XmlJavaTypeAdapter(RuntimeUtil.ToStringAdapter.class)
aoqi@0 101 private final Map<C,ClassInfoImpl<T,C,F,M>> beans
aoqi@0 102 = new LinkedHashMap<C,ClassInfoImpl<T,C,F,M>>();
aoqi@0 103
aoqi@0 104 @XmlTransient
aoqi@0 105 private final Map<C,ClassInfoImpl<T,C,F,M>> beansView =
aoqi@0 106 Collections.unmodifiableMap(beans);
aoqi@0 107
aoqi@0 108 /**
aoqi@0 109 * The element mapping.
aoqi@0 110 */
aoqi@0 111 private final Map<C,Map<QName,ElementInfoImpl<T,C,F,M>>> elementMappings =
aoqi@0 112 new LinkedHashMap<C,Map<QName,ElementInfoImpl<T,C,F,M>>>();
aoqi@0 113
aoqi@0 114 private final Iterable<? extends ElementInfoImpl<T,C,F,M>> allElements =
aoqi@0 115 new Iterable<ElementInfoImpl<T,C,F,M>>() {
aoqi@0 116 public Iterator<ElementInfoImpl<T,C,F,M>> iterator() {
aoqi@0 117 return new FlattenIterator<ElementInfoImpl<T,C,F,M>>(elementMappings.values());
aoqi@0 118 }
aoqi@0 119 };
aoqi@0 120
aoqi@0 121 /**
aoqi@0 122 * {@link TypeInfo} for <tt>xs:anyType</tt>.
aoqi@0 123 *
aoqi@0 124 * anyType is the only {@link TypeInfo} that works with an interface,
aoqi@0 125 * and accordingly it requires a lot of special casing.
aoqi@0 126 */
aoqi@0 127 private final NonElement<T,C> anyType;
aoqi@0 128
aoqi@0 129 /**
aoqi@0 130 * Lazily parsed set of {@link XmlNs}s.
aoqi@0 131 *
aoqi@0 132 * @see #getXmlNs(String)
aoqi@0 133 */
aoqi@0 134 private Map<String,Map<String,String>> xmlNsCache;
aoqi@0 135
aoqi@0 136 public TypeInfoSetImpl(Navigator<T,C,F,M> nav,
aoqi@0 137 AnnotationReader<T,C,F,M> reader,
aoqi@0 138 Map<T,? extends BuiltinLeafInfoImpl<T,C>> leaves) {
aoqi@0 139 this.nav = nav;
aoqi@0 140 this.reader = reader;
aoqi@0 141 this.builtins.putAll(leaves);
aoqi@0 142
aoqi@0 143 this.anyType = createAnyType();
aoqi@0 144
aoqi@0 145 // register primitive types.
aoqi@0 146 for (Map.Entry<Class, Class> e : RuntimeUtil.primitiveToBox.entrySet()) {
aoqi@0 147 this.builtins.put( nav.getPrimitive(e.getKey()), leaves.get(nav.ref(e.getValue())) );
aoqi@0 148 }
aoqi@0 149
aoqi@0 150 // make sure at lease we got a map for global ones.
aoqi@0 151 elementMappings.put(null,new LinkedHashMap<QName,ElementInfoImpl<T,C,F,M>>());
aoqi@0 152 }
aoqi@0 153
aoqi@0 154 protected NonElement<T,C> createAnyType() {
aoqi@0 155 return new AnyTypeImpl<T,C>(nav);
aoqi@0 156 }
aoqi@0 157
aoqi@0 158 public Navigator<T,C,F,M> getNavigator() {
aoqi@0 159 return nav;
aoqi@0 160 }
aoqi@0 161
aoqi@0 162 /**
aoqi@0 163 * Adds a new {@link ClassInfo} to the set.
aoqi@0 164 */
aoqi@0 165 public void add( ClassInfoImpl<T,C,F,M> ci ) {
aoqi@0 166 beans.put( ci.getClazz(), ci );
aoqi@0 167 }
aoqi@0 168
aoqi@0 169 /**
aoqi@0 170 * Adds a new {@link LeafInfo} to the set.
aoqi@0 171 */
aoqi@0 172 public void add( EnumLeafInfoImpl<T,C,F,M> li ) {
aoqi@0 173 enums.put( li.clazz, li );
aoqi@0 174 }
aoqi@0 175
aoqi@0 176 public void add(ArrayInfoImpl<T, C, F, M> ai) {
aoqi@0 177 arrays.put( ai.getType(), ai );
aoqi@0 178 }
aoqi@0 179
aoqi@0 180 /**
aoqi@0 181 * Returns a {@link TypeInfo} for the given type.
aoqi@0 182 *
aoqi@0 183 * @return
aoqi@0 184 * null if the specified type cannot be bound by JAXB, or
aoqi@0 185 * not known to this set.
aoqi@0 186 */
aoqi@0 187 public NonElement<T,C> getTypeInfo( T type ) {
aoqi@0 188 type = nav.erasure(type); // replace type variables by their bounds
aoqi@0 189
aoqi@0 190 LeafInfo<T,C> l = builtins.get(type);
aoqi@0 191 if(l!=null) return l;
aoqi@0 192
aoqi@0 193 if( nav.isArray(type) ) {
aoqi@0 194 return arrays.get(type);
aoqi@0 195 }
aoqi@0 196
aoqi@0 197 C d = nav.asDecl(type);
aoqi@0 198 if(d==null) return null;
aoqi@0 199 return getClassInfo(d);
aoqi@0 200 }
aoqi@0 201
aoqi@0 202 public NonElement<T,C> getAnyTypeInfo() {
aoqi@0 203 return anyType;
aoqi@0 204 }
aoqi@0 205
aoqi@0 206 /**
aoqi@0 207 * This method is used to add a root reference to a model.
aoqi@0 208 */
aoqi@0 209 public NonElement<T,C> getTypeInfo(Ref<T,C> ref) {
aoqi@0 210 // TODO: handle XmlValueList
aoqi@0 211 assert !ref.valueList;
aoqi@0 212 C c = nav.asDecl(ref.type);
aoqi@0 213 if(c!=null && reader.getClassAnnotation(XmlRegistry.class,c,null/*TODO: is this right?*/)!=null) {
aoqi@0 214 return null; // TODO: is this correct?
aoqi@0 215 } else
aoqi@0 216 return getTypeInfo(ref.type);
aoqi@0 217 }
aoqi@0 218
aoqi@0 219 /**
aoqi@0 220 * Returns all the {@link ClassInfo}s known to this set.
aoqi@0 221 */
aoqi@0 222 public Map<C,? extends ClassInfoImpl<T,C,F,M>> beans() {
aoqi@0 223 return beansView;
aoqi@0 224 }
aoqi@0 225
aoqi@0 226 public Map<T, ? extends BuiltinLeafInfo<T,C>> builtins() {
aoqi@0 227 return builtins;
aoqi@0 228 }
aoqi@0 229
aoqi@0 230 public Map<C, ? extends EnumLeafInfoImpl<T,C,F,M>> enums() {
aoqi@0 231 return enums;
aoqi@0 232 }
aoqi@0 233
aoqi@0 234 public Map<? extends T, ? extends ArrayInfoImpl<T,C,F,M>> arrays() {
aoqi@0 235 return arrays;
aoqi@0 236 }
aoqi@0 237
aoqi@0 238 /**
aoqi@0 239 * Returns a {@link ClassInfo} for the given bean.
aoqi@0 240 *
aoqi@0 241 * <p>
aoqi@0 242 * This method is almost like refinement of {@link #getTypeInfo(Object)} except
aoqi@0 243 * our C cannot derive from T.
aoqi@0 244 *
aoqi@0 245 * @return
aoqi@0 246 * null if the specified type is not bound by JAXB or otherwise
aoqi@0 247 * unknown to this set.
aoqi@0 248 */
aoqi@0 249 public NonElement<T,C> getClassInfo( C type ) {
aoqi@0 250 LeafInfo<T,C> l = builtins.get(nav.use(type));
aoqi@0 251 if(l!=null) return l;
aoqi@0 252
aoqi@0 253 l = enums.get(type);
aoqi@0 254 if(l!=null) return l;
aoqi@0 255
aoqi@0 256 if(nav.asDecl(Object.class).equals(type))
aoqi@0 257 return anyType;
aoqi@0 258
aoqi@0 259 return beans.get(type);
aoqi@0 260 }
aoqi@0 261
aoqi@0 262 public ElementInfoImpl<T,C,F,M> getElementInfo( C scope, QName name ) {
aoqi@0 263 while(scope!=null) {
aoqi@0 264 Map<QName,ElementInfoImpl<T,C,F,M>> m = elementMappings.get(scope);
aoqi@0 265 if(m!=null) {
aoqi@0 266 ElementInfoImpl<T,C,F,M> r = m.get(name);
aoqi@0 267 if(r!=null) return r;
aoqi@0 268 }
aoqi@0 269 scope = nav.getSuperClass(scope);
aoqi@0 270 }
aoqi@0 271 return elementMappings.get(null).get(name);
aoqi@0 272 }
aoqi@0 273
aoqi@0 274 /**
aoqi@0 275 * @param builder
aoqi@0 276 * used for reporting errors.
aoqi@0 277 */
aoqi@0 278 public final void add( ElementInfoImpl<T,C,F,M> ei, ModelBuilder<T,C,F,M> builder ) {
aoqi@0 279 C scope = null;
aoqi@0 280 if(ei.getScope()!=null)
aoqi@0 281 scope = ei.getScope().getClazz();
aoqi@0 282
aoqi@0 283 Map<QName,ElementInfoImpl<T,C,F,M>> m = elementMappings.get(scope);
aoqi@0 284 if(m==null)
aoqi@0 285 elementMappings.put(scope,m=new LinkedHashMap<QName,ElementInfoImpl<T,C,F,M>>());
aoqi@0 286
aoqi@0 287 ElementInfoImpl<T,C,F,M> existing = m.put(ei.getElementName(),ei);
aoqi@0 288
aoqi@0 289 if(existing!=null) {
aoqi@0 290 QName en = ei.getElementName();
aoqi@0 291 builder.reportError(
aoqi@0 292 new IllegalAnnotationException(
aoqi@0 293 Messages.CONFLICTING_XML_ELEMENT_MAPPING.format(en.getNamespaceURI(),en.getLocalPart()),
aoqi@0 294 ei, existing ));
aoqi@0 295 }
aoqi@0 296 }
aoqi@0 297
aoqi@0 298 public Map<QName,? extends ElementInfoImpl<T,C,F,M>> getElementMappings( C scope ) {
aoqi@0 299 return elementMappings.get(scope);
aoqi@0 300 }
aoqi@0 301
aoqi@0 302 public Iterable<? extends ElementInfoImpl<T,C,F,M>> getAllElements() {
aoqi@0 303 return allElements;
aoqi@0 304 }
aoqi@0 305
aoqi@0 306 public Map<String,String> getXmlNs(String namespaceUri) {
aoqi@0 307 if(xmlNsCache==null) {
aoqi@0 308 xmlNsCache = new HashMap<String,Map<String,String>>();
aoqi@0 309
aoqi@0 310 for (ClassInfoImpl<T, C, F, M> ci : beans().values()) {
aoqi@0 311 XmlSchema xs = reader.getPackageAnnotation( XmlSchema.class, ci.getClazz(), null );
aoqi@0 312 if(xs==null)
aoqi@0 313 continue;
aoqi@0 314
aoqi@0 315 String uri = xs.namespace();
aoqi@0 316 Map<String,String> m = xmlNsCache.get(uri);
aoqi@0 317 if(m==null)
aoqi@0 318 xmlNsCache.put(uri,m=new HashMap<String, String>());
aoqi@0 319
aoqi@0 320 for( XmlNs xns : xs.xmlns() ) {
aoqi@0 321 m.put(xns.prefix(),xns.namespaceURI());
aoqi@0 322 }
aoqi@0 323 }
aoqi@0 324 }
aoqi@0 325
aoqi@0 326 Map<String,String> r = xmlNsCache.get(namespaceUri);
aoqi@0 327 if(r!=null) return r;
aoqi@0 328 else return Collections.emptyMap();
aoqi@0 329 }
aoqi@0 330
aoqi@0 331 public Map<String,String> getSchemaLocations() {
aoqi@0 332 Map<String, String> r = new HashMap<String,String>();
aoqi@0 333 for (ClassInfoImpl<T, C, F, M> ci : beans().values()) {
aoqi@0 334 XmlSchema xs = reader.getPackageAnnotation( XmlSchema.class, ci.getClazz(), null );
aoqi@0 335 if(xs==null)
aoqi@0 336 continue;
aoqi@0 337
aoqi@0 338 String loc = xs.location();
aoqi@0 339 if(loc.equals(XmlSchema.NO_LOCATION))
aoqi@0 340 continue; // unspecified
aoqi@0 341
aoqi@0 342 r.put(xs.namespace(),loc);
aoqi@0 343 }
aoqi@0 344 return r;
aoqi@0 345 }
aoqi@0 346
aoqi@0 347 public final XmlNsForm getElementFormDefault(String nsUri) {
aoqi@0 348 for (ClassInfoImpl<T, C, F, M> ci : beans().values()) {
aoqi@0 349 XmlSchema xs = reader.getPackageAnnotation( XmlSchema.class, ci.getClazz(), null );
aoqi@0 350 if(xs==null)
aoqi@0 351 continue;
aoqi@0 352
aoqi@0 353 if(!xs.namespace().equals(nsUri))
aoqi@0 354 continue;
aoqi@0 355
aoqi@0 356 XmlNsForm xnf = xs.elementFormDefault();
aoqi@0 357 if(xnf!=XmlNsForm.UNSET)
aoqi@0 358 return xnf;
aoqi@0 359 }
aoqi@0 360 return XmlNsForm.UNSET;
aoqi@0 361 }
aoqi@0 362
aoqi@0 363 public final XmlNsForm getAttributeFormDefault(String nsUri) {
aoqi@0 364 for (ClassInfoImpl<T,C,F,M> ci : beans().values()) {
aoqi@0 365 XmlSchema xs = reader.getPackageAnnotation( XmlSchema.class, ci.getClazz(), null );
aoqi@0 366 if(xs==null)
aoqi@0 367 continue;
aoqi@0 368
aoqi@0 369 if(!xs.namespace().equals(nsUri))
aoqi@0 370 continue;
aoqi@0 371
aoqi@0 372 XmlNsForm xnf = xs.attributeFormDefault();
aoqi@0 373 if(xnf!=XmlNsForm.UNSET)
aoqi@0 374 return xnf;
aoqi@0 375 }
aoqi@0 376 return XmlNsForm.UNSET;
aoqi@0 377 }
aoqi@0 378
aoqi@0 379 /**
aoqi@0 380 * Dumps this model into XML.
aoqi@0 381 *
aoqi@0 382 * For debug only.
aoqi@0 383 *
aoqi@0 384 * TODO: not sure if this actually works. We don't really know what are T,C.
aoqi@0 385 */
aoqi@0 386 public void dump( Result out ) throws JAXBException {
aoqi@0 387 JAXBContext context = JAXBContext.newInstance(this.getClass());
aoqi@0 388 Marshaller m = context.createMarshaller();
aoqi@0 389 m.marshal(this,out);
aoqi@0 390 }
aoqi@0 391 }

mercurial