< prev index next >

src/java.xml.bind/share/classes/javax/xml/bind/annotation/adapters/XmlAdapter.java

Print this page




  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.xml.bind.annotation.adapters;
  27 
  28 /**
  29  * Adapts a Java type for custom marshaling.
  30  *
  31  * <p> <b> Usage: </b> </p>
  32  *
  33  * <p>
  34  * Some Java types do not map naturally to a XML representation, for
  35  * example <tt>HashMap</tt> or other non JavaBean classes. Conversely,
  36  * a XML repsentation may map to a Java type but an application may
  37  * choose to accesss the XML representation using another Java
  38  * type. For example, the schema to Java binding rules bind
  39  * xs:DateTime by default to XmlGregorianCalendar. But an application
  40  * may desire to bind xs:DateTime to a custom type,
  41  * MyXmlGregorianCalendar, for example. In both cases, there is a
  42  * mismatch between <i> bound type </i>, used by an application to
  43  * access XML content and the <i> value type</i>, that is mapped to an
  44  * XML representation.
  45  *
  46  * <p>
  47  * This abstract class defines methods for adapting a bound type to a value
  48  * type or vice versa. The methods are invoked by the JAXB binding
  49  * framework during marshaling and unmarshalling:
  50  *
  51  * <ul>
  52  *   <li> <b> XmlAdapter.marshal(...): </b> During marshalling, JAXB
  53  *        binding framework invokes XmlAdapter.marshal(..) to adapt a
  54  *        bound type to value type, which is then marshaled to XML
  55  *        representation. </li>
  56  *
  57  *   <li> <b> XmlAdapter.unmarshal(...): </b> During unmarshalling,
  58  *        JAXB binding framework first unmarshals XML representation
  59  *        to a value type and then invokes XmlAdapter.unmarshal(..) to
  60  *        adapt the value type to a bound type. </li>
  61  * </ul>
  62  *
  63  * Writing an adapter therefore involves the following steps:
  64  *
  65  * <ul>
  66  *   <li> Write an adapter that implements this abstract class. </li>
  67  *   <li> Install the adapter using the annotation {@link
  68  *        XmlJavaTypeAdapter} </li>
  69  * </ul>
  70  *
  71  * <p><b>Example:</b> Customized mapping of <tt>HashMap</tt></p>
  72  * <p> The following example illustrates the use of
  73  * <tt>@XmlAdapter</tt> and <tt>@XmlJavaTypeAdapter</tt> to
  74  * customize the mapping of a <tt>HashMap</tt>.
  75  *
  76  * <p> <b> Step 1: </b> Determine the desired XML representation for HashMap.
  77  *
  78  * <pre>{@code
  79  *     <hashmap>
  80  *         <entry key="id123">this is a value</entry>
  81  *         <entry key="id312">this is another value</entry>
  82  *         ...
  83  *     </hashmap>
  84  * }</pre>
  85  *
  86  * <p> <b> Step 2: </b> Determine the schema definition that the
  87  * desired XML representation shown above should follow.
  88  *
  89  * <pre>{@code
  90  *
  91  *     <xs:complexType name="myHashMapType">
  92  *       <xs:sequence>
  93  *         <xs:element name="entry" type="myHashMapEntryType"
  94  *                        minOccurs = "0" maxOccurs="unbounded"/>


 137  *     public class Foo {
 138  *         @XmlJavaTypeAdapter(MyHashMapAdapter.class)
 139  *         HashMap hashmap;
 140  *         ...
 141  *     }
 142  * </pre>
 143  *
 144  * The above code fragment will map to the following schema:
 145  *
 146  * <pre>{@code
 147  *     <xs:complexType name="Foo">
 148  *       <xs:sequence>
 149  *         <xs:element name="hashmap" type="myHashMapType">
 150  *       </xs:sequence>
 151  *     </xs:complexType>
 152  * }</pre>
 153  *
 154  * @param <BoundType>
 155  *      The type that JAXB doesn't know how to handle. An adapter is written
 156  *      to allow this type to be used as an in-memory representation through
 157  *      the <tt>ValueType</tt>.
 158  * @param <ValueType>
 159  *      The type that JAXB knows how to handle out of the box.
 160  *
 161  * @author <ul><li>Sekhar Vajjhala, Sun Microsystems Inc.</li> <li> Kohsuke Kawaguchi, Sun Microsystems Inc.</li></ul>
 162  * @see XmlJavaTypeAdapter
 163  * @since 1.6, JAXB 2.0
 164  */
 165 public abstract class XmlAdapter<ValueType,BoundType> {
 166 
 167     /**
 168      * Do-nothing constructor for the derived classes.
 169      */
 170     protected XmlAdapter() {}
 171 
 172     /**
 173      * Convert a value type to a bound type.
 174      *
 175      * @param v
 176      *      The value to be converted. Can be null.
 177      * @throws Exception


  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.xml.bind.annotation.adapters;
  27 
  28 /**
  29  * Adapts a Java type for custom marshaling.
  30  *
  31  * <p> <b> Usage: </b> </p>
  32  *
  33  * <p>
  34  * Some Java types do not map naturally to a XML representation, for
  35  * example {@code HashMap} or other non JavaBean classes. Conversely,
  36  * a XML repsentation may map to a Java type but an application may
  37  * choose to accesss the XML representation using another Java
  38  * type. For example, the schema to Java binding rules bind
  39  * xs:DateTime by default to XmlGregorianCalendar. But an application
  40  * may desire to bind xs:DateTime to a custom type,
  41  * MyXmlGregorianCalendar, for example. In both cases, there is a
  42  * mismatch between <i> bound type </i>, used by an application to
  43  * access XML content and the <i> value type</i>, that is mapped to an
  44  * XML representation.
  45  *
  46  * <p>
  47  * This abstract class defines methods for adapting a bound type to a value
  48  * type or vice versa. The methods are invoked by the JAXB binding
  49  * framework during marshaling and unmarshalling:
  50  *
  51  * <ul>
  52  *   <li> <b> XmlAdapter.marshal(...): </b> During marshalling, JAXB
  53  *        binding framework invokes XmlAdapter.marshal(..) to adapt a
  54  *        bound type to value type, which is then marshaled to XML
  55  *        representation. </li>
  56  *
  57  *   <li> <b> XmlAdapter.unmarshal(...): </b> During unmarshalling,
  58  *        JAXB binding framework first unmarshals XML representation
  59  *        to a value type and then invokes XmlAdapter.unmarshal(..) to
  60  *        adapt the value type to a bound type. </li>
  61  * </ul>
  62  *
  63  * Writing an adapter therefore involves the following steps:
  64  *
  65  * <ul>
  66  *   <li> Write an adapter that implements this abstract class. </li>
  67  *   <li> Install the adapter using the annotation {@link
  68  *        XmlJavaTypeAdapter} </li>
  69  * </ul>
  70  *
  71  * <p><b>Example:</b> Customized mapping of {@code HashMap}</p>
  72  * <p> The following example illustrates the use of
  73  * {@code @XmlAdapter} and {@code @XmlJavaTypeAdapter} to
  74  * customize the mapping of a {@code HashMap}.
  75  *
  76  * <p> <b> Step 1: </b> Determine the desired XML representation for HashMap.
  77  *
  78  * <pre>{@code
  79  *     <hashmap>
  80  *         <entry key="id123">this is a value</entry>
  81  *         <entry key="id312">this is another value</entry>
  82  *         ...
  83  *     </hashmap>
  84  * }</pre>
  85  *
  86  * <p> <b> Step 2: </b> Determine the schema definition that the
  87  * desired XML representation shown above should follow.
  88  *
  89  * <pre>{@code
  90  *
  91  *     <xs:complexType name="myHashMapType">
  92  *       <xs:sequence>
  93  *         <xs:element name="entry" type="myHashMapEntryType"
  94  *                        minOccurs = "0" maxOccurs="unbounded"/>


 137  *     public class Foo {
 138  *         @XmlJavaTypeAdapter(MyHashMapAdapter.class)
 139  *         HashMap hashmap;
 140  *         ...
 141  *     }
 142  * </pre>
 143  *
 144  * The above code fragment will map to the following schema:
 145  *
 146  * <pre>{@code
 147  *     <xs:complexType name="Foo">
 148  *       <xs:sequence>
 149  *         <xs:element name="hashmap" type="myHashMapType">
 150  *       </xs:sequence>
 151  *     </xs:complexType>
 152  * }</pre>
 153  *
 154  * @param <BoundType>
 155  *      The type that JAXB doesn't know how to handle. An adapter is written
 156  *      to allow this type to be used as an in-memory representation through
 157  *      the {@code ValueType}.
 158  * @param <ValueType>
 159  *      The type that JAXB knows how to handle out of the box.
 160  *
 161  * @author <ul><li>Sekhar Vajjhala, Sun Microsystems Inc.</li> <li> Kohsuke Kawaguchi, Sun Microsystems Inc.</li></ul>
 162  * @see XmlJavaTypeAdapter
 163  * @since 1.6, JAXB 2.0
 164  */
 165 public abstract class XmlAdapter<ValueType,BoundType> {
 166 
 167     /**
 168      * Do-nothing constructor for the derived classes.
 169      */
 170     protected XmlAdapter() {}
 171 
 172     /**
 173      * Convert a value type to a bound type.
 174      *
 175      * @param v
 176      *      The value to be converted. Can be null.
 177      * @throws Exception
< prev index next >