Sun, 31 Aug 2014 16:14:36 +0400
8036981: JAXB not preserving formatting for xsd:any Mixed content
Reviewed-by: lancea, mkos
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.runtime; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.io.IOException; |
aoqi@0 | 29 | import java.lang.reflect.InvocationTargetException; |
aoqi@0 | 30 | import java.lang.reflect.Method; |
aoqi@0 | 31 | import java.util.Arrays; |
aoqi@0 | 32 | import java.util.Collection; |
aoqi@0 | 33 | import java.util.Collections; |
aoqi@0 | 34 | import java.util.logging.Level; |
aoqi@0 | 35 | import java.util.logging.Logger; |
aoqi@0 | 36 | |
aoqi@0 | 37 | import javax.xml.bind.JAXBContext; |
aoqi@0 | 38 | import javax.xml.bind.Marshaller; |
aoqi@0 | 39 | import javax.xml.bind.Unmarshaller; |
aoqi@0 | 40 | import javax.xml.datatype.XMLGregorianCalendar; |
aoqi@0 | 41 | import javax.xml.namespace.QName; |
aoqi@0 | 42 | import javax.xml.stream.XMLStreamException; |
aoqi@0 | 43 | |
aoqi@0 | 44 | import com.sun.istack.internal.NotNull; |
aoqi@0 | 45 | import com.sun.xml.internal.bind.Util; |
aoqi@0 | 46 | import com.sun.xml.internal.bind.v2.model.runtime.RuntimeTypeInfo; |
aoqi@0 | 47 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader; |
aoqi@0 | 48 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl; |
aoqi@0 | 49 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext; |
aoqi@0 | 50 | |
aoqi@0 | 51 | import org.xml.sax.SAXException; |
aoqi@0 | 52 | |
aoqi@0 | 53 | /** |
aoqi@0 | 54 | * Encapsulates various JAXB operations on objects bound by JAXB. |
aoqi@0 | 55 | * Immutable and thread-safe. |
aoqi@0 | 56 | * |
aoqi@0 | 57 | * <p> |
aoqi@0 | 58 | * Each JAXB-bound class has a corresponding {@link JaxBeanInfo} object, |
aoqi@0 | 59 | * which performs all the JAXB related operations on behalf of |
aoqi@0 | 60 | * the JAXB-bound object. |
aoqi@0 | 61 | * |
aoqi@0 | 62 | * <p> |
aoqi@0 | 63 | * Given a class, the corresponding {@link JaxBeanInfo} can be located |
aoqi@0 | 64 | * via {@link JAXBContextImpl#getBeanInfo(Class,boolean)}. |
aoqi@0 | 65 | * |
aoqi@0 | 66 | * <p> |
aoqi@0 | 67 | * Typically, {@link JaxBeanInfo} implementations should be generated |
aoqi@0 | 68 | * by XJC/JXC. Those impl classes will register themselves to their |
aoqi@0 | 69 | * master <tt>ObjectFactory</tt> class. |
aoqi@0 | 70 | * |
aoqi@0 | 71 | * <p> |
aoqi@0 | 72 | * The type parameter BeanT is the Java class of the bean that this represents. |
aoqi@0 | 73 | * |
aoqi@0 | 74 | * @author |
aoqi@0 | 75 | * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) |
aoqi@0 | 76 | */ |
aoqi@0 | 77 | public abstract class JaxBeanInfo<BeanT> { |
aoqi@0 | 78 | |
aoqi@0 | 79 | protected boolean isNilIncluded = false; |
aoqi@0 | 80 | |
aoqi@0 | 81 | /** |
aoqi@0 | 82 | * For {@link JaxBeanInfo} that has multiple type names. |
aoqi@0 | 83 | */ |
aoqi@0 | 84 | protected JaxBeanInfo(JAXBContextImpl grammar, RuntimeTypeInfo rti, Class<BeanT> jaxbType, QName[] typeNames, boolean isElement,boolean isImmutable, boolean hasLifecycleEvents) { |
aoqi@0 | 85 | this(grammar,rti,jaxbType,(Object)typeNames,isElement,isImmutable,hasLifecycleEvents); |
aoqi@0 | 86 | } |
aoqi@0 | 87 | |
aoqi@0 | 88 | /** |
aoqi@0 | 89 | * For {@link JaxBeanInfo} that has one type name. |
aoqi@0 | 90 | */ |
aoqi@0 | 91 | protected JaxBeanInfo(JAXBContextImpl grammar, RuntimeTypeInfo rti, Class<BeanT> jaxbType, QName typeName, boolean isElement,boolean isImmutable, boolean hasLifecycleEvents) { |
aoqi@0 | 92 | this(grammar,rti,jaxbType,(Object)typeName,isElement,isImmutable,hasLifecycleEvents); |
aoqi@0 | 93 | } |
aoqi@0 | 94 | |
aoqi@0 | 95 | /** |
aoqi@0 | 96 | * For {@link JaxBeanInfo} that has no type names. |
aoqi@0 | 97 | */ |
aoqi@0 | 98 | protected JaxBeanInfo(JAXBContextImpl grammar, RuntimeTypeInfo rti, Class<BeanT> jaxbType, boolean isElement,boolean isImmutable, boolean hasLifecycleEvents) { |
aoqi@0 | 99 | this(grammar,rti,jaxbType,(Object)null,isElement,isImmutable,hasLifecycleEvents); |
aoqi@0 | 100 | } |
aoqi@0 | 101 | |
aoqi@0 | 102 | private JaxBeanInfo(JAXBContextImpl grammar, RuntimeTypeInfo rti, Class<BeanT> jaxbType, Object typeName, boolean isElement,boolean isImmutable, boolean hasLifecycleEvents) { |
aoqi@0 | 103 | grammar.beanInfos.put(rti,this); |
aoqi@0 | 104 | |
aoqi@0 | 105 | this.jaxbType = jaxbType; |
aoqi@0 | 106 | this.typeName = typeName; |
aoqi@0 | 107 | this.flag = (short)((isElement?FLAG_IS_ELEMENT:0) |
aoqi@0 | 108 | |(isImmutable?FLAG_IS_IMMUTABLE:0) |
aoqi@0 | 109 | |(hasLifecycleEvents?FLAG_HAS_LIFECYCLE_EVENTS:0)); |
aoqi@0 | 110 | } |
aoqi@0 | 111 | |
aoqi@0 | 112 | /** |
aoqi@0 | 113 | * Various boolean flags combined into one field to improve memory footprint. |
aoqi@0 | 114 | */ |
aoqi@0 | 115 | protected short flag; |
aoqi@0 | 116 | |
aoqi@0 | 117 | private static final short FLAG_IS_ELEMENT = 1; |
aoqi@0 | 118 | private static final short FLAG_IS_IMMUTABLE = 2; |
aoqi@0 | 119 | private static final short FLAG_HAS_ELEMENT_ONLY_CONTENTMODEL = 4; |
aoqi@0 | 120 | private static final short FLAG_HAS_BEFORE_UNMARSHAL_METHOD = 8; |
aoqi@0 | 121 | private static final short FLAG_HAS_AFTER_UNMARSHAL_METHOD = 16; |
aoqi@0 | 122 | private static final short FLAG_HAS_BEFORE_MARSHAL_METHOD = 32; |
aoqi@0 | 123 | private static final short FLAG_HAS_AFTER_MARSHAL_METHOD = 64; |
aoqi@0 | 124 | private static final short FLAG_HAS_LIFECYCLE_EVENTS = 128; |
aoqi@0 | 125 | |
aoqi@0 | 126 | /** cache of lifecycle methods */ |
aoqi@0 | 127 | private LifecycleMethods lcm = null; |
aoqi@0 | 128 | |
aoqi@0 | 129 | /** |
aoqi@0 | 130 | * True if {@link #jaxbType} has the lifecycle method. |
aoqi@0 | 131 | */ |
aoqi@0 | 132 | public final boolean hasBeforeUnmarshalMethod() { |
aoqi@0 | 133 | return (flag&FLAG_HAS_BEFORE_UNMARSHAL_METHOD) != 0; |
aoqi@0 | 134 | } |
aoqi@0 | 135 | |
aoqi@0 | 136 | /** |
aoqi@0 | 137 | * True if {@link #jaxbType} has the lifecycle method. |
aoqi@0 | 138 | */ |
aoqi@0 | 139 | public final boolean hasAfterUnmarshalMethod() { |
aoqi@0 | 140 | return (flag&FLAG_HAS_AFTER_UNMARSHAL_METHOD) != 0; |
aoqi@0 | 141 | } |
aoqi@0 | 142 | |
aoqi@0 | 143 | /** |
aoqi@0 | 144 | * True if {@link #jaxbType} has the lifecycle method. |
aoqi@0 | 145 | */ |
aoqi@0 | 146 | public final boolean hasBeforeMarshalMethod() { |
aoqi@0 | 147 | return (flag&FLAG_HAS_BEFORE_MARSHAL_METHOD) != 0; |
aoqi@0 | 148 | } |
aoqi@0 | 149 | |
aoqi@0 | 150 | /** |
aoqi@0 | 151 | * True if {@link #jaxbType} has the lifecycle method. |
aoqi@0 | 152 | */ |
aoqi@0 | 153 | public final boolean hasAfterMarshalMethod() { |
aoqi@0 | 154 | return (flag&FLAG_HAS_AFTER_MARSHAL_METHOD) != 0; |
aoqi@0 | 155 | } |
aoqi@0 | 156 | |
aoqi@0 | 157 | /** |
aoqi@0 | 158 | * Gets the JAXB bound class type that this {@link JaxBeanInfo} |
aoqi@0 | 159 | * handles. |
aoqi@0 | 160 | * |
aoqi@0 | 161 | * <p> |
aoqi@0 | 162 | * IOW, when a bean info object is requested for T, |
aoqi@0 | 163 | * sometimes the bean info for one of its base classes might be |
aoqi@0 | 164 | * returned. |
aoqi@0 | 165 | */ |
aoqi@0 | 166 | public final Class<BeanT> jaxbType; |
aoqi@0 | 167 | |
aoqi@0 | 168 | /** |
aoqi@0 | 169 | * Returns true if the bean is mapped to/from an XML element. |
aoqi@0 | 170 | * |
aoqi@0 | 171 | * <p> |
aoqi@0 | 172 | * When this method returns true, {@link #getElementNamespaceURI(Object)} |
aoqi@0 | 173 | * and {@link #getElementLocalName(Object)} returns the element name of |
aoqi@0 | 174 | * the bean. |
aoqi@0 | 175 | */ |
aoqi@0 | 176 | public final boolean isElement() { |
aoqi@0 | 177 | return (flag&FLAG_IS_ELEMENT)!=0; |
aoqi@0 | 178 | } |
aoqi@0 | 179 | |
aoqi@0 | 180 | /** |
aoqi@0 | 181 | * Returns true if the bean is immutable. |
aoqi@0 | 182 | * |
aoqi@0 | 183 | * <p> |
aoqi@0 | 184 | * If this is true, Binder won't try to ueuse this object, and the unmarshaller |
aoqi@0 | 185 | * won't create a new instance of it before it starts. |
aoqi@0 | 186 | */ |
aoqi@0 | 187 | public final boolean isImmutable() { |
aoqi@0 | 188 | return (flag&FLAG_IS_IMMUTABLE)!=0; |
aoqi@0 | 189 | } |
aoqi@0 | 190 | |
aoqi@0 | 191 | /** |
aoqi@0 | 192 | * True if this bean has an element-only content model. |
aoqi@0 | 193 | * <p> |
aoqi@0 | 194 | * If this flag is true, the unmarshaller can work |
aoqi@0 | 195 | * faster by ignoring whitespaces more efficiently. |
aoqi@0 | 196 | */ |
aoqi@0 | 197 | public final boolean hasElementOnlyContentModel() { |
aoqi@0 | 198 | return (flag&FLAG_HAS_ELEMENT_ONLY_CONTENTMODEL)!=0; |
aoqi@0 | 199 | } |
aoqi@0 | 200 | |
aoqi@0 | 201 | /** |
aoqi@0 | 202 | * True if this bean has an element-only content model. |
aoqi@0 | 203 | * <p> |
aoqi@0 | 204 | * Should be considered immutable, though I can't mark it final |
aoqi@0 | 205 | * because it cannot be computed in this constructor. |
aoqi@0 | 206 | */ |
aoqi@0 | 207 | protected final void hasElementOnlyContentModel(boolean value) { |
aoqi@0 | 208 | if(value) |
aoqi@0 | 209 | flag |= FLAG_HAS_ELEMENT_ONLY_CONTENTMODEL; |
aoqi@0 | 210 | else |
aoqi@0 | 211 | flag &= ~FLAG_HAS_ELEMENT_ONLY_CONTENTMODEL; |
aoqi@0 | 212 | } |
aoqi@0 | 213 | |
aoqi@0 | 214 | public boolean isNilIncluded() { |
aoqi@0 | 215 | return isNilIncluded; |
aoqi@0 | 216 | } |
aoqi@0 | 217 | |
aoqi@0 | 218 | /** |
aoqi@0 | 219 | * This method is used to determine which of the sub-classes should be |
aoqi@0 | 220 | * interrogated for the existence of lifecycle methods. |
aoqi@0 | 221 | * |
aoqi@0 | 222 | * @return true if the un|marshaller should look for lifecycle methods |
aoqi@0 | 223 | * on this beanInfo, false otherwise. |
aoqi@0 | 224 | */ |
aoqi@0 | 225 | public boolean lookForLifecycleMethods() { |
aoqi@0 | 226 | return (flag&FLAG_HAS_LIFECYCLE_EVENTS)!=0; |
aoqi@0 | 227 | } |
aoqi@0 | 228 | |
aoqi@0 | 229 | /** |
aoqi@0 | 230 | * Returns the namespace URI portion of the element name, |
aoqi@0 | 231 | * if the bean that this class represents is mapped from/to |
aoqi@0 | 232 | * an XML element. |
aoqi@0 | 233 | * |
aoqi@0 | 234 | * @throws UnsupportedOperationException |
aoqi@0 | 235 | * if {@link #isElement} is false. |
aoqi@0 | 236 | */ |
aoqi@0 | 237 | public abstract String getElementNamespaceURI(BeanT o); |
aoqi@0 | 238 | |
aoqi@0 | 239 | /** |
aoqi@0 | 240 | * Returns the local name portion of the element name, |
aoqi@0 | 241 | * if the bean that this class represents is mapped from/to |
aoqi@0 | 242 | * an XML element. |
aoqi@0 | 243 | * |
aoqi@0 | 244 | * @throws UnsupportedOperationException |
aoqi@0 | 245 | * if {@link #isElement} is false. |
aoqi@0 | 246 | */ |
aoqi@0 | 247 | public abstract String getElementLocalName(BeanT o); |
aoqi@0 | 248 | |
aoqi@0 | 249 | /** |
aoqi@0 | 250 | * Type names associated with this {@link JaxBeanInfo}. |
aoqi@0 | 251 | * |
aoqi@0 | 252 | * @see #getTypeNames() |
aoqi@0 | 253 | */ |
aoqi@0 | 254 | private final Object typeName; // either null, QName, or QName[]. save memory since most of them have just one. |
aoqi@0 | 255 | |
aoqi@0 | 256 | /** |
aoqi@0 | 257 | * Returns XML Schema type names if the bean is mapped from |
aoqi@0 | 258 | * a complex/simple type of XML Schema. |
aoqi@0 | 259 | * |
aoqi@0 | 260 | * <p> |
aoqi@0 | 261 | * This is an ugly necessity to correctly handle |
aoqi@0 | 262 | * the type substitution semantics of XML Schema. |
aoqi@0 | 263 | * |
aoqi@0 | 264 | * <p> |
aoqi@0 | 265 | * A single Java class maybe mapped to more than one |
aoqi@0 | 266 | * XML types. All the types listed here are recognized |
aoqi@0 | 267 | * when we are unmarshalling XML. |
aoqi@0 | 268 | * |
aoqi@0 | 269 | * <p> |
aoqi@0 | 270 | * null if the class is not bound to a named schema type. |
aoqi@0 | 271 | * |
aoqi@0 | 272 | * <p> |
aoqi@0 | 273 | */ |
aoqi@0 | 274 | public Collection<QName> getTypeNames() { |
aoqi@0 | 275 | if(typeName==null) return Collections.emptyList(); |
aoqi@0 | 276 | if(typeName instanceof QName) return Collections.singletonList((QName)typeName); |
aoqi@0 | 277 | return Arrays.asList((QName[])typeName); |
aoqi@0 | 278 | } |
aoqi@0 | 279 | |
aoqi@0 | 280 | /** |
aoqi@0 | 281 | * Returns the XML type name to be used to marshal the specified instance. |
aoqi@0 | 282 | * |
aoqi@0 | 283 | * <P> |
aoqi@0 | 284 | * Most of the times the type can be determined regardless of the actual |
aoqi@0 | 285 | * instance, but there's a few exceptions (most notably {@link XMLGregorianCalendar}), |
aoqi@0 | 286 | * so as a general rule we need an instance to determine it. |
aoqi@0 | 287 | */ |
aoqi@0 | 288 | public QName getTypeName(@NotNull BeanT instance) { |
aoqi@0 | 289 | if(typeName==null) return null; |
aoqi@0 | 290 | if(typeName instanceof QName) return (QName)typeName; |
aoqi@0 | 291 | return ((QName[])typeName)[0]; |
aoqi@0 | 292 | } |
aoqi@0 | 293 | |
aoqi@0 | 294 | /** |
aoqi@0 | 295 | * Creates a new instance of the bean. |
aoqi@0 | 296 | * |
aoqi@0 | 297 | * <p> |
aoqi@0 | 298 | * This operation is only supported when {@link #isImmutable} is false. |
aoqi@0 | 299 | * |
aoqi@0 | 300 | * @param context |
aoqi@0 | 301 | * Sometimes the created bean remembers the corresponding source location, |
aoqi@0 | 302 | */ |
aoqi@0 | 303 | public abstract BeanT createInstance(UnmarshallingContext context) throws IllegalAccessException, InvocationTargetException, InstantiationException, SAXException; |
aoqi@0 | 304 | |
aoqi@0 | 305 | /** |
aoqi@0 | 306 | * Resets the object to the initial state, as if the object |
aoqi@0 | 307 | * is created fresh. |
aoqi@0 | 308 | * |
aoqi@0 | 309 | * <p> |
aoqi@0 | 310 | * This is used to reuse an existing object for unmarshalling. |
aoqi@0 | 311 | * |
aoqi@0 | 312 | * @param context |
aoqi@0 | 313 | * used for reporting any errors. |
aoqi@0 | 314 | * |
aoqi@0 | 315 | * @return |
aoqi@0 | 316 | * true if the object was successfuly resetted. |
aoqi@0 | 317 | * False if the object is not resettable, in which case the object will be |
aoqi@0 | 318 | * discarded and new one will be created. |
aoqi@0 | 319 | * <p> |
aoqi@0 | 320 | * If the object is resettable but failed by an error, it should be reported to the context, |
aoqi@0 | 321 | * then return false. If the object is not resettable to begin with, do not report an error. |
aoqi@0 | 322 | * |
aoqi@0 | 323 | * @throws SAXException |
aoqi@0 | 324 | * as a result of reporting an error, the context may throw a {@link SAXException}. |
aoqi@0 | 325 | */ |
aoqi@0 | 326 | public abstract boolean reset( BeanT o, UnmarshallingContext context ) throws SAXException; |
aoqi@0 | 327 | |
aoqi@0 | 328 | /** |
aoqi@0 | 329 | * Gets the ID value of the given bean, if it has an ID value. |
aoqi@0 | 330 | * Otherwise return null. |
aoqi@0 | 331 | */ |
aoqi@0 | 332 | public abstract String getId(BeanT o, XMLSerializer target) throws SAXException; |
aoqi@0 | 333 | |
aoqi@0 | 334 | /** |
aoqi@0 | 335 | * Serializes child elements and texts into the specified target. |
aoqi@0 | 336 | */ |
aoqi@0 | 337 | public abstract void serializeBody( BeanT o, XMLSerializer target ) throws SAXException, IOException, XMLStreamException; |
aoqi@0 | 338 | |
aoqi@0 | 339 | /** |
aoqi@0 | 340 | * Serializes attributes into the specified target. |
aoqi@0 | 341 | */ |
aoqi@0 | 342 | public abstract void serializeAttributes( BeanT o, XMLSerializer target ) throws SAXException, IOException, XMLStreamException; |
aoqi@0 | 343 | |
aoqi@0 | 344 | /** |
aoqi@0 | 345 | * Serializes the bean as the root element. |
aoqi@0 | 346 | * |
aoqi@0 | 347 | * <p> |
aoqi@0 | 348 | * In the java-to-schema binding, an object might marshal in two different |
aoqi@0 | 349 | * ways depending on whether it is used as the root of the graph or not. |
aoqi@0 | 350 | * In the former case, an object could marshal as an element, whereas |
aoqi@0 | 351 | * in the latter case, it marshals as a type. |
aoqi@0 | 352 | * |
aoqi@0 | 353 | * <p> |
aoqi@0 | 354 | * This method is used to marshal the root of the object graph to allow |
aoqi@0 | 355 | * this semantics to be implemented. |
aoqi@0 | 356 | * |
aoqi@0 | 357 | * <p> |
aoqi@0 | 358 | * It is doubtful to me if it's a good idea for an object to marshal |
aoqi@0 | 359 | * in two ways depending on the context. |
aoqi@0 | 360 | * |
aoqi@0 | 361 | * <p> |
aoqi@0 | 362 | * For schema-to-java, this is equivalent to {@link #serializeBody(Object, XMLSerializer)}. |
aoqi@0 | 363 | */ |
aoqi@0 | 364 | public abstract void serializeRoot( BeanT o, XMLSerializer target ) throws SAXException, IOException, XMLStreamException; |
aoqi@0 | 365 | |
aoqi@0 | 366 | /** |
aoqi@0 | 367 | * Declares all the namespace URIs this object is using at |
aoqi@0 | 368 | * its top-level scope into the specified target. |
aoqi@0 | 369 | */ |
aoqi@0 | 370 | public abstract void serializeURIs( BeanT o, XMLSerializer target ) throws SAXException; |
aoqi@0 | 371 | |
aoqi@0 | 372 | /** |
aoqi@0 | 373 | * Gets the {@link Loader} that will unmarshall the given object. |
aoqi@0 | 374 | * |
aoqi@0 | 375 | * @param context |
aoqi@0 | 376 | * The {@link JAXBContextImpl} object that governs this object. |
aoqi@0 | 377 | * This object is taken as a parameter so that {@link JaxBeanInfo} doesn't have |
aoqi@0 | 378 | * to store them on its own. |
aoqi@0 | 379 | * |
aoqi@0 | 380 | * When this method is invoked from within the unmarshaller, tihs parameter can be |
aoqi@0 | 381 | * null (because the loader is constructed already.) |
aoqi@0 | 382 | * |
aoqi@0 | 383 | * @param typeSubstitutionCapable |
aoqi@0 | 384 | * If true, the returned {@link Loader} is capable of recognizing @xsi:type (if necessary) |
aoqi@0 | 385 | * and unmarshals a subtype. This allowes an optimization where this bean info |
aoqi@0 | 386 | * is guaranteed not to have a type substitution. |
aoqi@0 | 387 | * If false, the returned {@link Loader} doesn't look for @xsi:type. |
aoqi@0 | 388 | * @return |
aoqi@0 | 389 | * must return non-null valid object |
aoqi@0 | 390 | */ |
aoqi@0 | 391 | public abstract Loader getLoader(JAXBContextImpl context, boolean typeSubstitutionCapable); |
aoqi@0 | 392 | |
aoqi@0 | 393 | /** |
aoqi@0 | 394 | * If the bean's representation in XML is just a text, |
aoqi@0 | 395 | * this method return a {@link Transducer} that lets you convert |
aoqi@0 | 396 | * values between the text and the bean. |
aoqi@0 | 397 | */ |
aoqi@0 | 398 | public abstract Transducer<BeanT> getTransducer(); |
aoqi@0 | 399 | |
aoqi@0 | 400 | |
aoqi@0 | 401 | /** |
aoqi@0 | 402 | * Called after all the {@link JaxBeanInfo}s are created. |
aoqi@0 | 403 | * @param grammar |
aoqi@0 | 404 | */ |
aoqi@0 | 405 | protected void link(JAXBContextImpl grammar) { |
aoqi@0 | 406 | } |
aoqi@0 | 407 | |
aoqi@0 | 408 | /** |
aoqi@0 | 409 | * Called at the end of the {@link JAXBContext} initialization phase |
aoqi@0 | 410 | * to clean up any unnecessary references. |
aoqi@0 | 411 | */ |
aoqi@0 | 412 | public void wrapUp() {} |
aoqi@0 | 413 | |
aoqi@0 | 414 | |
aoqi@0 | 415 | private static final Class[] unmarshalEventParams = { Unmarshaller.class, Object.class }; |
aoqi@0 | 416 | private static Class[] marshalEventParams = { Marshaller.class }; |
aoqi@0 | 417 | |
aoqi@0 | 418 | /** |
aoqi@0 | 419 | * use reflection to determine which of the 4 object lifecycle methods exist on |
aoqi@0 | 420 | * the JAXB bound type. |
aoqi@0 | 421 | */ |
aoqi@0 | 422 | protected final void setLifecycleFlags() { |
aoqi@0 | 423 | try { |
aoqi@0 | 424 | Class<BeanT> jt = jaxbType; |
aoqi@0 | 425 | |
aoqi@0 | 426 | if (lcm == null) { |
aoqi@0 | 427 | lcm = new LifecycleMethods(); |
aoqi@0 | 428 | } |
aoqi@0 | 429 | |
aoqi@0 | 430 | while (jt != null) { |
aoqi@0 | 431 | for (Method m : jt.getDeclaredMethods()) { |
aoqi@0 | 432 | String name = m.getName(); |
aoqi@0 | 433 | |
aoqi@0 | 434 | if (lcm.beforeUnmarshal == null) { |
aoqi@0 | 435 | if (name.equals("beforeUnmarshal")) { |
aoqi@0 | 436 | if (match(m, unmarshalEventParams)) { |
aoqi@0 | 437 | cacheLifecycleMethod(m, FLAG_HAS_BEFORE_UNMARSHAL_METHOD); |
aoqi@0 | 438 | } |
aoqi@0 | 439 | } |
aoqi@0 | 440 | } |
aoqi@0 | 441 | |
aoqi@0 | 442 | if (lcm.afterUnmarshal == null) { |
aoqi@0 | 443 | if (name.equals("afterUnmarshal")) { |
aoqi@0 | 444 | if (match(m, unmarshalEventParams)) { |
aoqi@0 | 445 | cacheLifecycleMethod(m, FLAG_HAS_AFTER_UNMARSHAL_METHOD); |
aoqi@0 | 446 | } |
aoqi@0 | 447 | } |
aoqi@0 | 448 | } |
aoqi@0 | 449 | |
aoqi@0 | 450 | if (lcm.beforeMarshal == null) { |
aoqi@0 | 451 | if (name.equals("beforeMarshal")) { |
aoqi@0 | 452 | if (match(m, marshalEventParams)) { |
aoqi@0 | 453 | cacheLifecycleMethod(m, FLAG_HAS_BEFORE_MARSHAL_METHOD); |
aoqi@0 | 454 | } |
aoqi@0 | 455 | } |
aoqi@0 | 456 | } |
aoqi@0 | 457 | |
aoqi@0 | 458 | if (lcm.afterMarshal == null) { |
aoqi@0 | 459 | if (name.equals("afterMarshal")) { |
aoqi@0 | 460 | if (match(m, marshalEventParams)) { |
aoqi@0 | 461 | cacheLifecycleMethod(m, FLAG_HAS_AFTER_MARSHAL_METHOD); |
aoqi@0 | 462 | } |
aoqi@0 | 463 | } |
aoqi@0 | 464 | } |
aoqi@0 | 465 | } |
aoqi@0 | 466 | jt = (Class<BeanT>) jt.getSuperclass(); |
aoqi@0 | 467 | } |
aoqi@0 | 468 | } catch (SecurityException e) { |
aoqi@0 | 469 | // this happens when we don't have enough permission. |
aoqi@0 | 470 | logger.log(Level.WARNING, Messages.UNABLE_TO_DISCOVER_EVENTHANDLER.format( |
aoqi@0 | 471 | jaxbType.getName(), e)); |
aoqi@0 | 472 | } |
aoqi@0 | 473 | } |
aoqi@0 | 474 | |
aoqi@0 | 475 | private boolean match(Method m, Class[] params) { |
aoqi@0 | 476 | return Arrays.equals(m.getParameterTypes(),params); |
aoqi@0 | 477 | } |
aoqi@0 | 478 | |
aoqi@0 | 479 | /** |
aoqi@0 | 480 | * Cache a reference to the specified lifecycle method for the jaxbType |
aoqi@0 | 481 | * associated with this beanInfo. |
aoqi@0 | 482 | * |
aoqi@0 | 483 | * @param m Method reference |
aoqi@0 | 484 | * @param lifecycleFlag byte representing which of the 4 lifecycle methods |
aoqi@0 | 485 | * is being cached |
aoqi@0 | 486 | */ |
aoqi@0 | 487 | private void cacheLifecycleMethod(Method m, short lifecycleFlag) { |
aoqi@0 | 488 | //LifecycleMethods lcm = getLifecycleMethods(); |
aoqi@0 | 489 | if(lcm==null) { |
aoqi@0 | 490 | lcm = new LifecycleMethods(); |
aoqi@0 | 491 | //lcmCache.put(jaxbType, lcm); |
aoqi@0 | 492 | } |
aoqi@0 | 493 | |
aoqi@0 | 494 | m.setAccessible(true); |
aoqi@0 | 495 | |
aoqi@0 | 496 | flag |= lifecycleFlag; |
aoqi@0 | 497 | |
aoqi@0 | 498 | switch (lifecycleFlag) { |
aoqi@0 | 499 | case FLAG_HAS_BEFORE_UNMARSHAL_METHOD: |
aoqi@0 | 500 | lcm.beforeUnmarshal = m; |
aoqi@0 | 501 | break; |
aoqi@0 | 502 | case FLAG_HAS_AFTER_UNMARSHAL_METHOD: |
aoqi@0 | 503 | lcm.afterUnmarshal = m; |
aoqi@0 | 504 | break; |
aoqi@0 | 505 | case FLAG_HAS_BEFORE_MARSHAL_METHOD: |
aoqi@0 | 506 | lcm.beforeMarshal = m; |
aoqi@0 | 507 | break; |
aoqi@0 | 508 | case FLAG_HAS_AFTER_MARSHAL_METHOD: |
aoqi@0 | 509 | lcm.afterMarshal = m; |
aoqi@0 | 510 | break; |
aoqi@0 | 511 | } |
aoqi@0 | 512 | } |
aoqi@0 | 513 | |
aoqi@0 | 514 | /** |
aoqi@0 | 515 | * Return the LifecycleMethods cache for this ClassBeanInfo's corresponding |
aoqi@0 | 516 | * jaxbType if it exists, else return null. |
aoqi@0 | 517 | * |
aoqi@0 | 518 | */ |
aoqi@0 | 519 | public final LifecycleMethods getLifecycleMethods() { |
aoqi@0 | 520 | return lcm; |
aoqi@0 | 521 | } |
aoqi@0 | 522 | |
aoqi@0 | 523 | /** |
aoqi@0 | 524 | * Invokes the beforeUnmarshal method if applicable. |
aoqi@0 | 525 | */ |
aoqi@0 | 526 | public final void invokeBeforeUnmarshalMethod(UnmarshallerImpl unm, Object child, Object parent) throws SAXException { |
aoqi@0 | 527 | Method m = getLifecycleMethods().beforeUnmarshal; |
aoqi@0 | 528 | invokeUnmarshallCallback(m, child, unm, parent); |
aoqi@0 | 529 | } |
aoqi@0 | 530 | |
aoqi@0 | 531 | /** |
aoqi@0 | 532 | * Invokes the afterUnmarshal method if applicable. |
aoqi@0 | 533 | */ |
aoqi@0 | 534 | public final void invokeAfterUnmarshalMethod(UnmarshallerImpl unm, Object child, Object parent) throws SAXException { |
aoqi@0 | 535 | Method m = getLifecycleMethods().afterUnmarshal; |
aoqi@0 | 536 | invokeUnmarshallCallback(m, child, unm, parent); |
aoqi@0 | 537 | } |
aoqi@0 | 538 | |
aoqi@0 | 539 | private void invokeUnmarshallCallback(Method m, Object child, UnmarshallerImpl unm, Object parent) throws SAXException { |
aoqi@0 | 540 | try { |
aoqi@0 | 541 | m.invoke(child,unm,parent); |
aoqi@0 | 542 | } catch (IllegalAccessException e) { |
aoqi@0 | 543 | UnmarshallingContext.getInstance().handleError(e, false); |
aoqi@0 | 544 | } catch (InvocationTargetException e) { |
aoqi@0 | 545 | UnmarshallingContext.getInstance().handleError(e, false); |
aoqi@0 | 546 | } |
aoqi@0 | 547 | } |
aoqi@0 | 548 | |
aoqi@0 | 549 | private static final Logger logger = Util.getClassLogger(); |
aoqi@0 | 550 | } |