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