Sun, 18 Jun 2017 23:18:45 +0100
8172297: In java 8, the marshalling with JAX-WS does not escape carriage return
Reviewed-by: lancea
ohair@286 | 1 | /* |
mkos@384 | 2 | * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
ohair@286 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ohair@286 | 4 | * |
ohair@286 | 5 | * This code is free software; you can redistribute it and/or modify it |
ohair@286 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@286 | 7 | * published by the Free Software Foundation. Oracle designates this |
ohair@286 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@286 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
ohair@286 | 10 | * |
ohair@286 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ohair@286 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ohair@286 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ohair@286 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
ohair@286 | 15 | * accompanied this code). |
ohair@286 | 16 | * |
ohair@286 | 17 | * You should have received a copy of the GNU General Public License version |
ohair@286 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
ohair@286 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ohair@286 | 20 | * |
ohair@286 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@286 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@286 | 23 | * questions. |
ohair@286 | 24 | */ |
ohair@286 | 25 | |
ohair@286 | 26 | package com.sun.xml.internal.bind.v2.runtime; |
ohair@286 | 27 | |
ohair@286 | 28 | import java.io.IOException; |
ohair@286 | 29 | import java.lang.ref.WeakReference; |
ohair@286 | 30 | import java.lang.reflect.Field; |
ohair@286 | 31 | import java.lang.reflect.Method; |
ohair@286 | 32 | import java.lang.reflect.Type; |
ohair@286 | 33 | import java.util.Arrays; |
ohair@286 | 34 | import java.util.Collection; |
ohair@286 | 35 | import java.util.Collections; |
ohair@286 | 36 | import java.util.Comparator; |
ohair@286 | 37 | import java.util.HashMap; |
ohair@286 | 38 | import java.util.HashSet; |
ohair@286 | 39 | import java.util.LinkedHashMap; |
ohair@286 | 40 | import java.util.List; |
ohair@286 | 41 | import java.util.Map; |
ohair@286 | 42 | import java.util.Map.Entry; |
ohair@286 | 43 | import java.util.Set; |
ohair@286 | 44 | import java.util.TreeSet; |
ohair@286 | 45 | import javax.xml.bind.Binder; |
ohair@286 | 46 | import javax.xml.bind.JAXBContext; |
ohair@286 | 47 | import javax.xml.bind.JAXBElement; |
ohair@286 | 48 | import javax.xml.bind.JAXBException; |
ohair@286 | 49 | import javax.xml.bind.JAXBIntrospector; |
ohair@286 | 50 | import javax.xml.bind.Marshaller; |
ohair@286 | 51 | import javax.xml.bind.SchemaOutputResolver; |
ohair@286 | 52 | import javax.xml.bind.Unmarshaller; |
ohair@286 | 53 | import javax.xml.bind.Validator; |
ohair@286 | 54 | import javax.xml.bind.annotation.XmlAttachmentRef; |
ohair@286 | 55 | import javax.xml.bind.annotation.XmlList; |
ohair@286 | 56 | import javax.xml.bind.annotation.XmlNs; |
ohair@286 | 57 | import javax.xml.bind.annotation.XmlSchema; |
ohair@286 | 58 | import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; |
ohair@286 | 59 | import javax.xml.namespace.QName; |
ohair@286 | 60 | import javax.xml.parsers.DocumentBuilder; |
ohair@286 | 61 | import javax.xml.parsers.DocumentBuilderFactory; |
ohair@286 | 62 | import javax.xml.parsers.FactoryConfigurationError; |
ohair@286 | 63 | import javax.xml.parsers.ParserConfigurationException; |
ohair@286 | 64 | import javax.xml.transform.Result; |
ohair@286 | 65 | import javax.xml.transform.Transformer; |
ohair@286 | 66 | import javax.xml.transform.TransformerConfigurationException; |
ohair@286 | 67 | import javax.xml.transform.TransformerFactory; |
ohair@286 | 68 | import javax.xml.transform.sax.SAXTransformerFactory; |
ohair@286 | 69 | import javax.xml.transform.sax.TransformerHandler; |
ohair@286 | 70 | |
ohair@286 | 71 | import com.sun.istack.internal.NotNull; |
ohair@286 | 72 | import com.sun.istack.internal.Pool; |
alanb@368 | 73 | import com.sun.xml.internal.bind.v2.WellKnownNamespace; |
ohair@286 | 74 | import com.sun.xml.internal.bind.api.AccessorException; |
ohair@286 | 75 | import com.sun.xml.internal.bind.api.Bridge; |
ohair@286 | 76 | import com.sun.xml.internal.bind.api.BridgeContext; |
ohair@286 | 77 | import com.sun.xml.internal.bind.api.CompositeStructure; |
ohair@286 | 78 | import com.sun.xml.internal.bind.api.ErrorListener; |
ohair@286 | 79 | import com.sun.xml.internal.bind.api.JAXBRIContext; |
ohair@286 | 80 | import com.sun.xml.internal.bind.api.RawAccessor; |
ohair@286 | 81 | import com.sun.xml.internal.bind.api.TypeReference; |
ohair@286 | 82 | import com.sun.xml.internal.bind.unmarshaller.DOMScanner; |
ohair@286 | 83 | import com.sun.xml.internal.bind.util.Which; |
ohair@286 | 84 | import com.sun.xml.internal.bind.v2.model.annotation.RuntimeAnnotationReader; |
ohair@286 | 85 | import com.sun.xml.internal.bind.v2.model.annotation.RuntimeInlineAnnotationReader; |
ohair@286 | 86 | import com.sun.xml.internal.bind.v2.model.core.Adapter; |
ohair@286 | 87 | import com.sun.xml.internal.bind.v2.model.core.NonElement; |
ohair@286 | 88 | import com.sun.xml.internal.bind.v2.model.core.Ref; |
ohair@286 | 89 | import com.sun.xml.internal.bind.v2.model.impl.RuntimeBuiltinLeafInfoImpl; |
ohair@286 | 90 | import com.sun.xml.internal.bind.v2.model.impl.RuntimeModelBuilder; |
ohair@286 | 91 | import com.sun.xml.internal.bind.v2.model.nav.Navigator; |
ohair@286 | 92 | import com.sun.xml.internal.bind.v2.model.runtime.RuntimeArrayInfo; |
ohair@286 | 93 | import com.sun.xml.internal.bind.v2.model.runtime.RuntimeBuiltinLeafInfo; |
ohair@286 | 94 | import com.sun.xml.internal.bind.v2.model.runtime.RuntimeClassInfo; |
ohair@286 | 95 | import com.sun.xml.internal.bind.v2.model.runtime.RuntimeElementInfo; |
ohair@286 | 96 | import com.sun.xml.internal.bind.v2.model.runtime.RuntimeEnumLeafInfo; |
ohair@286 | 97 | import com.sun.xml.internal.bind.v2.model.runtime.RuntimeLeafInfo; |
ohair@286 | 98 | import com.sun.xml.internal.bind.v2.model.runtime.RuntimeTypeInfo; |
ohair@286 | 99 | import com.sun.xml.internal.bind.v2.model.runtime.RuntimeTypeInfoSet; |
ohair@286 | 100 | import com.sun.xml.internal.bind.v2.runtime.output.Encoded; |
ohair@286 | 101 | import com.sun.xml.internal.bind.v2.runtime.property.AttributeProperty; |
ohair@286 | 102 | import com.sun.xml.internal.bind.v2.runtime.property.Property; |
ohair@286 | 103 | import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor; |
ohair@286 | 104 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader; |
ohair@286 | 105 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.TagName; |
ohair@286 | 106 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl; |
ohair@286 | 107 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext; |
ohair@286 | 108 | import com.sun.xml.internal.bind.v2.schemagen.XmlSchemaGenerator; |
ohair@286 | 109 | import com.sun.xml.internal.bind.v2.util.EditDistance; |
ohair@286 | 110 | import com.sun.xml.internal.bind.v2.util.QNameMap; |
alanb@368 | 111 | import com.sun.xml.internal.bind.v2.util.XmlFactory; |
ohair@286 | 112 | import com.sun.xml.internal.txw2.output.ResultFactory; |
ohair@286 | 113 | |
ohair@286 | 114 | import org.w3c.dom.Document; |
ohair@286 | 115 | import org.w3c.dom.Element; |
ohair@286 | 116 | import org.w3c.dom.Node; |
ohair@286 | 117 | import org.xml.sax.SAXException; |
ohair@286 | 118 | import org.xml.sax.SAXParseException; |
ohair@286 | 119 | |
ohair@286 | 120 | /** |
ohair@286 | 121 | * This class provides the implementation of JAXBContext. |
ohair@286 | 122 | * |
ohair@286 | 123 | */ |
ohair@286 | 124 | public final class JAXBContextImpl extends JAXBRIContext { |
ohair@286 | 125 | |
ohair@286 | 126 | /** |
ohair@286 | 127 | * All the bridge classes. |
ohair@286 | 128 | */ |
ohair@286 | 129 | private final Map<TypeReference,Bridge> bridges = new LinkedHashMap<TypeReference,Bridge>(); |
ohair@286 | 130 | |
ohair@286 | 131 | /** |
ohair@286 | 132 | * Shared instance of {@link DocumentBuilder}. |
ohair@286 | 133 | * Lock before use. Lazily created. |
ohair@286 | 134 | */ |
ohair@286 | 135 | private static DocumentBuilder db; |
ohair@286 | 136 | |
ohair@286 | 137 | private final QNameMap<JaxBeanInfo> rootMap = new QNameMap<JaxBeanInfo>(); |
ohair@286 | 138 | private final HashMap<QName,JaxBeanInfo> typeMap = new HashMap<QName,JaxBeanInfo>(); |
ohair@286 | 139 | |
ohair@286 | 140 | /** |
ohair@286 | 141 | * Map from JAXB-bound {@link Class} to its {@link JaxBeanInfo}. |
ohair@286 | 142 | */ |
ohair@286 | 143 | private final Map<Class,JaxBeanInfo> beanInfoMap = new LinkedHashMap<Class,JaxBeanInfo>(); |
ohair@286 | 144 | |
ohair@286 | 145 | /** |
ohair@286 | 146 | * All created {@link JaxBeanInfo}s. |
ohair@286 | 147 | * Updated from each {@link JaxBeanInfo}s constructors to avoid infinite recursion |
ohair@286 | 148 | * for a cyclic reference. |
ohair@286 | 149 | * |
ohair@286 | 150 | * <p> |
ohair@286 | 151 | * This map is only used while the {@link JAXBContextImpl} is built and set to null |
ohair@286 | 152 | * to avoid keeping references too long. |
ohair@286 | 153 | */ |
ohair@286 | 154 | protected Map<RuntimeTypeInfo,JaxBeanInfo> beanInfos = new LinkedHashMap<RuntimeTypeInfo, JaxBeanInfo>(); |
ohair@286 | 155 | |
ohair@286 | 156 | private final Map<Class/*scope*/,Map<QName,ElementBeanInfoImpl>> elements = new LinkedHashMap<Class, Map<QName, ElementBeanInfoImpl>>(); |
ohair@286 | 157 | |
ohair@286 | 158 | /** |
ohair@286 | 159 | * Pool of {@link Marshaller}s. |
ohair@286 | 160 | */ |
ohair@286 | 161 | public final Pool<Marshaller> marshallerPool = new Pool.Impl<Marshaller>() { |
ohair@286 | 162 | protected @NotNull Marshaller create() { |
ohair@286 | 163 | return createMarshaller(); |
ohair@286 | 164 | } |
ohair@286 | 165 | }; |
ohair@286 | 166 | |
ohair@286 | 167 | public final Pool<Unmarshaller> unmarshallerPool = new Pool.Impl<Unmarshaller>() { |
ohair@286 | 168 | protected @NotNull Unmarshaller create() { |
ohair@286 | 169 | return createUnmarshaller(); |
ohair@286 | 170 | } |
ohair@286 | 171 | }; |
ohair@286 | 172 | |
ohair@286 | 173 | /** |
ohair@286 | 174 | * Used to assign indices to known names in this grammar. |
ohair@286 | 175 | * Reset to null once the build phase is completed. |
ohair@286 | 176 | */ |
ohair@286 | 177 | public NameBuilder nameBuilder = new NameBuilder(); |
ohair@286 | 178 | |
ohair@286 | 179 | /** |
ohair@286 | 180 | * Keeps the list of known names. |
ohair@286 | 181 | * This field is set once the build pahse is completed. |
ohair@286 | 182 | */ |
ohair@286 | 183 | public final NameList nameList; |
ohair@286 | 184 | |
ohair@286 | 185 | /** |
ohair@286 | 186 | * Input to the JAXBContext.newInstance, so that we can recreate |
ohair@286 | 187 | * {@link RuntimeTypeInfoSet} whenever we need. |
ohair@286 | 188 | */ |
ohair@286 | 189 | private final String defaultNsUri; |
ohair@286 | 190 | private final Class[] classes; |
ohair@286 | 191 | |
ohair@286 | 192 | /** |
ohair@286 | 193 | * true to reorder attributes lexicographically in preparation of the c14n support. |
ohair@286 | 194 | */ |
ohair@286 | 195 | protected final boolean c14nSupport; |
ohair@286 | 196 | |
ohair@286 | 197 | /** |
ohair@286 | 198 | * Flag that user has provided a custom AccessorFactory for JAXB to use |
ohair@286 | 199 | */ |
ohair@286 | 200 | public final boolean xmlAccessorFactorySupport; |
ohair@286 | 201 | |
ohair@286 | 202 | /** |
ohair@286 | 203 | * @see JAXBRIContext#TREAT_EVERYTHING_NILLABLE |
ohair@286 | 204 | */ |
ohair@286 | 205 | public final boolean allNillable; |
ohair@286 | 206 | |
ohair@286 | 207 | /** |
ohair@286 | 208 | * Store properties, so that they can be recovered in the run (is here because of JSON encoding of Jersey). |
ohair@286 | 209 | */ |
ohair@286 | 210 | public final boolean retainPropertyInfo; |
ohair@286 | 211 | |
ohair@286 | 212 | /** |
alanb@368 | 213 | * Suppress reflection accessor warnings. |
ohair@286 | 214 | */ |
ohair@286 | 215 | public final boolean supressAccessorWarnings; |
ohair@286 | 216 | |
ohair@286 | 217 | /** |
ohair@286 | 218 | * Improved xsi type handling. |
ohair@286 | 219 | */ |
ohair@286 | 220 | public final boolean improvedXsiTypeHandling; |
ohair@286 | 221 | |
alanb@368 | 222 | /** |
alanb@368 | 223 | * Disable security processing. |
alanb@368 | 224 | */ |
alanb@368 | 225 | public final boolean disableSecurityProcessing; |
alanb@368 | 226 | |
ohair@286 | 227 | private WeakReference<RuntimeTypeInfoSet> typeInfoSetCache; |
ohair@286 | 228 | |
ohair@286 | 229 | private @NotNull RuntimeAnnotationReader annotationReader; |
ohair@286 | 230 | |
ohair@286 | 231 | private /*almost final*/ boolean hasSwaRef; |
ohair@286 | 232 | private final @NotNull Map<Class,Class> subclassReplacements; |
ohair@286 | 233 | |
ohair@286 | 234 | /** |
alanb@368 | 235 | * If true, we aim for faster {@link JAXBContext} instantiation performance, |
ohair@286 | 236 | * instead of going after efficient sustained unmarshalling/marshalling performance. |
ohair@286 | 237 | * |
ohair@286 | 238 | * @since 2.0.4 |
ohair@286 | 239 | */ |
ohair@286 | 240 | public final boolean fastBoot; |
ohair@286 | 241 | |
ohair@286 | 242 | private Set<XmlNs> xmlNsSet = null; |
ohair@286 | 243 | |
ohair@286 | 244 | /** |
ohair@286 | 245 | * Returns declared XmlNs annotations (from package-level annotation XmlSchema |
ohair@286 | 246 | * |
ohair@286 | 247 | * @return set of all present XmlNs annotations |
ohair@286 | 248 | */ |
ohair@286 | 249 | public Set<XmlNs> getXmlNsSet() { |
ohair@286 | 250 | return xmlNsSet; |
ohair@286 | 251 | } |
ohair@286 | 252 | |
ohair@286 | 253 | private JAXBContextImpl(JAXBContextBuilder builder) throws JAXBException { |
ohair@286 | 254 | |
ohair@286 | 255 | this.defaultNsUri = builder.defaultNsUri; |
ohair@286 | 256 | this.retainPropertyInfo = builder.retainPropertyInfo; |
ohair@286 | 257 | this.annotationReader = builder.annotationReader; |
ohair@286 | 258 | this.subclassReplacements = builder.subclassReplacements; |
ohair@286 | 259 | this.c14nSupport = builder.c14nSupport; |
ohair@286 | 260 | this.classes = builder.classes; |
ohair@286 | 261 | this.xmlAccessorFactorySupport = builder.xmlAccessorFactorySupport; |
ohair@286 | 262 | this.allNillable = builder.allNillable; |
ohair@286 | 263 | this.supressAccessorWarnings = builder.supressAccessorWarnings; |
ohair@286 | 264 | this.improvedXsiTypeHandling = builder.improvedXsiTypeHandling; |
alanb@368 | 265 | this.disableSecurityProcessing = builder.disableSecurityProcessing; |
ohair@286 | 266 | |
ohair@286 | 267 | Collection<TypeReference> typeRefs = builder.typeRefs; |
ohair@286 | 268 | |
ohair@286 | 269 | boolean fastB; |
ohair@286 | 270 | try { |
ohair@286 | 271 | fastB = Boolean.getBoolean(JAXBContextImpl.class.getName()+".fastBoot"); |
ohair@286 | 272 | } catch (SecurityException e) { |
ohair@286 | 273 | fastB = false; |
ohair@286 | 274 | } |
ohair@286 | 275 | this.fastBoot = fastB; |
ohair@286 | 276 | |
ohair@286 | 277 | RuntimeTypeInfoSet typeSet = getTypeInfoSet(); |
ohair@286 | 278 | |
ohair@286 | 279 | // at least prepare the empty table so that we don't have to check for null later |
ohair@286 | 280 | elements.put(null,new LinkedHashMap<QName, ElementBeanInfoImpl>()); |
ohair@286 | 281 | |
ohair@286 | 282 | // recognize leaf bean infos |
ohair@286 | 283 | for( RuntimeBuiltinLeafInfo leaf : RuntimeBuiltinLeafInfoImpl.builtinBeanInfos ) { |
ohair@286 | 284 | LeafBeanInfoImpl<?> bi = new LeafBeanInfoImpl(this,leaf); |
ohair@286 | 285 | beanInfoMap.put(leaf.getClazz(),bi); |
ohair@286 | 286 | for( QName t : bi.getTypeNames() ) |
ohair@286 | 287 | typeMap.put(t,bi); |
ohair@286 | 288 | } |
ohair@286 | 289 | |
ohair@286 | 290 | for (RuntimeEnumLeafInfo e : typeSet.enums().values()) { |
ohair@286 | 291 | JaxBeanInfo<?> bi = getOrCreate(e); |
ohair@286 | 292 | for (QName qn : bi.getTypeNames()) |
ohair@286 | 293 | typeMap.put( qn, bi ); |
ohair@286 | 294 | if(e.isElement()) |
ohair@286 | 295 | rootMap.put( e.getElementName(), bi ); |
ohair@286 | 296 | } |
ohair@286 | 297 | |
ohair@286 | 298 | for (RuntimeArrayInfo a : typeSet.arrays().values()) { |
ohair@286 | 299 | JaxBeanInfo<?> ai = getOrCreate(a); |
ohair@286 | 300 | for (QName qn : ai.getTypeNames()) |
ohair@286 | 301 | typeMap.put( qn, ai ); |
ohair@286 | 302 | } |
ohair@286 | 303 | |
ohair@286 | 304 | for( Entry<Class, ? extends RuntimeClassInfo> e : typeSet.beans().entrySet() ) { |
ohair@286 | 305 | ClassBeanInfoImpl<?> bi = getOrCreate(e.getValue()); |
ohair@286 | 306 | |
ohair@286 | 307 | XmlSchema xs = this.annotationReader.getPackageAnnotation(XmlSchema.class, e.getKey(), null); |
ohair@286 | 308 | if(xs != null) { |
ohair@286 | 309 | if(xs.xmlns() != null && xs.xmlns().length > 0) { |
ohair@286 | 310 | if(xmlNsSet == null) |
ohair@286 | 311 | xmlNsSet = new HashSet<XmlNs>(); |
ohair@286 | 312 | xmlNsSet.addAll(Arrays.asList(xs.xmlns())); |
ohair@286 | 313 | } |
ohair@286 | 314 | } |
ohair@286 | 315 | |
ohair@286 | 316 | if(bi.isElement()) |
ohair@286 | 317 | rootMap.put( e.getValue().getElementName(), bi ); |
ohair@286 | 318 | |
ohair@286 | 319 | for (QName qn : bi.getTypeNames()) |
ohair@286 | 320 | typeMap.put( qn, bi ); |
ohair@286 | 321 | } |
ohair@286 | 322 | |
ohair@286 | 323 | // fill in element mappings |
ohair@286 | 324 | for( RuntimeElementInfo n : typeSet.getAllElements() ) { |
ohair@286 | 325 | ElementBeanInfoImpl bi = getOrCreate(n); |
ohair@286 | 326 | if(n.getScope()==null) |
ohair@286 | 327 | rootMap.put(n.getElementName(),bi); |
ohair@286 | 328 | |
ohair@286 | 329 | RuntimeClassInfo scope = n.getScope(); |
ohair@286 | 330 | Class scopeClazz = scope==null?null:scope.getClazz(); |
ohair@286 | 331 | Map<QName,ElementBeanInfoImpl> m = elements.get(scopeClazz); |
ohair@286 | 332 | if(m==null) { |
ohair@286 | 333 | m = new LinkedHashMap<QName, ElementBeanInfoImpl>(); |
ohair@286 | 334 | elements.put(scopeClazz,m); |
ohair@286 | 335 | } |
ohair@286 | 336 | m.put(n.getElementName(),bi); |
ohair@286 | 337 | } |
ohair@286 | 338 | |
ohair@286 | 339 | // this one is so that we can handle plain JAXBElements. |
ohair@286 | 340 | beanInfoMap.put(JAXBElement.class,new ElementBeanInfoImpl(this)); |
ohair@286 | 341 | // another special BeanInfoImpl just for marshalling |
ohair@286 | 342 | beanInfoMap.put(CompositeStructure.class,new CompositeStructureBeanInfo(this)); |
ohair@286 | 343 | |
ohair@286 | 344 | getOrCreate(typeSet.getAnyTypeInfo()); |
ohair@286 | 345 | |
ohair@286 | 346 | // then link them all! |
ohair@286 | 347 | for (JaxBeanInfo bi : beanInfos.values()) |
ohair@286 | 348 | bi.link(this); |
ohair@286 | 349 | |
ohair@286 | 350 | // register primitives for boxed types just to make GrammarInfo fool-proof |
ohair@286 | 351 | for( Map.Entry<Class,Class> e : RuntimeUtil.primitiveToBox.entrySet() ) |
ohair@286 | 352 | beanInfoMap.put( e.getKey(), beanInfoMap.get(e.getValue()) ); |
ohair@286 | 353 | |
ohair@286 | 354 | // build bridges |
mkos@450 | 355 | Navigator<Type, Class, Field, Method> nav = typeSet.getNavigator(); |
ohair@286 | 356 | |
ohair@286 | 357 | for (TypeReference tr : typeRefs) { |
ohair@286 | 358 | XmlJavaTypeAdapter xjta = tr.get(XmlJavaTypeAdapter.class); |
ohair@286 | 359 | Adapter<Type,Class> a=null; |
ohair@286 | 360 | XmlList xl = tr.get(XmlList.class); |
ohair@286 | 361 | |
ohair@286 | 362 | // eventually compute the in-memory type |
mkos@450 | 363 | Class erasedType = (Class) nav.erasure(tr.type); |
ohair@286 | 364 | |
ohair@286 | 365 | if(xjta!=null) { |
ohair@286 | 366 | a = new Adapter<Type,Class>(xjta.value(),nav); |
ohair@286 | 367 | } |
ohair@286 | 368 | if(tr.get(XmlAttachmentRef.class)!=null) { |
ohair@286 | 369 | a = new Adapter<Type,Class>(SwaRefAdapter.class,nav); |
ohair@286 | 370 | hasSwaRef = true; |
ohair@286 | 371 | } |
ohair@286 | 372 | |
ohair@286 | 373 | if(a!=null) { |
mkos@450 | 374 | erasedType = (Class) nav.erasure(a.defaultType); |
ohair@286 | 375 | } |
ohair@286 | 376 | |
ohair@286 | 377 | Name name = nameBuilder.createElementName(tr.tagName); |
ohair@286 | 378 | |
ohair@286 | 379 | InternalBridge bridge; |
ohair@286 | 380 | if(xl==null) |
ohair@286 | 381 | bridge = new BridgeImpl(this, name,getBeanInfo(erasedType,true),tr); |
ohair@286 | 382 | else |
ohair@286 | 383 | bridge = new BridgeImpl(this, name,new ValueListBeanInfoImpl(this,erasedType),tr); |
ohair@286 | 384 | |
ohair@286 | 385 | if(a!=null) |
ohair@286 | 386 | bridge = new BridgeAdapter(bridge,a.adapterType); |
ohair@286 | 387 | |
ohair@286 | 388 | bridges.put(tr,bridge); |
ohair@286 | 389 | } |
ohair@286 | 390 | |
ohair@286 | 391 | this.nameList = nameBuilder.conclude(); |
ohair@286 | 392 | |
ohair@286 | 393 | for (JaxBeanInfo bi : beanInfos.values()) |
ohair@286 | 394 | bi.wrapUp(); |
ohair@286 | 395 | |
ohair@286 | 396 | // no use for them now |
ohair@286 | 397 | nameBuilder = null; |
ohair@286 | 398 | beanInfos = null; |
ohair@286 | 399 | } |
ohair@286 | 400 | |
ohair@286 | 401 | /** |
ohair@286 | 402 | * True if this JAXBContext has {@link XmlAttachmentRef}. |
ohair@286 | 403 | */ |
ohair@286 | 404 | public boolean hasSwaRef() { |
ohair@286 | 405 | return hasSwaRef; |
ohair@286 | 406 | } |
ohair@286 | 407 | |
ohair@286 | 408 | public RuntimeTypeInfoSet getRuntimeTypeInfoSet() { |
ohair@286 | 409 | try { |
ohair@286 | 410 | return getTypeInfoSet(); |
ohair@286 | 411 | } catch (IllegalAnnotationsException e) { |
ohair@286 | 412 | // impossible, once the model is constructred |
ohair@286 | 413 | throw new AssertionError(e); |
ohair@286 | 414 | } |
ohair@286 | 415 | } |
ohair@286 | 416 | |
ohair@286 | 417 | /** |
ohair@286 | 418 | * Creates a {@link RuntimeTypeInfoSet}. |
ohair@286 | 419 | */ |
ohair@286 | 420 | public RuntimeTypeInfoSet getTypeInfoSet() throws IllegalAnnotationsException { |
ohair@286 | 421 | |
ohair@286 | 422 | // check cache |
ohair@286 | 423 | if(typeInfoSetCache!=null) { |
ohair@286 | 424 | RuntimeTypeInfoSet r = typeInfoSetCache.get(); |
ohair@286 | 425 | if(r!=null) |
ohair@286 | 426 | return r; |
ohair@286 | 427 | } |
ohair@286 | 428 | |
ohair@286 | 429 | final RuntimeModelBuilder builder = new RuntimeModelBuilder(this,annotationReader,subclassReplacements,defaultNsUri); |
ohair@286 | 430 | |
ohair@286 | 431 | IllegalAnnotationsException.Builder errorHandler = new IllegalAnnotationsException.Builder(); |
ohair@286 | 432 | builder.setErrorHandler(errorHandler); |
ohair@286 | 433 | |
ohair@286 | 434 | for( Class c : classes ) { |
ohair@286 | 435 | if(c==CompositeStructure.class) |
ohair@286 | 436 | // CompositeStructure doesn't have TypeInfo, so skip it. |
ohair@286 | 437 | // We'll add JaxBeanInfo for this later automatically |
ohair@286 | 438 | continue; |
ohair@286 | 439 | builder.getTypeInfo(new Ref<Type,Class>(c)); |
ohair@286 | 440 | } |
ohair@286 | 441 | |
ohair@286 | 442 | this.hasSwaRef |= builder.hasSwaRef; |
ohair@286 | 443 | RuntimeTypeInfoSet r = builder.link(); |
ohair@286 | 444 | |
ohair@286 | 445 | errorHandler.check(); |
ohair@286 | 446 | assert r!=null : "if no error was reported, the link must be a success"; |
ohair@286 | 447 | |
ohair@286 | 448 | typeInfoSetCache = new WeakReference<RuntimeTypeInfoSet>(r); |
ohair@286 | 449 | |
ohair@286 | 450 | return r; |
ohair@286 | 451 | } |
ohair@286 | 452 | |
ohair@286 | 453 | |
ohair@286 | 454 | public ElementBeanInfoImpl getElement(Class scope, QName name) { |
ohair@286 | 455 | Map<QName,ElementBeanInfoImpl> m = elements.get(scope); |
ohair@286 | 456 | if(m!=null) { |
ohair@286 | 457 | ElementBeanInfoImpl bi = m.get(name); |
ohair@286 | 458 | if(bi!=null) |
ohair@286 | 459 | return bi; |
ohair@286 | 460 | } |
ohair@286 | 461 | m = elements.get(null); |
ohair@286 | 462 | return m.get(name); |
ohair@286 | 463 | } |
ohair@286 | 464 | |
ohair@286 | 465 | |
ohair@286 | 466 | |
ohair@286 | 467 | |
ohair@286 | 468 | |
ohair@286 | 469 | private ElementBeanInfoImpl getOrCreate( RuntimeElementInfo rei ) { |
ohair@286 | 470 | JaxBeanInfo bi = beanInfos.get(rei); |
ohair@286 | 471 | if(bi!=null) return (ElementBeanInfoImpl)bi; |
ohair@286 | 472 | |
ohair@286 | 473 | // all elements share the same type, so we can't register them to beanInfoMap |
ohair@286 | 474 | return new ElementBeanInfoImpl(this, rei); |
ohair@286 | 475 | } |
ohair@286 | 476 | |
ohair@286 | 477 | protected JaxBeanInfo getOrCreate( RuntimeEnumLeafInfo eli ) { |
ohair@286 | 478 | JaxBeanInfo bi = beanInfos.get(eli); |
ohair@286 | 479 | if(bi!=null) return bi; |
ohair@286 | 480 | bi = new LeafBeanInfoImpl(this,eli); |
ohair@286 | 481 | beanInfoMap.put(bi.jaxbType,bi); |
ohair@286 | 482 | return bi; |
ohair@286 | 483 | } |
ohair@286 | 484 | |
ohair@286 | 485 | protected ClassBeanInfoImpl getOrCreate( RuntimeClassInfo ci ) { |
ohair@286 | 486 | ClassBeanInfoImpl bi = (ClassBeanInfoImpl)beanInfos.get(ci); |
ohair@286 | 487 | if(bi!=null) return bi; |
ohair@286 | 488 | bi = new ClassBeanInfoImpl(this,ci); |
ohair@286 | 489 | beanInfoMap.put(bi.jaxbType,bi); |
ohair@286 | 490 | return bi; |
ohair@286 | 491 | } |
ohair@286 | 492 | |
ohair@286 | 493 | protected JaxBeanInfo getOrCreate( RuntimeArrayInfo ai ) { |
ohair@286 | 494 | JaxBeanInfo abi = beanInfos.get(ai); |
ohair@286 | 495 | if(abi!=null) return abi; |
ohair@286 | 496 | |
ohair@286 | 497 | abi = new ArrayBeanInfoImpl(this,ai); |
ohair@286 | 498 | |
ohair@286 | 499 | beanInfoMap.put(ai.getType(),abi); |
ohair@286 | 500 | return abi; |
ohair@286 | 501 | } |
ohair@286 | 502 | |
ohair@286 | 503 | public JaxBeanInfo getOrCreate(RuntimeTypeInfo e) { |
ohair@286 | 504 | if(e instanceof RuntimeElementInfo) |
ohair@286 | 505 | return getOrCreate((RuntimeElementInfo)e); |
ohair@286 | 506 | if(e instanceof RuntimeClassInfo) |
ohair@286 | 507 | return getOrCreate((RuntimeClassInfo)e); |
ohair@286 | 508 | if(e instanceof RuntimeLeafInfo) { |
ohair@286 | 509 | JaxBeanInfo bi = beanInfos.get(e); // must have been created |
ohair@286 | 510 | assert bi!=null; |
ohair@286 | 511 | return bi; |
ohair@286 | 512 | } |
ohair@286 | 513 | if(e instanceof RuntimeArrayInfo) |
ohair@286 | 514 | return getOrCreate((RuntimeArrayInfo)e); |
ohair@286 | 515 | if(e.getType()==Object.class) { |
ohair@286 | 516 | // anyType |
ohair@286 | 517 | JaxBeanInfo bi = beanInfoMap.get(Object.class); |
ohair@286 | 518 | if(bi==null) { |
ohair@286 | 519 | bi = new AnyTypeBeanInfo(this,e); |
ohair@286 | 520 | beanInfoMap.put(Object.class,bi); |
ohair@286 | 521 | } |
ohair@286 | 522 | return bi; |
ohair@286 | 523 | } |
ohair@286 | 524 | |
ohair@286 | 525 | throw new IllegalArgumentException(); |
ohair@286 | 526 | } |
ohair@286 | 527 | |
ohair@286 | 528 | /** |
ohair@286 | 529 | * Gets the {@link JaxBeanInfo} object that can handle |
ohair@286 | 530 | * the given JAXB-bound object. |
ohair@286 | 531 | * |
ohair@286 | 532 | * <p> |
ohair@286 | 533 | * This method traverses the base classes of the given object. |
ohair@286 | 534 | * |
ohair@286 | 535 | * @return null |
ohair@286 | 536 | * if <tt>c</tt> isn't a JAXB-bound class and <tt>fatal==false</tt>. |
ohair@286 | 537 | */ |
ohair@286 | 538 | public final JaxBeanInfo getBeanInfo(Object o) { |
ohair@286 | 539 | // don't allow xs:anyType beanInfo to handle all the unbound objects |
ohair@286 | 540 | for( Class c=o.getClass(); c!=Object.class; c=c.getSuperclass()) { |
ohair@286 | 541 | JaxBeanInfo bi = beanInfoMap.get(c); |
ohair@286 | 542 | if(bi!=null) return bi; |
ohair@286 | 543 | } |
ohair@286 | 544 | if(o instanceof Element) |
ohair@286 | 545 | return beanInfoMap.get(Object.class); // return the BeanInfo for xs:anyType |
ohair@286 | 546 | for( Class c : o.getClass().getInterfaces()) { |
ohair@286 | 547 | JaxBeanInfo bi = beanInfoMap.get(c); |
ohair@286 | 548 | if(bi!=null) return bi; |
ohair@286 | 549 | } |
ohair@286 | 550 | return null; |
ohair@286 | 551 | } |
ohair@286 | 552 | |
ohair@286 | 553 | /** |
ohair@286 | 554 | * Gets the {@link JaxBeanInfo} object that can handle |
ohair@286 | 555 | * the given JAXB-bound object. |
ohair@286 | 556 | * |
ohair@286 | 557 | * @param fatal |
ohair@286 | 558 | * if true, the failure to look up will throw an exception. |
ohair@286 | 559 | * Otherwise it will just return null. |
ohair@286 | 560 | */ |
ohair@286 | 561 | public final JaxBeanInfo getBeanInfo(Object o,boolean fatal) throws JAXBException { |
ohair@286 | 562 | JaxBeanInfo bi = getBeanInfo(o); |
ohair@286 | 563 | if(bi!=null) return bi; |
ohair@286 | 564 | if(fatal) { |
ohair@286 | 565 | if(o instanceof Document) |
ohair@286 | 566 | throw new JAXBException(Messages.ELEMENT_NEEDED_BUT_FOUND_DOCUMENT.format(o.getClass())); |
ohair@286 | 567 | throw new JAXBException(Messages.UNKNOWN_CLASS.format(o.getClass())); |
ohair@286 | 568 | } |
ohair@286 | 569 | return null; |
ohair@286 | 570 | } |
ohair@286 | 571 | |
ohair@286 | 572 | /** |
ohair@286 | 573 | * Gets the {@link JaxBeanInfo} object that can handle |
ohair@286 | 574 | * the given JAXB-bound class. |
ohair@286 | 575 | * |
ohair@286 | 576 | * <p> |
ohair@286 | 577 | * This method doesn't look for base classes. |
ohair@286 | 578 | * |
ohair@286 | 579 | * @return null |
ohair@286 | 580 | * if <tt>c</tt> isn't a JAXB-bound class and <tt>fatal==false</tt>. |
ohair@286 | 581 | */ |
ohair@286 | 582 | public final <T> JaxBeanInfo<T> getBeanInfo(Class<T> clazz) { |
ohair@286 | 583 | return (JaxBeanInfo<T>)beanInfoMap.get(clazz); |
ohair@286 | 584 | } |
ohair@286 | 585 | |
ohair@286 | 586 | /** |
ohair@286 | 587 | * Gets the {@link JaxBeanInfo} object that can handle |
ohair@286 | 588 | * the given JAXB-bound class. |
ohair@286 | 589 | * |
ohair@286 | 590 | * @param fatal |
ohair@286 | 591 | * if true, the failure to look up will throw an exception. |
ohair@286 | 592 | * Otherwise it will just return null. |
ohair@286 | 593 | */ |
ohair@286 | 594 | public final <T> JaxBeanInfo<T> getBeanInfo(Class<T> clazz,boolean fatal) throws JAXBException { |
ohair@286 | 595 | JaxBeanInfo<T> bi = getBeanInfo(clazz); |
ohair@286 | 596 | if(bi!=null) return bi; |
ohair@286 | 597 | if(fatal) |
ohair@286 | 598 | throw new JAXBException(clazz.getName()+" is not known to this context"); |
ohair@286 | 599 | return null; |
ohair@286 | 600 | } |
ohair@286 | 601 | |
ohair@286 | 602 | /** |
ohair@286 | 603 | * Based on the tag name, determine what object to unmarshal, |
ohair@286 | 604 | * and then set a new object and its loader to the current unmarshaller state. |
ohair@286 | 605 | * |
ohair@286 | 606 | * @return |
ohair@286 | 607 | * null if the given name pair is not recognized. |
ohair@286 | 608 | */ |
ohair@286 | 609 | public final Loader selectRootLoader( UnmarshallingContext.State state, TagName tag ) { |
ohair@286 | 610 | JaxBeanInfo beanInfo = rootMap.get(tag.uri,tag.local); |
ohair@286 | 611 | if(beanInfo==null) |
ohair@286 | 612 | return null; |
ohair@286 | 613 | |
ohair@286 | 614 | return beanInfo.getLoader(this,true); |
ohair@286 | 615 | } |
ohair@286 | 616 | |
ohair@286 | 617 | /** |
ohair@286 | 618 | * Gets the {@link JaxBeanInfo} for the given named XML Schema type. |
ohair@286 | 619 | * |
ohair@286 | 620 | * @return |
ohair@286 | 621 | * null if the type name is not recognized. For schema |
ohair@286 | 622 | * languages other than XML Schema, this method always |
ohair@286 | 623 | * returns null. |
ohair@286 | 624 | */ |
ohair@286 | 625 | public JaxBeanInfo getGlobalType(QName name) { |
ohair@286 | 626 | return typeMap.get(name); |
ohair@286 | 627 | } |
ohair@286 | 628 | |
ohair@286 | 629 | /** |
ohair@286 | 630 | * Finds a type name that this context recognizes which is |
ohair@286 | 631 | * "closest" to the given type name. |
ohair@286 | 632 | * |
ohair@286 | 633 | * <p> |
ohair@286 | 634 | * This method is used for error recovery. |
ohair@286 | 635 | */ |
ohair@286 | 636 | public String getNearestTypeName(QName name) { |
ohair@286 | 637 | String[] all = new String[typeMap.size()]; |
ohair@286 | 638 | int i=0; |
ohair@286 | 639 | for (QName qn : typeMap.keySet()) { |
ohair@286 | 640 | if(qn.getLocalPart().equals(name.getLocalPart())) |
ohair@286 | 641 | return qn.toString(); // probably a match, as people often gets confused about namespace. |
ohair@286 | 642 | all[i++] = qn.toString(); |
ohair@286 | 643 | } |
ohair@286 | 644 | |
ohair@286 | 645 | String nearest = EditDistance.findNearest(name.toString(), all); |
ohair@286 | 646 | |
ohair@286 | 647 | if(EditDistance.editDistance(nearest,name.toString())>10) |
ohair@286 | 648 | return null; // too far apart. |
ohair@286 | 649 | |
ohair@286 | 650 | return nearest; |
ohair@286 | 651 | } |
ohair@286 | 652 | |
ohair@286 | 653 | /** |
ohair@286 | 654 | * Returns the set of valid root tag names. |
ohair@286 | 655 | * For diagnostic use. |
ohair@286 | 656 | */ |
ohair@286 | 657 | public Set<QName> getValidRootNames() { |
ohair@286 | 658 | Set<QName> r = new TreeSet<QName>(QNAME_COMPARATOR); |
ohair@286 | 659 | for (QNameMap.Entry e : rootMap.entrySet()) { |
ohair@286 | 660 | r.add(e.createQName()); |
ohair@286 | 661 | } |
ohair@286 | 662 | return r; |
ohair@286 | 663 | } |
ohair@286 | 664 | |
ohair@286 | 665 | /** |
ohair@286 | 666 | * Cache of UTF-8 encoded local names to improve the performance for the marshalling. |
ohair@286 | 667 | */ |
ohair@286 | 668 | private Encoded[] utf8nameTable; |
ohair@286 | 669 | |
ohair@286 | 670 | public synchronized Encoded[] getUTF8NameTable() { |
ohair@286 | 671 | if(utf8nameTable==null) { |
ohair@286 | 672 | Encoded[] x = new Encoded[nameList.localNames.length]; |
ohair@286 | 673 | for( int i=0; i<x.length; i++ ) { |
ohair@286 | 674 | Encoded e = new Encoded(nameList.localNames[i]); |
ohair@286 | 675 | e.compact(); |
ohair@286 | 676 | x[i] = e; |
ohair@286 | 677 | } |
ohair@286 | 678 | utf8nameTable = x; |
ohair@286 | 679 | } |
ohair@286 | 680 | return utf8nameTable; |
ohair@286 | 681 | } |
ohair@286 | 682 | |
ohair@286 | 683 | public int getNumberOfLocalNames() { |
ohair@286 | 684 | return nameList.localNames.length; |
ohair@286 | 685 | } |
ohair@286 | 686 | |
ohair@286 | 687 | public int getNumberOfElementNames() { |
ohair@286 | 688 | return nameList.numberOfElementNames; |
ohair@286 | 689 | } |
ohair@286 | 690 | |
ohair@286 | 691 | public int getNumberOfAttributeNames() { |
ohair@286 | 692 | return nameList.numberOfAttributeNames; |
ohair@286 | 693 | } |
ohair@286 | 694 | |
ohair@286 | 695 | /** |
ohair@286 | 696 | * Creates a new identity transformer. |
ohair@286 | 697 | */ |
alanb@368 | 698 | static Transformer createTransformer(boolean disableSecureProcessing) { |
ohair@286 | 699 | try { |
mkos@515 | 700 | SAXTransformerFactory tf = (SAXTransformerFactory)XmlFactory.createTransformerFactory(disableSecureProcessing); |
ohair@286 | 701 | return tf.newTransformer(); |
ohair@286 | 702 | } catch (TransformerConfigurationException e) { |
ohair@286 | 703 | throw new Error(e); // impossible |
ohair@286 | 704 | } |
ohair@286 | 705 | } |
ohair@286 | 706 | |
ohair@286 | 707 | /** |
ohair@286 | 708 | * Creates a new identity transformer. |
ohair@286 | 709 | */ |
alanb@368 | 710 | public static TransformerHandler createTransformerHandler(boolean disableSecureProcessing) { |
ohair@286 | 711 | try { |
mkos@515 | 712 | SAXTransformerFactory tf = (SAXTransformerFactory)XmlFactory.createTransformerFactory(disableSecureProcessing); |
ohair@286 | 713 | return tf.newTransformerHandler(); |
ohair@286 | 714 | } catch (TransformerConfigurationException e) { |
ohair@286 | 715 | throw new Error(e); // impossible |
ohair@286 | 716 | } |
ohair@286 | 717 | } |
ohair@286 | 718 | |
ohair@286 | 719 | /** |
ohair@286 | 720 | * Creates a new DOM document. |
ohair@286 | 721 | */ |
alanb@368 | 722 | static Document createDom(boolean disableSecurityProcessing) { |
ohair@286 | 723 | synchronized(JAXBContextImpl.class) { |
ohair@286 | 724 | if(db==null) { |
ohair@286 | 725 | try { |
alanb@368 | 726 | DocumentBuilderFactory dbf = XmlFactory.createDocumentBuilderFactory(disableSecurityProcessing); |
ohair@286 | 727 | db = dbf.newDocumentBuilder(); |
ohair@286 | 728 | } catch (ParserConfigurationException e) { |
ohair@286 | 729 | // impossible |
ohair@286 | 730 | throw new FactoryConfigurationError(e); |
ohair@286 | 731 | } |
ohair@286 | 732 | } |
ohair@286 | 733 | return db.newDocument(); |
ohair@286 | 734 | } |
ohair@286 | 735 | } |
ohair@286 | 736 | |
ohair@286 | 737 | public MarshallerImpl createMarshaller() { |
ohair@286 | 738 | return new MarshallerImpl(this,null); |
ohair@286 | 739 | } |
ohair@286 | 740 | |
ohair@286 | 741 | public UnmarshallerImpl createUnmarshaller() { |
ohair@286 | 742 | return new UnmarshallerImpl(this,null); |
ohair@286 | 743 | } |
ohair@286 | 744 | |
ohair@286 | 745 | public Validator createValidator() { |
ohair@286 | 746 | throw new UnsupportedOperationException(Messages.NOT_IMPLEMENTED_IN_2_0.format()); |
ohair@286 | 747 | } |
ohair@286 | 748 | |
ohair@286 | 749 | @Override |
ohair@286 | 750 | public JAXBIntrospector createJAXBIntrospector() { |
ohair@286 | 751 | return new JAXBIntrospector() { |
ohair@286 | 752 | public boolean isElement(Object object) { |
ohair@286 | 753 | return getElementName(object)!=null; |
ohair@286 | 754 | } |
ohair@286 | 755 | |
ohair@286 | 756 | public QName getElementName(Object jaxbElement) { |
ohair@286 | 757 | try { |
ohair@286 | 758 | return JAXBContextImpl.this.getElementName(jaxbElement); |
ohair@286 | 759 | } catch (JAXBException e) { |
ohair@286 | 760 | return null; |
ohair@286 | 761 | } |
ohair@286 | 762 | } |
ohair@286 | 763 | }; |
ohair@286 | 764 | } |
ohair@286 | 765 | |
ohair@286 | 766 | private NonElement<Type,Class> getXmlType(RuntimeTypeInfoSet tis, TypeReference tr) { |
ohair@286 | 767 | if(tr==null) |
ohair@286 | 768 | throw new IllegalArgumentException(); |
ohair@286 | 769 | |
ohair@286 | 770 | XmlJavaTypeAdapter xjta = tr.get(XmlJavaTypeAdapter.class); |
ohair@286 | 771 | XmlList xl = tr.get(XmlList.class); |
ohair@286 | 772 | |
ohair@286 | 773 | Ref<Type,Class> ref = new Ref<Type,Class>(annotationReader, tis.getNavigator(), tr.type, xjta, xl ); |
ohair@286 | 774 | |
ohair@286 | 775 | return tis.getTypeInfo(ref); |
ohair@286 | 776 | } |
ohair@286 | 777 | |
ohair@286 | 778 | @Override |
ohair@286 | 779 | public void generateEpisode(Result output) { |
ohair@286 | 780 | if(output==null) |
ohair@286 | 781 | throw new IllegalArgumentException(); |
ohair@286 | 782 | createSchemaGenerator().writeEpisodeFile(ResultFactory.createSerializer(output)); |
ohair@286 | 783 | } |
ohair@286 | 784 | |
ohair@286 | 785 | @Override |
ohair@286 | 786 | @SuppressWarnings("ThrowableInitCause") |
ohair@286 | 787 | public void generateSchema(SchemaOutputResolver outputResolver) throws IOException { |
ohair@286 | 788 | if(outputResolver==null) |
ohair@286 | 789 | throw new IOException(Messages.NULL_OUTPUT_RESOLVER.format()); |
ohair@286 | 790 | |
ohair@286 | 791 | final SAXParseException[] e = new SAXParseException[1]; |
ohair@286 | 792 | final SAXParseException[] w = new SAXParseException[1]; |
ohair@286 | 793 | |
ohair@286 | 794 | createSchemaGenerator().write(outputResolver, new ErrorListener() { |
ohair@286 | 795 | public void error(SAXParseException exception) { |
ohair@286 | 796 | e[0] = exception; |
ohair@286 | 797 | } |
ohair@286 | 798 | |
ohair@286 | 799 | public void fatalError(SAXParseException exception) { |
ohair@286 | 800 | e[0] = exception; |
ohair@286 | 801 | } |
ohair@286 | 802 | |
ohair@286 | 803 | public void warning(SAXParseException exception) { |
ohair@286 | 804 | w[0] = exception; |
ohair@286 | 805 | } |
ohair@286 | 806 | |
ohair@286 | 807 | public void info(SAXParseException exception) {} |
ohair@286 | 808 | }); |
ohair@286 | 809 | |
ohair@286 | 810 | if (e[0]!=null) { |
ohair@286 | 811 | IOException x = new IOException(Messages.FAILED_TO_GENERATE_SCHEMA.format()); |
ohair@286 | 812 | x.initCause(e[0]); |
ohair@286 | 813 | throw x; |
ohair@286 | 814 | } |
ohair@286 | 815 | if (w[0]!=null) { |
ohair@286 | 816 | IOException x = new IOException(Messages.ERROR_PROCESSING_SCHEMA.format()); |
ohair@286 | 817 | x.initCause(w[0]); |
ohair@286 | 818 | throw x; |
ohair@286 | 819 | } |
ohair@286 | 820 | } |
ohair@286 | 821 | |
ohair@286 | 822 | private XmlSchemaGenerator<Type,Class,Field,Method> createSchemaGenerator() { |
ohair@286 | 823 | RuntimeTypeInfoSet tis; |
ohair@286 | 824 | try { |
ohair@286 | 825 | tis = getTypeInfoSet(); |
ohair@286 | 826 | } catch (IllegalAnnotationsException e) { |
ohair@286 | 827 | // this shouldn't happen because we've already |
ohair@286 | 828 | throw new AssertionError(e); |
ohair@286 | 829 | } |
ohair@286 | 830 | |
ohair@286 | 831 | XmlSchemaGenerator<Type,Class,Field,Method> xsdgen = |
ohair@286 | 832 | new XmlSchemaGenerator<Type,Class,Field,Method>(tis.getNavigator(),tis); |
ohair@286 | 833 | |
ohair@286 | 834 | // JAX-RPC uses Bridge objects that collide with |
ohair@286 | 835 | // @XmlRootElement. |
ohair@286 | 836 | // we will avoid collision here |
ohair@286 | 837 | Set<QName> rootTagNames = new HashSet<QName>(); |
ohair@286 | 838 | for (RuntimeElementInfo ei : tis.getAllElements()) { |
ohair@286 | 839 | rootTagNames.add(ei.getElementName()); |
ohair@286 | 840 | } |
ohair@286 | 841 | for (RuntimeClassInfo ci : tis.beans().values()) { |
ohair@286 | 842 | if(ci.isElement()) |
ohair@286 | 843 | rootTagNames.add(ci.asElement().getElementName()); |
ohair@286 | 844 | } |
ohair@286 | 845 | |
ohair@286 | 846 | for (TypeReference tr : bridges.keySet()) { |
ohair@286 | 847 | if(rootTagNames.contains(tr.tagName)) |
ohair@286 | 848 | continue; |
ohair@286 | 849 | |
ohair@286 | 850 | if(tr.type==void.class || tr.type==Void.class) { |
ohair@286 | 851 | xsdgen.add(tr.tagName,false,null); |
ohair@286 | 852 | } else |
ohair@286 | 853 | if(tr.type==CompositeStructure.class) { |
ohair@286 | 854 | // this is a special class we introduced for JAX-WS that we *don't* want in the schema |
ohair@286 | 855 | } else { |
ohair@286 | 856 | NonElement<Type,Class> typeInfo = getXmlType(tis,tr); |
mkos@450 | 857 | xsdgen.add(tr.tagName, !tis.getNavigator().isPrimitive(tr.type),typeInfo); |
ohair@286 | 858 | } |
ohair@286 | 859 | } |
ohair@286 | 860 | return xsdgen; |
ohair@286 | 861 | } |
ohair@286 | 862 | |
ohair@286 | 863 | public QName getTypeName(TypeReference tr) { |
ohair@286 | 864 | try { |
ohair@286 | 865 | NonElement<Type,Class> xt = getXmlType(getTypeInfoSet(),tr); |
ohair@286 | 866 | if(xt==null) throw new IllegalArgumentException(); |
ohair@286 | 867 | return xt.getTypeName(); |
ohair@286 | 868 | } catch (IllegalAnnotationsException e) { |
ohair@286 | 869 | // impossible given that JAXBRIContext has been successfully built in the first place |
ohair@286 | 870 | throw new AssertionError(e); |
ohair@286 | 871 | } |
ohair@286 | 872 | } |
ohair@286 | 873 | |
ohair@286 | 874 | @Override |
ohair@286 | 875 | public <T> Binder<T> createBinder(Class<T> domType) { |
ohair@286 | 876 | if(domType==Node.class) |
ohair@286 | 877 | return (Binder<T>)createBinder(); |
ohair@286 | 878 | else |
ohair@286 | 879 | return super.createBinder(domType); |
ohair@286 | 880 | } |
ohair@286 | 881 | |
ohair@286 | 882 | @Override |
ohair@286 | 883 | public Binder<Node> createBinder() { |
ohair@286 | 884 | return new BinderImpl<Node>(this,new DOMScanner()); |
ohair@286 | 885 | } |
ohair@286 | 886 | |
ohair@286 | 887 | public QName getElementName(Object o) throws JAXBException { |
ohair@286 | 888 | JaxBeanInfo bi = getBeanInfo(o,true); |
ohair@286 | 889 | if(!bi.isElement()) |
ohair@286 | 890 | return null; |
ohair@286 | 891 | return new QName(bi.getElementNamespaceURI(o),bi.getElementLocalName(o)); |
ohair@286 | 892 | } |
ohair@286 | 893 | |
ohair@286 | 894 | public QName getElementName(Class o) throws JAXBException { |
ohair@286 | 895 | JaxBeanInfo bi = getBeanInfo(o,true); |
ohair@286 | 896 | if(!bi.isElement()) |
ohair@286 | 897 | return null; |
ohair@286 | 898 | return new QName(bi.getElementNamespaceURI(o),bi.getElementLocalName(o)); |
ohair@286 | 899 | } |
ohair@286 | 900 | |
ohair@286 | 901 | public Bridge createBridge(TypeReference ref) { |
ohair@286 | 902 | return bridges.get(ref); |
ohair@286 | 903 | } |
ohair@286 | 904 | |
ohair@286 | 905 | public @NotNull BridgeContext createBridgeContext() { |
ohair@286 | 906 | return new BridgeContextImpl(this); |
ohair@286 | 907 | } |
ohair@286 | 908 | |
ohair@286 | 909 | public RawAccessor getElementPropertyAccessor(Class wrapperBean, String nsUri, String localName) throws JAXBException { |
ohair@286 | 910 | JaxBeanInfo bi = getBeanInfo(wrapperBean,true); |
ohair@286 | 911 | if(!(bi instanceof ClassBeanInfoImpl)) |
ohair@286 | 912 | throw new JAXBException(wrapperBean+" is not a bean"); |
ohair@286 | 913 | |
ohair@286 | 914 | for( ClassBeanInfoImpl cb = (ClassBeanInfoImpl) bi; cb!=null; cb=cb.superClazz) { |
ohair@286 | 915 | for (Property p : cb.properties) { |
ohair@286 | 916 | final Accessor acc = p.getElementPropertyAccessor(nsUri,localName); |
ohair@286 | 917 | if(acc!=null) |
ohair@286 | 918 | return new RawAccessor() { |
ohair@286 | 919 | // Accessor.set/get are designed for unmarshaller/marshaller, and hence |
ohair@286 | 920 | // they go through an adapter behind the scene. |
ohair@286 | 921 | // this isn't desirable for JAX-WS, which essentially uses this method |
ohair@286 | 922 | // just as a reflection library. So use the "unadapted" version to |
ohair@286 | 923 | // achieve the desired semantics |
ohair@286 | 924 | public Object get(Object bean) throws AccessorException { |
ohair@286 | 925 | return acc.getUnadapted(bean); |
ohair@286 | 926 | } |
ohair@286 | 927 | |
ohair@286 | 928 | public void set(Object bean, Object value) throws AccessorException { |
ohair@286 | 929 | acc.setUnadapted(bean,value); |
ohair@286 | 930 | } |
ohair@286 | 931 | }; |
ohair@286 | 932 | } |
ohair@286 | 933 | } |
ohair@286 | 934 | throw new JAXBException(new QName(nsUri,localName)+" is not a valid property on "+wrapperBean); |
ohair@286 | 935 | } |
ohair@286 | 936 | |
ohair@286 | 937 | public List<String> getKnownNamespaceURIs() { |
ohair@286 | 938 | return Arrays.asList(nameList.namespaceURIs); |
ohair@286 | 939 | } |
ohair@286 | 940 | |
ohair@286 | 941 | public String getBuildId() { |
ohair@286 | 942 | Package pkg = getClass().getPackage(); |
ohair@286 | 943 | if(pkg==null) return null; |
ohair@286 | 944 | return pkg.getImplementationVersion(); |
ohair@286 | 945 | } |
ohair@286 | 946 | |
ohair@286 | 947 | @Override |
ohair@286 | 948 | public String toString() { |
ohair@286 | 949 | StringBuilder buf = new StringBuilder(Which.which(getClass()) + " Build-Id: " + getBuildId()); |
ohair@286 | 950 | buf.append("\nClasses known to this context:\n"); |
ohair@286 | 951 | |
ohair@286 | 952 | Set<String> names = new TreeSet<String>(); // sort them so that it's easy to read |
ohair@286 | 953 | |
ohair@286 | 954 | for (Class key : beanInfoMap.keySet()) |
ohair@286 | 955 | names.add(key.getName()); |
ohair@286 | 956 | |
ohair@286 | 957 | for(String name: names) |
ohair@286 | 958 | buf.append(" ").append(name).append('\n'); |
ohair@286 | 959 | |
ohair@286 | 960 | return buf.toString(); |
ohair@286 | 961 | } |
ohair@286 | 962 | |
ohair@286 | 963 | /** |
ohair@286 | 964 | * Gets the value of the xmime:contentType attribute on the given object, or null |
ohair@286 | 965 | * if for some reason it couldn't be found, including any error. |
ohair@286 | 966 | */ |
ohair@286 | 967 | public String getXMIMEContentType( Object o ) { |
ohair@286 | 968 | JaxBeanInfo bi = getBeanInfo(o); |
ohair@286 | 969 | if(!(bi instanceof ClassBeanInfoImpl)) |
ohair@286 | 970 | return null; |
ohair@286 | 971 | |
ohair@286 | 972 | ClassBeanInfoImpl cb = (ClassBeanInfoImpl) bi; |
ohair@286 | 973 | for (Property p : cb.properties) { |
ohair@286 | 974 | if (p instanceof AttributeProperty) { |
ohair@286 | 975 | AttributeProperty ap = (AttributeProperty) p; |
ohair@286 | 976 | if(ap.attName.equals(WellKnownNamespace.XML_MIME_URI,"contentType")) |
ohair@286 | 977 | try { |
ohair@286 | 978 | return (String)ap.xacc.print(o); |
ohair@286 | 979 | } catch (AccessorException e) { |
ohair@286 | 980 | return null; |
ohair@286 | 981 | } catch (SAXException e) { |
ohair@286 | 982 | return null; |
ohair@286 | 983 | } catch (ClassCastException e) { |
ohair@286 | 984 | return null; |
ohair@286 | 985 | } |
ohair@286 | 986 | } |
ohair@286 | 987 | } |
ohair@286 | 988 | return null; |
ohair@286 | 989 | } |
ohair@286 | 990 | |
ohair@286 | 991 | /** |
ohair@286 | 992 | * Creates a {@link JAXBContextImpl} that includes the specified additional classes. |
ohair@286 | 993 | */ |
ohair@286 | 994 | public JAXBContextImpl createAugmented(Class<?> clazz) throws JAXBException { |
ohair@286 | 995 | Class[] newList = new Class[classes.length+1]; |
ohair@286 | 996 | System.arraycopy(classes,0,newList,0,classes.length); |
ohair@286 | 997 | newList[classes.length] = clazz; |
ohair@286 | 998 | |
ohair@286 | 999 | JAXBContextBuilder builder = new JAXBContextBuilder(this); |
ohair@286 | 1000 | builder.setClasses(newList); |
ohair@286 | 1001 | return builder.build(); |
ohair@286 | 1002 | } |
ohair@286 | 1003 | |
ohair@286 | 1004 | private static final Comparator<QName> QNAME_COMPARATOR = new Comparator<QName>() { |
ohair@286 | 1005 | public int compare(QName lhs, QName rhs) { |
ohair@286 | 1006 | int r = lhs.getLocalPart().compareTo(rhs.getLocalPart()); |
ohair@286 | 1007 | if(r!=0) return r; |
ohair@286 | 1008 | |
ohair@286 | 1009 | return lhs.getNamespaceURI().compareTo(rhs.getNamespaceURI()); |
ohair@286 | 1010 | } |
ohair@286 | 1011 | }; |
ohair@286 | 1012 | |
ohair@286 | 1013 | public static class JAXBContextBuilder { |
ohair@286 | 1014 | |
ohair@286 | 1015 | private boolean retainPropertyInfo = false; |
ohair@286 | 1016 | private boolean supressAccessorWarnings = false; |
ohair@286 | 1017 | private String defaultNsUri = ""; |
ohair@286 | 1018 | private @NotNull RuntimeAnnotationReader annotationReader = new RuntimeInlineAnnotationReader(); |
ohair@286 | 1019 | private @NotNull Map<Class,Class> subclassReplacements = Collections.emptyMap(); |
ohair@286 | 1020 | private boolean c14nSupport = false; |
ohair@286 | 1021 | private Class[] classes; |
ohair@286 | 1022 | private Collection<TypeReference> typeRefs; |
ohair@286 | 1023 | private boolean xmlAccessorFactorySupport = false; |
ohair@286 | 1024 | private boolean allNillable; |
ohair@286 | 1025 | private boolean improvedXsiTypeHandling = true; |
alanb@368 | 1026 | private boolean disableSecurityProcessing = true; |
ohair@286 | 1027 | |
ohair@286 | 1028 | public JAXBContextBuilder() {}; |
ohair@286 | 1029 | |
ohair@286 | 1030 | public JAXBContextBuilder(JAXBContextImpl baseImpl) { |
ohair@286 | 1031 | this.supressAccessorWarnings = baseImpl.supressAccessorWarnings; |
ohair@286 | 1032 | this.retainPropertyInfo = baseImpl.retainPropertyInfo; |
ohair@286 | 1033 | this.defaultNsUri = baseImpl.defaultNsUri; |
ohair@286 | 1034 | this.annotationReader = baseImpl.annotationReader; |
ohair@286 | 1035 | this.subclassReplacements = baseImpl.subclassReplacements; |
ohair@286 | 1036 | this.c14nSupport = baseImpl.c14nSupport; |
ohair@286 | 1037 | this.classes = baseImpl.classes; |
ohair@286 | 1038 | this.typeRefs = baseImpl.bridges.keySet(); |
ohair@286 | 1039 | this.xmlAccessorFactorySupport = baseImpl.xmlAccessorFactorySupport; |
ohair@286 | 1040 | this.allNillable = baseImpl.allNillable; |
alanb@368 | 1041 | this.disableSecurityProcessing = baseImpl.disableSecurityProcessing; |
ohair@286 | 1042 | } |
ohair@286 | 1043 | |
ohair@286 | 1044 | public JAXBContextBuilder setRetainPropertyInfo(boolean val) { |
ohair@286 | 1045 | this.retainPropertyInfo = val; |
ohair@286 | 1046 | return this; |
ohair@286 | 1047 | } |
ohair@286 | 1048 | |
ohair@286 | 1049 | public JAXBContextBuilder setSupressAccessorWarnings(boolean val) { |
ohair@286 | 1050 | this.supressAccessorWarnings = val; |
ohair@286 | 1051 | return this; |
ohair@286 | 1052 | } |
ohair@286 | 1053 | |
ohair@286 | 1054 | public JAXBContextBuilder setC14NSupport(boolean val) { |
ohair@286 | 1055 | this.c14nSupport = val; |
ohair@286 | 1056 | return this; |
ohair@286 | 1057 | } |
ohair@286 | 1058 | |
ohair@286 | 1059 | public JAXBContextBuilder setXmlAccessorFactorySupport(boolean val) { |
ohair@286 | 1060 | this.xmlAccessorFactorySupport = val; |
ohair@286 | 1061 | return this; |
ohair@286 | 1062 | } |
ohair@286 | 1063 | |
ohair@286 | 1064 | public JAXBContextBuilder setDefaultNsUri(String val) { |
ohair@286 | 1065 | this.defaultNsUri = val; |
ohair@286 | 1066 | return this; |
ohair@286 | 1067 | } |
ohair@286 | 1068 | |
ohair@286 | 1069 | public JAXBContextBuilder setAllNillable(boolean val) { |
ohair@286 | 1070 | this.allNillable = val; |
ohair@286 | 1071 | return this; |
ohair@286 | 1072 | } |
ohair@286 | 1073 | |
ohair@286 | 1074 | public JAXBContextBuilder setClasses(Class[] val) { |
ohair@286 | 1075 | this.classes = val; |
ohair@286 | 1076 | return this; |
ohair@286 | 1077 | } |
ohair@286 | 1078 | |
ohair@286 | 1079 | public JAXBContextBuilder setAnnotationReader(RuntimeAnnotationReader val) { |
ohair@286 | 1080 | this.annotationReader = val; |
ohair@286 | 1081 | return this; |
ohair@286 | 1082 | } |
ohair@286 | 1083 | |
ohair@286 | 1084 | public JAXBContextBuilder setSubclassReplacements(Map<Class,Class> val) { |
ohair@286 | 1085 | this.subclassReplacements = val; |
ohair@286 | 1086 | return this; |
ohair@286 | 1087 | } |
ohair@286 | 1088 | |
ohair@286 | 1089 | public JAXBContextBuilder setTypeRefs(Collection<TypeReference> val) { |
ohair@286 | 1090 | this.typeRefs = val; |
ohair@286 | 1091 | return this; |
ohair@286 | 1092 | } |
ohair@286 | 1093 | |
ohair@286 | 1094 | public JAXBContextBuilder setImprovedXsiTypeHandling(boolean val) { |
ohair@286 | 1095 | this.improvedXsiTypeHandling = val; |
ohair@286 | 1096 | return this; |
ohair@286 | 1097 | } |
ohair@286 | 1098 | |
alanb@368 | 1099 | public JAXBContextBuilder setDisableSecurityProcessing(boolean val) { |
alanb@368 | 1100 | this.disableSecurityProcessing = val; |
alanb@368 | 1101 | return this; |
alanb@368 | 1102 | } |
alanb@368 | 1103 | |
ohair@286 | 1104 | public JAXBContextImpl build() throws JAXBException { |
ohair@286 | 1105 | |
ohair@286 | 1106 | // fool-proof |
ohair@286 | 1107 | if (this.defaultNsUri == null) { |
ohair@286 | 1108 | this.defaultNsUri = ""; |
ohair@286 | 1109 | } |
ohair@286 | 1110 | |
ohair@286 | 1111 | if (this.subclassReplacements == null) { |
ohair@286 | 1112 | this.subclassReplacements = Collections.emptyMap(); |
ohair@286 | 1113 | } |
ohair@286 | 1114 | |
ohair@286 | 1115 | if (this.annotationReader == null) { |
ohair@286 | 1116 | this.annotationReader = new RuntimeInlineAnnotationReader(); |
ohair@286 | 1117 | } |
ohair@286 | 1118 | |
ohair@286 | 1119 | if (this.typeRefs == null) { |
ohair@286 | 1120 | this.typeRefs = Collections.<TypeReference>emptyList(); |
ohair@286 | 1121 | } |
ohair@286 | 1122 | |
ohair@286 | 1123 | return new JAXBContextImpl(this); |
ohair@286 | 1124 | } |
ohair@286 | 1125 | |
ohair@286 | 1126 | } |
ohair@286 | 1127 | |
ohair@286 | 1128 | } |