001 /*
002 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.xml.bind.annotation.adapters;
027
028 import javax.xml.bind.Unmarshaller;
029 import javax.xml.bind.Marshaller;
030
031 /**
032 * Adapts a Java type for custom marshaling.
033 *
034 * <p> <b> Usage: </b> </p>
035 *
036 * <p>
037 * Some Java types do not map naturally to a XML representation, for
038 * example <tt>HashMap</tt> or other non JavaBean classes. Conversely,
039 * a XML repsentation may map to a Java type but an application may
040 * choose to accesss the XML representation using another Java
041 * type. For example, the schema to Java binding rules bind
042 * xs:DateTime by default to XmlGregorianCalendar. But an application
043 * may desire to bind xs:DateTime to a custom type,
044 * MyXmlGregorianCalendar, for example. In both cases, there is a
045 * mismatch between <i> bound type </i>, used by an application to
046 * access XML content and the <i> value type</i>, that is mapped to an
047 * XML representation.
048 *
049 * <p>
050 * This abstract class defines methods for adapting a bound type to a value
051 * type or vice versa. The methods are invoked by the JAXB binding
052 * framework during marshaling and unmarshalling:
053 *
054 * <ul>
055 * <li> <b> XmlAdapter.marshal(...): </b> During marshalling, JAXB
056 * binding framework invokes XmlAdapter.marshal(..) to adapt a
057 * bound type to value type, which is then marshaled to XML
058 * representation. </li>
059 *
060 * <li> <b> XmlAdapter.unmarshal(...): </b> During unmarshalling,
061 * JAXB binding framework first unmarshals XML representation
062 * to a value type and then invokes XmlAdapter.unmarshal(..) to
063 * adapt the value type to a bound type. </li>
064 * </ul>
065 *
066 * Writing an adapter therefore involves the following steps:
067 *
068 * <ul>
069 * <li> Write an adapter that implements this abstract class. </li>
070 * <li> Install the adapter using the annotation {@link
071 * XmlJavaTypeAdapter} </li>
072 * </ul>
073 *
074 * <p><b>Example:</b> Customized mapping of <tt>HashMap</tt></p>
075 * <p> The following example illustrates the use of
076 * <tt>@XmlAdapter</tt> and <tt>@XmlJavaTypeAdapter</tt> to
077 * customize the mapping of a <tt>HashMap</tt>.
078 *
079 * <p> <b> Step 1: </b> Determine the desired XML representation for HashMap.
080 *
081 * <pre>
082 * <hashmap>
083 * <entry key="id123">this is a value</entry>
084 * <entry key="id312">this is another value</entry>
085 * ...
086 * </hashmap>
087 * </pre>
088 *
089 * <p> <b> Step 2: </b> Determine the schema definition that the
090 * desired XML representation shown above should follow.
091 *
092 * <pre>
093 *
094 * <xs:complexType name="myHashMapType">
095 * <xs:sequence>
096 * <xs:element name="entry" type="myHashMapEntryType"
097 * minOccurs = "0" maxOccurs="unbounded"/>
098 * </xs:sequence>
099 * </xs:complexType>
100 *
101 * <xs:complexType name="myHashMapEntryType">
102 * <xs:simpleContent>
103 * <xs:extension base="xs:string">
104 * <xs:attribute name="key" type="xs:int"/>
105 * </xs:extension>
106 * </xs:simpleContent>
107 * </xs:complexType>
108 *
109 * </pre>
110 *
111 * <p> <b> Step 3: </b> Write value types that can generate the above
112 * schema definition.
113 *
114 * <pre>
115 * public class MyHashMapType {
116 * List<MyHashMapEntryType> entry;
117 * }
118 *
119 * public class MyHashMapEntryType {
120 * @XmlAttribute
121 * public Integer key;
122 *
123 * @XmlValue
124 * public String value;
125 * }
126 * </pre>
127 *
128 * <p> <b> Step 4: </b> Write the adapter that adapts the value type,
129 * MyHashMapType to a bound type, HashMap, used by the application.
130 *
131 * <pre>
132 * public final class MyHashMapAdapter extends
133 * XmlAdapter<HashMap, MyHashMapType> { ... }
134 *
135 * </pre>
136 *
137 * <p> <b> Step 5: </b> Use the adapter.
138 *
139 * <pre>
140 * public class Foo {
141 * @XmlJavaTypeAdapter(MyHashMapAdapter.class)
142 * HashMap hashmap;
143 * ...
144 * }
145 * </pre>
146 *
147 * The above code fragment will map to the following schema:
148 *
149 * <pre>
150 * <xs:complexType name="Foo">
151 * <xs:sequence>
152 * <xs:element name="hashmap" type="myHashMapType"
153 * </xs:sequence>
154 * </xs:complexType>
155 * </pre>
156 *
157 * @param <BoundType>
158 * The type that JAXB doesn't know how to handle. An adapter is written
159 * to allow this type to be used as an in-memory representation through
160 * the <tt>ValueType</tt>.
161 * @param <ValueType>
162 * The type that JAXB knows how to handle out of the box.
163 *
164 * @author <ul><li>Sekhar Vajjhala, Sun Microsystems Inc.</li> <li> Kohsuke Kawaguchi, Sun Microsystems Inc.</li></ul>
165 * @see XmlJavaTypeAdapter
166 * @since JAXB 2.0
167 */
168 public abstract class XmlAdapter<ValueType, BoundType> {
169
170 /**
171 * Do-nothing constructor for the derived classes.
172 */
173 protected XmlAdapter() {
174 }
175
176 /**
177 * Convert a value type to a bound type.
178 *
179 * @param v
180 * The value to be converted. Can be null.
181 * @throws Exception
182 * if there's an error during the conversion. The caller is responsible for
183 * reporting the error to the user through {@link javax.xml.bind.ValidationEventHandler}.
184 */
185 public abstract BoundType unmarshal(ValueType v) throws Exception;
186
187 /**
188 * Convert a bound type to a value type.
189 *
190 * @param v
191 * The value to be convereted. Can be null.
192 * @throws Exception
193 * if there's an error during the conversion. The caller is responsible for
194 * reporting the error to the user through {@link javax.xml.bind.ValidationEventHandler}.
195 */
196 public abstract ValueType marshal(BoundType v) throws Exception;
197 }
|