KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > xml > bind > annotation > adapters > XmlAdapter


1 /*
2  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
3  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
4  */

5
6 package javax.xml.bind.annotation.adapters;
7
8 /**
9  * Adapts a Java type for custom marshaling.
10  *
11  * <p> <b> Usage: </b> </p>
12  *
13  * <p>
14  * Some Java types do not map naturally to a XML representation, for
15  * example <tt>HashMap</tt> or other non JavaBean classes. Conversely,
16  * a XML repsentation may map to a Java type but an application may
17  * choose to accesss the XML representation using another Java
18  * type. For example, the schema to Java binding rules bind
19  * xs:DateTime by default to XmlGregorianCalendar. But an application
20  * may desire to bind xs:DateTime to a custom type,
21  * MyXmlGregorianCalendar, for example. In both cases, there is a
22  * mismatch between <i> bound type </i>, used by an application to
23  * access XML content and the <i> value type</i>, that is mapped to an
24  * XML representation.
25  *
26  * <p>
27  * This abstract class defines methods for adapting a bound type to a value
28  * type or vice versa. The methods are invoked by the JAXB binding
29  * framework during marshaling and unmarshalling:
30  *
31  * <ul>
32  * <li> <b> XmlAdapter.marshal(...): </b> During marshalling, JAXB
33  * binding framework invokes XmlAdapter.marshal(..) to adapt a
34  * bound type to value type, which is then marshaled to XML
35  * representation. </li>
36  *
37  * <li> <b> XmlAdapter.unmarshal(...): </b> During unmarshalling,
38  * JAXB binding framework first unmarshals XML representation
39  * to a value type and then invokes XmlAdapter.unmarshal(..) to
40  * adapt the value type to a bound type. </li>
41  * </ul>
42  *
43  * Writing an adapter therefore involves the following steps:
44  *
45  * <ul>
46  * <li> Write an adapter that implements this abstract class. </li>
47  * <li> Install the adapter using the annotation {@link
48  * XmlJavaTypeAdapter} </li>
49  * </ul>
50  *
51  * <p><b>Example:</b> Customized mapping of </tt>HashMap</tt></p>
52  * <p> The following example illustrates the use of
53  * <tt>&#64;XmlAdapter</tt> and <tt>&#64;XmlJavaTypeAdapter</tt> to
54  * customize the mapping of a <tt>HashMap</tt>.
55  *
56  * <p> <b> Step 1: </b> Determine the desired XML representation for HashMap.
57  *
58  * <pre>
59  * &lt;hashmap>
60  * &lt;entry key="id123">this is a value&lt;/entry>
61  * &lt;entry key="id312">this is another value&lt;/entry>
62  * ...
63  * &lt;/hashmap>
64  * </pre>
65  *
66  * <p> <b> Step 2: </b> Determine the schema definition that the
67  * desired XML representation shown above should follow.
68  *
69  * <pre>
70  *
71  * &lt;xs:complexType name="myHashMapType">
72  * &lt;xs:sequence>
73  * &lt;xs:element name="entry" type="myHashMapEntryType"
74  * minOccurs = "0" maxOccurs="unbounded"/>
75  * &lt;/xs:sequence>
76  * &lt;/xs:complexType>
77  *
78  * &lt;xs:complexType name="myHashMapEntryType">
79  * &lt;xs:simpleContent>
80  * &lt;xs:extension base="xs:string">
81  * &lt;xs:attribute name="key" type="xs:int"/>
82  * &lt;/xs:extension>
83  * &lt;/xs:simpleContent>
84  * &lt;/xs:complexType>
85  *
86  * </pre>
87  *
88  * <p> <b> Step 3: </b> Write value types that can generate the above
89  * schema definition.
90  *
91  * <pre>
92  * public class MyHashMapType {
93  * List&lt;MyHashMapEntryType> entry;
94  * }
95  *
96  * public class MyHashMapEntryType {
97  * &#64;XmlAttribute
98  * public Integer key;
99  *
100  * &#64;XmlValue
101  * public String value;
102  * }
103  * </pre>
104  *
105  * <p> <b> Step 4: </b> Write the adapter that adapts the value type,
106  * MyHashMapType to a bound type, HashMap, used by the application.
107  *
108  * <pre>
109  * public final class MyHashMapAdapter extends
110  * XmlAdapter&lt;MyHashMapType,HashMap> { ... }
111  *
112  * </pre>
113  *
114  * <p> <b> Step 5: </b> Use the adapter.
115  *
116  * <pre>
117  * public class Foo {
118  * &#64;XmlJavaTypeAdapter(MyHashMapAdapter.class)
119  * HashMap hashmap;
120  * ...
121  * }
122  * </pre>
123  *
124  * The above code fragment will map to the following schema:
125  *
126  * <pre>
127  * &lt;xs:complexType name="Foo">
128  * &lt;xs:sequence>
129  * &lt;xs:element name="hashmap" type="myHashMapType"
130  * &lt;/xs:sequence>
131  * &lt;/xs:complexType>
132  * </pre>
133  *
134  * @param <BoundType>
135  * The type that JAXB doesn't know how to handle. An adapter is written
136  * to allow this type to be used as an in-memory representation through
137  * the <tt>ValueType</tt>.
138  * @param <ValueType>
139  * The type that JAXB knows how to handle out of the box.
140  *
141  * @author <ul><li>Sekhar Vajjhala, Sun Microsystems Inc.</li> <li> Kohsuke Kawaguchi, Sun Microsystems Inc.</li></ul>
142  * @see XmlJavaTypeAdapter
143  * @since JAXB 2.0
144  */

145 public abstract class XmlAdapter<ValueType,BoundType> {
146
147     /**
148      * Do-nothing constructor for the derived classes.
149      */

150     protected XmlAdapter() {}
151
152     /**
153      * Convert a value type to a bound type.
154      *
155      * @param v
156      * The value to be converted. Can be null.
157      * @throws Exception
158      * if there's an error during the conversion. The caller is responsible for
159      * reporting the error to the user through {@link javax.xml.bind.ValidationEventHandler}.
160      */

161     public abstract BoundType unmarshal(ValueType v) throws Exception JavaDoc;
162
163     /**
164      * Convert a bound type to a value type.
165      *
166      * @param v
167      * The value to be convereted. Can be null.
168      * @throws Exception
169      * if there's an error during the conversion. The caller is responsible for
170      * reporting the error to the user through {@link javax.xml.bind.ValidationEventHandler}.
171      */

172     public abstract ValueType marshal(BoundType v) throws Exception JavaDoc;
173 }
174
Popular Tags