KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jibx > extras > Dom4JElementMapper


1 /*
2 Copyright (c) 2004, Dennis M. Sosnoski
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8  * Redistributions of source code must retain the above copyright notice, this
9    list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice,
11    this list of conditions and the following disclaimer in the documentation
12    and/or other materials provided with the distribution.
13  * Neither the name of JiBX nor the names of its contributors may be used
14    to endorse or promote products derived from this software without specific
15    prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */

28
29 package org.jibx.extras;
30
31 import java.io.IOException JavaDoc;
32
33 import org.dom4j.Element;
34 import org.jibx.runtime.IAliasable;
35 import org.jibx.runtime.IMarshaller;
36 import org.jibx.runtime.IMarshallingContext;
37 import org.jibx.runtime.IUnmarshaller;
38 import org.jibx.runtime.IUnmarshallingContext;
39 import org.jibx.runtime.JiBXException;
40 import org.jibx.runtime.impl.UnmarshallingContext;
41
42 /**
43  * <p>Custom element marshaller/unmarshaller to dom4j representation. This
44  * allows you to mix data binding and document model representations for XML
45  * within the same application. You simply use this marshaller/unmarshaller with
46  * a linked object type of <code>org.dom4j.Element</code> (the actual runtime
47  * type - the declared type is ignored and can be anything). If a name is
48  * supplied on a reference that element name will always be matched when
49  * unmarshalling but will be ignored when marshalling (with the actual dom4j
50  * element name used). If no name is supplied this will unmarshal a single
51  * element with any name.</p>
52  *
53  * @author Dennis M. Sosnoski
54  * @version 1.0
55  */

56
57 public class Dom4JElementMapper extends Dom4JMapperBase
58     implements IMarshaller, IUnmarshaller, IAliasable
59 {
60     /** Root element namespace URI. */
61     private final String JavaDoc m_uri;
62     
63     /** Namespace URI index in binding. */
64     private final int m_index;
65     
66     /** Root element name. */
67     private final String JavaDoc m_name;
68     
69     /**
70      * Default constructor.
71      */

72     
73     public Dom4JElementMapper() {
74         m_uri = null;
75         m_index = -1;
76         m_name = null;
77     }
78     
79     /**
80      * Aliased constructor. This takes a name definition for the element. It'll
81      * be used by JiBX when a name is supplied by the mapping which references
82      * this custom marshaller/unmarshaller.
83      *
84      * @param uri namespace URI for the top-level element
85      * @param index namespace index corresponding to the defined URI within the
86      * marshalling context definitions
87      * @param name local name for the top-level element
88      */

89     
90     public Dom4JElementMapper(String JavaDoc uri, int index, String JavaDoc name) {
91         
92         // save the simple values
93
m_uri = uri;
94         m_index = index;
95         m_name = name;
96     }
97     
98     /* (non-Javadoc)
99      * @see org.jibx.runtime.IMarshaller#isExtension(int)
100      */

101     
102     public boolean isExtension(int index) {
103         return false;
104     }
105
106     /* (non-Javadoc)
107      * @see org.jibx.runtime.IMarshaller#marshal(java.lang.Object,
108      * org.jibx.runtime.IMarshallingContext)
109      */

110     
111     public void marshal(Object JavaDoc obj, IMarshallingContext ictx)
112         throws JiBXException {
113         
114         // make sure the parameters are as expected
115
if (!(obj instanceof Element)) {
116             throw new JiBXException("Mapped object not an org.dom4j.Element");
117         } else {
118             try {
119                     
120                 // marshal element and all content with only leading indentation
121
m_xmlWriter = ictx.getXmlWriter();
122                 m_xmlWriter.indent();
123                 int indent = ictx.getIndent();
124                 ictx.setIndent(-1);
125                 m_defaultNamespaceURI = null;
126                 marshalElement((Element)obj);
127                 ictx.setIndent(indent);
128                 
129             } catch (IOException JavaDoc e) {
130                 throw new JiBXException("Error writing to document", e);
131             }
132         }
133     }
134
135     /* (non-Javadoc)
136      * @see org.jibx.runtime.IUnmarshaller#isPresent(org.jibx.runtime.IUnmarshallingContext)
137      */

138      
139     public boolean isPresent(IUnmarshallingContext ctx) throws JiBXException {
140         if (m_name == null) {
141             if (!(ctx instanceof UnmarshallingContext)) {
142                 throw new JiBXException
143                     ("Unmarshalling context not of expected type");
144             } else {
145                 return !((UnmarshallingContext)ctx).isEnd();
146             }
147         } else {
148             return ctx.isAt(m_uri, m_name);
149         }
150     }
151
152     /* (non-Javadoc)
153      * @see org.jibx.runtime.IUnmarshaller#unmarshal(java.lang.Object,
154      * org.jibx.runtime.IUnmarshallingContext)
155      */

156      
157     public Object JavaDoc unmarshal(Object JavaDoc obj, IUnmarshallingContext ictx)
158         throws JiBXException {
159         
160         // verify the entry conditions
161
if (!(ictx instanceof UnmarshallingContext)) {
162             throw new JiBXException
163                 ("Unmarshalling context not of expected type");
164         } else if (m_name != null && !ictx.isAt(m_uri, m_name)) {
165             ((UnmarshallingContext)ictx).throwStartTagNameError(m_uri, m_name);
166         }
167         
168         // position to element start tag
169
m_unmarshalContext = (UnmarshallingContext)ictx;
170         m_unmarshalContext.toStart();
171         
172         // unmarshal element to document model
173
try {
174             return unmarshalElement();
175         } catch (IOException JavaDoc e) {
176             throw new JiBXException("Error reading from document", e);
177         }
178     }
179 }
Popular Tags