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

Fri, 04 Oct 2013 16:21:34 +0100

author
mkos
date
Fri, 04 Oct 2013 16:21:34 +0100
changeset 408
b0610cd08440
parent 368
0989ad8c0860
child 637
9c07ef4934dd
permissions
-rw-r--r--

8025054: Update JAX-WS RI integration to 2.2.9-b130926.1035
Reviewed-by: chegar

ohair@286 1 /*
alanb@368 2 * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package javax.xml.bind.annotation.adapters;
ohair@286 27
ohair@286 28 /**
ohair@286 29 * Adapts a Java type for custom marshaling.
ohair@286 30 *
ohair@286 31 * <p> <b> Usage: </b> </p>
ohair@286 32 *
ohair@286 33 * <p>
ohair@286 34 * Some Java types do not map naturally to a XML representation, for
ohair@286 35 * example <tt>HashMap</tt> or other non JavaBean classes. Conversely,
ohair@286 36 * a XML repsentation may map to a Java type but an application may
ohair@286 37 * choose to accesss the XML representation using another Java
ohair@286 38 * type. For example, the schema to Java binding rules bind
ohair@286 39 * xs:DateTime by default to XmlGregorianCalendar. But an application
ohair@286 40 * may desire to bind xs:DateTime to a custom type,
ohair@286 41 * MyXmlGregorianCalendar, for example. In both cases, there is a
ohair@286 42 * mismatch between <i> bound type </i>, used by an application to
ohair@286 43 * access XML content and the <i> value type</i>, that is mapped to an
ohair@286 44 * XML representation.
ohair@286 45 *
ohair@286 46 * <p>
ohair@286 47 * This abstract class defines methods for adapting a bound type to a value
ohair@286 48 * type or vice versa. The methods are invoked by the JAXB binding
ohair@286 49 * framework during marshaling and unmarshalling:
ohair@286 50 *
ohair@286 51 * <ul>
ohair@286 52 * <li> <b> XmlAdapter.marshal(...): </b> During marshalling, JAXB
ohair@286 53 * binding framework invokes XmlAdapter.marshal(..) to adapt a
ohair@286 54 * bound type to value type, which is then marshaled to XML
ohair@286 55 * representation. </li>
ohair@286 56 *
ohair@286 57 * <li> <b> XmlAdapter.unmarshal(...): </b> During unmarshalling,
ohair@286 58 * JAXB binding framework first unmarshals XML representation
ohair@286 59 * to a value type and then invokes XmlAdapter.unmarshal(..) to
ohair@286 60 * adapt the value type to a bound type. </li>
ohair@286 61 * </ul>
ohair@286 62 *
ohair@286 63 * Writing an adapter therefore involves the following steps:
ohair@286 64 *
ohair@286 65 * <ul>
ohair@286 66 * <li> Write an adapter that implements this abstract class. </li>
ohair@286 67 * <li> Install the adapter using the annotation {@link
ohair@286 68 * XmlJavaTypeAdapter} </li>
ohair@286 69 * </ul>
ohair@286 70 *
ohair@286 71 * <p><b>Example:</b> Customized mapping of <tt>HashMap</tt></p>
ohair@286 72 * <p> The following example illustrates the use of
ohair@286 73 * <tt>&#64;XmlAdapter</tt> and <tt>&#64;XmlJavaTypeAdapter</tt> to
ohair@286 74 * customize the mapping of a <tt>HashMap</tt>.
ohair@286 75 *
ohair@286 76 * <p> <b> Step 1: </b> Determine the desired XML representation for HashMap.
ohair@286 77 *
ohair@286 78 * <pre>
ohair@286 79 * &lt;hashmap>
ohair@286 80 * &lt;entry key="id123">this is a value&lt;/entry>
ohair@286 81 * &lt;entry key="id312">this is another value&lt;/entry>
ohair@286 82 * ...
ohair@286 83 * &lt;/hashmap>
ohair@286 84 * </pre>
ohair@286 85 *
ohair@286 86 * <p> <b> Step 2: </b> Determine the schema definition that the
ohair@286 87 * desired XML representation shown above should follow.
ohair@286 88 *
ohair@286 89 * <pre>
ohair@286 90 *
ohair@286 91 * &lt;xs:complexType name="myHashMapType">
ohair@286 92 * &lt;xs:sequence>
ohair@286 93 * &lt;xs:element name="entry" type="myHashMapEntryType"
ohair@286 94 * minOccurs = "0" maxOccurs="unbounded"/>
ohair@286 95 * &lt;/xs:sequence>
ohair@286 96 * &lt;/xs:complexType>
ohair@286 97 *
ohair@286 98 * &lt;xs:complexType name="myHashMapEntryType">
ohair@286 99 * &lt;xs:simpleContent>
ohair@286 100 * &lt;xs:extension base="xs:string">
ohair@286 101 * &lt;xs:attribute name="key" type="xs:int"/>
ohair@286 102 * &lt;/xs:extension>
ohair@286 103 * &lt;/xs:simpleContent>
ohair@286 104 * &lt;/xs:complexType>
ohair@286 105 *
ohair@286 106 * </pre>
ohair@286 107 *
ohair@286 108 * <p> <b> Step 3: </b> Write value types that can generate the above
ohair@286 109 * schema definition.
ohair@286 110 *
ohair@286 111 * <pre>
ohair@286 112 * public class MyHashMapType {
ohair@286 113 * List&lt;MyHashMapEntryType> entry;
ohair@286 114 * }
ohair@286 115 *
ohair@286 116 * public class MyHashMapEntryType {
ohair@286 117 * &#64;XmlAttribute
ohair@286 118 * public Integer key;
ohair@286 119 *
ohair@286 120 * &#64;XmlValue
ohair@286 121 * public String value;
ohair@286 122 * }
ohair@286 123 * </pre>
ohair@286 124 *
ohair@286 125 * <p> <b> Step 4: </b> Write the adapter that adapts the value type,
ohair@286 126 * MyHashMapType to a bound type, HashMap, used by the application.
ohair@286 127 *
ohair@286 128 * <pre>
ohair@286 129 * public final class MyHashMapAdapter extends
ohair@286 130 * XmlAdapter&lt;MyHashMapType,HashMap> { ... }
ohair@286 131 *
ohair@286 132 * </pre>
ohair@286 133 *
ohair@286 134 * <p> <b> Step 5: </b> Use the adapter.
ohair@286 135 *
ohair@286 136 * <pre>
ohair@286 137 * public class Foo {
ohair@286 138 * &#64;XmlJavaTypeAdapter(MyHashMapAdapter.class)
ohair@286 139 * HashMap hashmap;
ohair@286 140 * ...
ohair@286 141 * }
ohair@286 142 * </pre>
ohair@286 143 *
ohair@286 144 * The above code fragment will map to the following schema:
ohair@286 145 *
ohair@286 146 * <pre>
ohair@286 147 * &lt;xs:complexType name="Foo">
ohair@286 148 * &lt;xs:sequence>
ohair@286 149 * &lt;xs:element name="hashmap" type="myHashMapType"
ohair@286 150 * &lt;/xs:sequence>
ohair@286 151 * &lt;/xs:complexType>
ohair@286 152 * </pre>
ohair@286 153 *
ohair@286 154 * @param <BoundType>
ohair@286 155 * The type that JAXB doesn't know how to handle. An adapter is written
ohair@286 156 * to allow this type to be used as an in-memory representation through
ohair@286 157 * the <tt>ValueType</tt>.
ohair@286 158 * @param <ValueType>
ohair@286 159 * The type that JAXB knows how to handle out of the box.
ohair@286 160 *
ohair@286 161 * @author <ul><li>Sekhar Vajjhala, Sun Microsystems Inc.</li> <li> Kohsuke Kawaguchi, Sun Microsystems Inc.</li></ul>
ohair@286 162 * @see XmlJavaTypeAdapter
ohair@286 163 * @since JAXB 2.0
ohair@286 164 */
ohair@286 165 public abstract class XmlAdapter<ValueType,BoundType> {
ohair@286 166
ohair@286 167 /**
ohair@286 168 * Do-nothing constructor for the derived classes.
ohair@286 169 */
ohair@286 170 protected XmlAdapter() {}
ohair@286 171
ohair@286 172 /**
ohair@286 173 * Convert a value type to a bound type.
ohair@286 174 *
ohair@286 175 * @param v
ohair@286 176 * The value to be converted. Can be null.
ohair@286 177 * @throws Exception
ohair@286 178 * if there's an error during the conversion. The caller is responsible for
ohair@286 179 * reporting the error to the user through {@link javax.xml.bind.ValidationEventHandler}.
ohair@286 180 */
ohair@286 181 public abstract BoundType unmarshal(ValueType v) throws Exception;
ohair@286 182
ohair@286 183 /**
ohair@286 184 * Convert a bound type to a value type.
ohair@286 185 *
ohair@286 186 * @param v
ohair@286 187 * The value to be convereted. Can be null.
ohair@286 188 * @throws Exception
ohair@286 189 * if there's an error during the conversion. The caller is responsible for
ohair@286 190 * reporting the error to the user through {@link javax.xml.bind.ValidationEventHandler}.
ohair@286 191 */
ohair@286 192 public abstract ValueType marshal(BoundType v) throws Exception;
ohair@286 193 }

mercurial