Source Code Cross Referenced for XmlAdapter.java in  » 6.0-JDK-Core » xml » javax » xml » bind » annotation » adapters » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » xml » javax.xml.bind.annotation.adapters 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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>&#64;XmlAdapter</tt> and <tt>&#64;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         *     &lt;hashmap>
083         *         &lt;entry key="id123">this is a value&lt;/entry>
084         *         &lt;entry key="id312">this is another value&lt;/entry>
085         *         ...
086         *       &lt;/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         *     &lt;xs:complexType name="myHashMapType">
095         *       &lt;xs:sequence>
096         *         &lt;xs:element name="entry" type="myHashMapEntryType"
097         *                        minOccurs = "0" maxOccurs="unbounded"/>
098         *       &lt;/xs:sequence>
099         *     &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            /**
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        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.