KickJava   Java API By Example, From Geeks To Geeks.

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


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

55
56 public class DomElementMapper extends DomMapperBase
57     implements IMarshaller, IUnmarshaller, IAliasable
58 {
59     /** Root element namespace URI. */
60     private final String JavaDoc m_uri;
61     
62     /** Namespace URI index in binding. */
63     private final int m_index;
64     
65     /** Root element name. */
66     private final String JavaDoc m_name;
67     
68     /**
69      * Default constructor.
70      *
71      * @throws JiBXException on error creating document
72      */

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

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

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

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

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

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