XmlAdapter.java revision 325
325N/A * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. 325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 325N/A * This code is free software; you can redistribute it and/or modify it 325N/A * under the terms of the GNU General Public License version 2 only, as 325N/A * published by the Free Software Foundation. Oracle designates this 325N/A * particular file as subject to the "Classpath" exception as provided 325N/A * by Oracle in the LICENSE file that accompanied this code. 325N/A * This code is distributed in the hope that it will be useful, but WITHOUT 325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 325N/A * version 2 for more details (a copy is included in the LICENSE file that 325N/A * accompanied this code). 325N/A * You should have received a copy of the GNU General Public License version 325N/A * 2 along with this work; if not, write to the Free Software Foundation, 325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 325N/A * or visit www.oracle.com if you need additional information or have any 325N/A * Adapts a Java type for custom marshaling. 325N/A * <p> <b> Usage: </b> </p> 325N/A * Some Java types do not map naturally to a XML representation, for 325N/A * example <tt>HashMap</tt> or other non JavaBean classes. Conversely, 325N/A * a XML repsentation may map to a Java type but an application may 325N/A * choose to accesss the XML representation using another Java 325N/A * type. For example, the schema to Java binding rules bind 325N/A * xs:DateTime by default to XmlGregorianCalendar. But an application 325N/A * may desire to bind xs:DateTime to a custom type, 325N/A * MyXmlGregorianCalendar, for example. In both cases, there is a 325N/A * mismatch between <i> bound type </i>, used by an application to 325N/A * access XML content and the <i> value type</i>, that is mapped to an 325N/A * This abstract class defines methods for adapting a bound type to a value 325N/A * type or vice versa. The methods are invoked by the JAXB binding 325N/A * framework during marshaling and unmarshalling: 325N/A * <li> <b> XmlAdapter.marshal(...): </b> During marshalling, JAXB 325N/A * binding framework invokes XmlAdapter.marshal(..) to adapt a 325N/A * bound type to value type, which is then marshaled to XML 325N/A * representation. </li> 325N/A * <li> <b> XmlAdapter.unmarshal(...): </b> During unmarshalling, 325N/A * JAXB binding framework first unmarshals XML representation 325N/A * to a value type and then invokes XmlAdapter.unmarshal(..) to 325N/A * adapt the value type to a bound type. </li> 325N/A * Writing an adapter therefore involves the following steps: 325N/A * <li> Write an adapter that implements this abstract class. </li> 325N/A * <li> Install the adapter using the annotation {@link 325N/A * XmlJavaTypeAdapter} </li> 325N/A * <p><b>Example:</b> Customized mapping of <tt>HashMap</tt></p> 325N/A * <p> The following example illustrates the use of 325N/A * <tt>@XmlAdapter</tt> and <tt>@XmlJavaTypeAdapter</tt> to 325N/A * customize the mapping of a <tt>HashMap</tt>. 325N/A * <p> <b> Step 1: </b> Determine the desired XML representation for HashMap. 325N/A * <entry key="id123">this is a value</entry> 325N/A * <entry key="id312">this is another value</entry> 325N/A * <p> <b> Step 2: </b> Determine the schema definition that the 325N/A * desired XML representation shown above should follow. 325N/A * <xs:complexType name="myHashMapType"> 325N/A * <xs:element name="entry" type="myHashMapEntryType" 325N/A * minOccurs = "0" maxOccurs="unbounded"/> 325N/A * <xs:complexType name="myHashMapEntryType"> 325N/A * <xs:simpleContent> 325N/A * <xs:extension base="xs:string"> 325N/A * <xs:attribute name="key" type="xs:int"/> 325N/A * </xs:simpleContent> 325N/A * <p> <b> Step 3: </b> Write value types that can generate the above 325N/A * public class MyHashMapType { 325N/A * List<MyHashMapEntryType> entry; 325N/A * public class MyHashMapEntryType { 325N/A * <p> <b> Step 4: </b> Write the adapter that adapts the value type, 325N/A * MyHashMapType to a bound type, HashMap, used by the application. 325N/A * public final class MyHashMapAdapter extends 325N/A * XmlAdapter<MyHashMapType,HashMap> { ... } 325N/A * <p> <b> Step 5: </b> Use the adapter. 325N/A * @XmlJavaTypeAdapter(MyHashMapAdapter.class) 325N/A * The above code fragment will map to the following schema: 325N/A * <xs:complexType name="Foo"> 325N/A * <xs:element name="hashmap" type="myHashMapType" 325N/A * The type that JAXB doesn't know how to handle. An adapter is written 325N/A * to allow this type to be used as an in-memory representation through 325N/A * the <tt>ValueType</tt>. 325N/A * The type that JAXB knows how to handle out of the box. 325N/A * @author <ul><li>Sekhar Vajjhala, Sun Microsystems Inc.</li> <li> Kohsuke Kawaguchi, Sun Microsystems Inc.</li></ul> 325N/A * @see XmlJavaTypeAdapter 325N/A * Do-nothing constructor for the derived classes. 325N/A * Convert a value type to a bound type. 325N/A * The value to be converted. Can be null. 325N/A * if there's an error during the conversion. The caller is responsible for 325N/A * reporting the error to the user through {@link javax.xml.bind.ValidationEventHandler}. 325N/A * Convert a bound type to a value type. 325N/A * The value to be convereted. Can be null. 325N/A * if there's an error during the conversion. The caller is responsible for 325N/A * reporting the error to the user through {@link javax.xml.bind.ValidationEventHandler}.