1 /* |
1 /* |
2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
236 * Additionally, when the root element of XML data has an <tt>xsi:type</tt> attribute and |
236 * Additionally, when the root element of XML data has an <tt>xsi:type</tt> attribute and |
237 * that attribute's value references a type definition that is mapped |
237 * that attribute's value references a type definition that is mapped |
238 * to a JAXB mapped class by {@link JAXBContext}, that the root |
238 * to a JAXB mapped class by {@link JAXBContext}, that the root |
239 * element's <tt>xsi:type</tt> attribute takes |
239 * element's <tt>xsi:type</tt> attribute takes |
240 * precedence over the unmarshal methods <tt>declaredType</tt> parameter. |
240 * precedence over the unmarshal methods <tt>declaredType</tt> parameter. |
241 * These methods always return a <tt>JAXBElement<declaredType></tt> |
241 * These methods always return a <tt>JAXBElement<declaredType></tt> |
242 * instance. The table below shows how the properties of the returned JAXBElement |
242 * instance. The table below shows how the properties of the returned JAXBElement |
243 * instance are set. |
243 * instance are set. |
244 * |
244 * |
245 * <a name="unmarshalDeclaredTypeReturn"></a> |
245 * <a name="unmarshalDeclaredTypeReturn"></a> |
246 * <p> |
246 * <table summary="" border="2" rules="all" cellpadding="4"> |
247 * <table border="2" rules="all" cellpadding="4"> |
|
248 * <thead> |
247 * <thead> |
249 * <tr> |
248 * <tr> |
250 * <th align="center" colspan="2"> |
249 * <th align="center" colspan="2"> |
251 * Unmarshal By Declared Type returned JAXBElement |
250 * Unmarshal By Declared Type returned JAXBElement |
252 * </tr> |
251 * </tr> |
282 * <p> |
281 * <p> |
283 * Unmarshal by declaredType from a <tt>org.w3c.dom.Node</tt>: |
282 * Unmarshal by declaredType from a <tt>org.w3c.dom.Node</tt>: |
284 * <blockquote> |
283 * <blockquote> |
285 * <pre> |
284 * <pre> |
286 * Schema fragment for example |
285 * Schema fragment for example |
287 * <xs:schema> |
286 * <xs:schema> |
288 * <xs:complexType name="FooType">...<\xs:complexType> |
287 * <xs:complexType name="FooType">...<\xs:complexType> |
289 * <!-- global element declaration "PurchaseOrder" --> |
288 * <!-- global element declaration "PurchaseOrder" --> |
290 * <xs:element name="PurchaseOrder"> |
289 * <xs:element name="PurchaseOrder"> |
291 * <xs:complexType> |
290 * <xs:complexType> |
292 * <xs:sequence> |
291 * <xs:sequence> |
293 * <!-- local element declaration "foo" --> |
292 * <!-- local element declaration "foo" --> |
294 * <xs:element name="foo" type="FooType"/> |
293 * <xs:element name="foo" type="FooType"/> |
295 * ... |
294 * ... |
296 * </xs:sequence> |
295 * </xs:sequence> |
297 * </xs:complexType> |
296 * </xs:complexType> |
298 * </xs:element> |
297 * </xs:element> |
299 * </xs:schema> |
298 * </xs:schema> |
300 * |
299 * |
301 * JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" ); |
300 * JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" ); |
302 * Unmarshaller u = jc.createUnmarshaller(); |
301 * Unmarshaller u = jc.createUnmarshaller(); |
303 * |
302 * |
304 * DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
303 * DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
307 * Document doc = db.parse(new File( "nosferatu.xml")); |
306 * Document doc = db.parse(new File( "nosferatu.xml")); |
308 * Element fooSubtree = ...; // traverse DOM till reach xml element foo, constrained by a |
307 * Element fooSubtree = ...; // traverse DOM till reach xml element foo, constrained by a |
309 * // local element declaration in schema. |
308 * // local element declaration in schema. |
310 * |
309 * |
311 * // FooType is the JAXB mapping of the type of local element declaration foo. |
310 * // FooType is the JAXB mapping of the type of local element declaration foo. |
312 * JAXBElement<FooType> foo = u.unmarshal( fooSubtree, FooType.class); |
311 * JAXBElement<FooType> foo = u.unmarshal( fooSubtree, FooType.class); |
313 * </pre> |
312 * </pre> |
314 * </blockquote> |
313 * </blockquote> |
315 * |
314 * |
316 * <p> |
315 * <p> |
317 * <b>Support for SAX2.0 Compliant Parsers</b><br> |
316 * <b>Support for SAX2.0 Compliant Parsers</b><br> |
388 * access to non-public methods and/or fields of the class. |
387 * access to non-public methods and/or fields of the class. |
389 * <p> |
388 * <p> |
390 * The external listener callback mechanism enables the registration of a {@link Listener} |
389 * The external listener callback mechanism enables the registration of a {@link Listener} |
391 * instance with an {@link Unmarshaller#setListener(Listener)}. The external listener receives all callback events, |
390 * instance with an {@link Unmarshaller#setListener(Listener)}. The external listener receives all callback events, |
392 * allowing for more centralized processing than per class defined callback methods. The external listener |
391 * allowing for more centralized processing than per class defined callback methods. The external listener |
393 * receives events when unmarshalling proces is marshalling to a JAXB element or to JAXB mapped class. |
392 * receives events when unmarshalling process is marshalling to a JAXB element or to JAXB mapped class. |
394 * <p> |
393 * <p> |
395 * The 'class defined' and external listener event callback methods are independent of each other, |
394 * The 'class defined' and external listener event callback methods are independent of each other, |
396 * both can be called for one event. The invocation ordering when both listener callback methods exist is |
395 * both can be called for one event. The invocation ordering when both listener callback methods exist is |
397 * defined in {@link Listener#beforeUnmarshal(Object, Object)} and {@link Listener#afterUnmarshal(Object, Object)}. |
396 * defined in {@link Listener#beforeUnmarshal(Object, Object)} and {@link Listener#afterUnmarshal(Object, Object)}. |
398 * <p> |
397 * <p> |
1008 /** |
1007 /** |
1009 * Associates a configured instance of {@link XmlAdapter} with this unmarshaller. |
1008 * Associates a configured instance of {@link XmlAdapter} with this unmarshaller. |
1010 * |
1009 * |
1011 * <p> |
1010 * <p> |
1012 * Every unmarshaller internally maintains a |
1011 * Every unmarshaller internally maintains a |
1013 * {@link java.util.Map}<{@link Class},{@link XmlAdapter}>, |
1012 * {@link java.util.Map}<{@link Class},{@link XmlAdapter}>, |
1014 * which it uses for unmarshalling classes whose fields/methods are annotated |
1013 * which it uses for unmarshalling classes whose fields/methods are annotated |
1015 * with {@link javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter}. |
1014 * with {@link javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter}. |
1016 * |
1015 * |
1017 * <p> |
1016 * <p> |
1018 * This method allows applications to use a configured instance of {@link XmlAdapter}. |
1017 * This method allows applications to use a configured instance of {@link XmlAdapter}. |
1048 public <A extends XmlAdapter> A getAdapter( Class<A> type ); |
1047 public <A extends XmlAdapter> A getAdapter( Class<A> type ); |
1049 |
1048 |
1050 /** |
1049 /** |
1051 * <p>Associate a context that resolves cid's, content-id URIs, to |
1050 * <p>Associate a context that resolves cid's, content-id URIs, to |
1052 * binary data passed as attachments.</p> |
1051 * binary data passed as attachments.</p> |
1053 * <p/> |
|
1054 * <p>Unmarshal time validation, enabled via {@link #setSchema(Schema)}, |
1052 * <p>Unmarshal time validation, enabled via {@link #setSchema(Schema)}, |
1055 * must be supported even when unmarshaller is performing XOP processing. |
1053 * must be supported even when unmarshaller is performing XOP processing. |
1056 * </p> |
1054 * </p> |
1057 * |
1055 * |
1058 * @throws IllegalStateException if attempt to concurrently call this |
1056 * @throws IllegalStateException if attempt to concurrently call this |
1061 void setAttachmentUnmarshaller(AttachmentUnmarshaller au); |
1059 void setAttachmentUnmarshaller(AttachmentUnmarshaller au); |
1062 |
1060 |
1063 AttachmentUnmarshaller getAttachmentUnmarshaller(); |
1061 AttachmentUnmarshaller getAttachmentUnmarshaller(); |
1064 |
1062 |
1065 /** |
1063 /** |
1066 * <p/> |
1064 * <p> |
1067 * Register an instance of an implementation of this class with {@link Unmarshaller} to externally listen |
1065 * Register an instance of an implementation of this class with {@link Unmarshaller} to externally listen |
1068 * for unmarshal events. |
1066 * for unmarshal events. |
1069 * <p/> |
1067 * </p> |
1070 * <p/> |
1068 * <p> |
1071 * This class enables pre and post processing of an instance of a JAXB mapped class |
1069 * This class enables pre and post processing of an instance of a JAXB mapped class |
1072 * as XML data is unmarshalled into it. The event callbacks are called when unmarshalling |
1070 * as XML data is unmarshalled into it. The event callbacks are called when unmarshalling |
1073 * XML content into a JAXBElement instance or a JAXB mapped class that represents a complex type definition. |
1071 * XML content into a JAXBElement instance or a JAXB mapped class that represents a complex type definition. |
1074 * The event callbacks are not called when unmarshalling to an instance of a |
1072 * The event callbacks are not called when unmarshalling to an instance of a |
1075 * Java datatype that represents a simple type definition. |
1073 * Java datatype that represents a simple type definition. |
1076 * <p/> |
1074 * </p> |
1077 * <p/> |
1075 * <p> |
1078 * External listener is one of two different mechanisms for defining unmarshal event callbacks. |
1076 * External listener is one of two different mechanisms for defining unmarshal event callbacks. |
1079 * See <a href="Unmarshaller.html#unmarshalEventCallback">Unmarshal Event Callbacks</a> for an overview. |
1077 * See <a href="Unmarshaller.html#unmarshalEventCallback">Unmarshal Event Callbacks</a> for an overview. |
1080 * <p/> |
1078 * </p> |
1081 * (@link #setListener(Listener)} |
1079 * (@link #setListener(Listener)} |
1082 * (@link #getListener()} |
1080 * (@link #getListener()} |
1083 * |
1081 * |
1084 * @since 1.6, JAXB 2.0 |
1082 * @since 1.6, JAXB 2.0 |
1085 */ |
1083 */ |
1086 public static abstract class Listener { |
1084 public static abstract class Listener { |
1087 /** |
1085 /** |
1088 * <p/> |
1086 * <p> |
1089 * Callback method invoked before unmarshalling into <tt>target</tt>. |
1087 * Callback method invoked before unmarshalling into <tt>target</tt>. |
1090 * <p/> |
1088 * </p> |
1091 * <p/> |
1089 * <p> |
1092 * This method is invoked immediately after <tt>target</tt> was created and |
1090 * This method is invoked immediately after <tt>target</tt> was created and |
1093 * before the unmarshalling of this object begins. Note that |
1091 * before the unmarshalling of this object begins. Note that |
1094 * if the class of <tt>target</tt> defines its own <tt>beforeUnmarshal</tt> method, |
1092 * if the class of <tt>target</tt> defines its own <tt>beforeUnmarshal</tt> method, |
1095 * the class specific callback method is invoked before this method is invoked. |
1093 * the class specific callback method is invoked before this method is invoked. |
1096 * |
1094 * |
1100 */ |
1098 */ |
1101 public void beforeUnmarshal(Object target, Object parent) { |
1099 public void beforeUnmarshal(Object target, Object parent) { |
1102 } |
1100 } |
1103 |
1101 |
1104 /** |
1102 /** |
1105 * <p/> |
1103 * <p> |
1106 * Callback method invoked after unmarshalling XML data into <tt>target</tt>. |
1104 * Callback method invoked after unmarshalling XML data into <tt>target</tt>. |
1107 * <p/> |
1105 * </p> |
1108 * <p/> |
1106 * <p> |
1109 * This method is invoked after all the properties (except IDREF) are unmarshalled into <tt>target</tt>, |
1107 * This method is invoked after all the properties (except IDREF) are unmarshalled into <tt>target</tt>, |
1110 * but before <tt>target</tt> is set into its <tt>parent</tt> object. |
1108 * but before <tt>target</tt> is set into its <tt>parent</tt> object. |
1111 * Note that if the class of <tt>target</tt> defines its own <tt>afterUnmarshal</tt> method, |
1109 * Note that if the class of <tt>target</tt> defines its own <tt>afterUnmarshal</tt> method, |
1112 * the class specific callback method is invoked before this method is invoked. |
1110 * the class specific callback method is invoked before this method is invoked. |
1113 * |
1111 * |