Save This Page
Home » openjdk-7 » javax » xml » bind » annotation » adapters » [javadoc | source]
    1   /*
    2    * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
    3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    4    *
    5    * This code is free software; you can redistribute it and/or modify it
    6    * under the terms of the GNU General Public License version 2 only, as
    7    * published by the Free Software Foundation.  Sun designates this
    8    * particular file as subject to the "Classpath" exception as provided
    9    * by Sun in the LICENSE file that accompanied this code.
   10    *
   11    * This code is distributed in the hope that it will be useful, but WITHOUT
   12    * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   13    * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   14    * version 2 for more details (a copy is included in the LICENSE file that
   15    * accompanied this code).
   16    *
   17    * You should have received a copy of the GNU General Public License version
   18    * 2 along with this work; if not, write to the Free Software Foundation,
   19    * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   20    *
   21    * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
   22    * CA 95054 USA or visit www.sun.com if you need additional information or
   23    * have any questions.
   24    */
   25   
   26   package javax.xml.bind.annotation.adapters;
   27   
   28   import javax.xml.bind.Unmarshaller;
   29   import javax.xml.bind.Marshaller;
   30   
   31   /**
   32    * Adapts a Java type for custom marshaling.
   33    *
   34    * <p> <b> Usage: </b> </p>
   35    *
   36    * <p>
   37    * Some Java types do not map naturally to a XML representation, for
   38    * example <tt>HashMap</tt> or other non JavaBean classes. Conversely,
   39    * a XML repsentation may map to a Java type but an application may
   40    * choose to accesss the XML representation using another Java
   41    * type. For example, the schema to Java binding rules bind
   42    * xs:DateTime by default to XmlGregorianCalendar. But an application
   43    * may desire to bind xs:DateTime to a custom type,
   44    * MyXmlGregorianCalendar, for example. In both cases, there is a
   45    * mismatch between <i> bound type </i>, used by an application to
   46    * access XML content and the <i> value type</i>, that is mapped to an
   47    * XML representation.
   48    *
   49    * <p>
   50    * This abstract class defines methods for adapting a bound type to a value
   51    * type or vice versa. The methods are invoked by the JAXB binding
   52    * framework during marshaling and unmarshalling:
   53    *
   54    * <ul>
   55    *   <li> <b> XmlAdapter.marshal(...): </b> During marshalling, JAXB
   56    *        binding framework invokes XmlAdapter.marshal(..) to adapt a
   57    *        bound type to value type, which is then marshaled to XML
   58    *        representation. </li>
   59    *
   60    *   <li> <b> XmlAdapter.unmarshal(...): </b> During unmarshalling,
   61    *        JAXB binding framework first unmarshals XML representation
   62    *        to a value type and then invokes XmlAdapter.unmarshal(..) to
   63    *        adapt the value type to a bound type. </li>
   64    * </ul>
   65    *
   66    * Writing an adapter therefore involves the following steps:
   67    *
   68    * <ul>
   69    *   <li> Write an adapter that implements this abstract class. </li>
   70    *   <li> Install the adapter using the annotation {@link
   71    *        XmlJavaTypeAdapter} </li>
   72    * </ul>
   73    *
   74    * <p><b>Example:</b> Customized mapping of <tt>HashMap</tt></p>
   75    * <p> The following example illustrates the use of
   76    * <tt>&#64;XmlAdapter</tt> and <tt>&#64;XmlJavaTypeAdapter</tt> to
   77    * customize the mapping of a <tt>HashMap</tt>.
   78    *
   79    * <p> <b> Step 1: </b> Determine the desired XML representation for HashMap.
   80    *
   81    * <pre>
   82    *     &lt;hashmap>
   83    *         &lt;entry key="id123">this is a value&lt;/entry>
   84    *         &lt;entry key="id312">this is another value&lt;/entry>
   85    *         ...
   86    *       &lt;/hashmap>
   87    * </pre>
   88    *
   89    * <p> <b> Step 2: </b> Determine the schema definition that the
   90    * desired XML representation shown above should follow.
   91    *
   92    * <pre>
   93    *
   94    *     &lt;xs:complexType name="myHashMapType">
   95    *       &lt;xs:sequence>
   96    *         &lt;xs:element name="entry" type="myHashMapEntryType"
   97    *                        minOccurs = "0" maxOccurs="unbounded"/>
   98    *       &lt;/xs:sequence>
   99    *     &lt;/xs:complexType>
  100    *
  101    *     &lt;xs:complexType name="myHashMapEntryType">
  102    *       &lt;xs:simpleContent>
  103    *         &lt;xs:extension base="xs:string">
  104    *           &lt;xs:attribute name="key" type="xs:int"/>
  105    *         &lt;/xs:extension>
  106    *       &lt;/xs:simpleContent>
  107    *     &lt;/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&lt;MyHashMapEntryType> entry;
  117    *     }
  118    *
  119    *     public class MyHashMapEntryType {
  120    *         &#64;XmlAttribute
  121    *         public Integer key;
  122    *
  123    *         &#64;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&lt;HashMap, MyHashMapType> { ... }
  134    *
  135    * </pre>
  136    *
  137    * <p> <b> Step 5: </b> Use the adapter.
  138    *
  139    * <pre>
  140    *     public class Foo {
  141    *         &#64;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    *     &lt;xs:complexType name="Foo">
  151    *       &lt;xs:sequence>
  152    *         &lt;xs:element name="hashmap" type="myHashMapType"
  153    *       &lt;/xs:sequence>
  154    *     &lt;/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        * Convert a value type to a bound type.
  177        *
  178        * @param v
  179        *      The value to be converted. Can be null.
  180        * @throws Exception
  181        *      if there's an error during the conversion. The caller is responsible for
  182        *      reporting the error to the user through {@link javax.xml.bind.ValidationEventHandler}.
  183        */
  184       public abstract BoundType unmarshal(ValueType v) throws Exception;
  185   
  186       /**
  187        * Convert a bound type to a value type.
  188        *
  189        * @param v
  190        *      The value to be convereted. Can be null.
  191        * @throws Exception
  192        *      if there's an error during the conversion. The caller is responsible for
  193        *      reporting the error to the user through {@link javax.xml.bind.ValidationEventHandler}.
  194        */
  195       public abstract ValueType marshal(BoundType v) throws Exception;
  196   }

Save This Page
Home » openjdk-7 » javax » xml » bind » annotation » adapters » [javadoc | source]