KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.ArrayList JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35
36 import org.jibx.runtime.IMarshaller;
37 import org.jibx.runtime.IMarshallingContext;
38 import org.jibx.runtime.IUnmarshaller;
39 import org.jibx.runtime.IUnmarshallingContext;
40 import org.jibx.runtime.JiBXException;
41 import org.jibx.runtime.impl.UnmarshallingContext;
42 import org.w3c.dom.Node JavaDoc;
43
44 /**
45  * <p>Custom content list marshaller/unmarshaller to DOM representation. This
46  * allows you to mix data binding and document model representations for XML
47  * within the same application. You simply use this marshaller/unmarshaller with
48  * a linked object type that implements <code>java.util.List</code> (the actual
49  * runtime type - the declared type is ignored and can be anything). When
50  * unmarshalling it will create an instance of <code>java.util.ArrayList</code>
51  * if a list is not passed in and any content is present, then return all the
52  * content up to the close tag for the enclosing element in the list. When
53  * marshalling, it will simply write out any content directly.</p>
54  *
55  * @author Dennis M. Sosnoski
56  * @version 1.0
57  */

58
59 public class DomListMapper extends DomMapperBase
60     implements IMarshaller, IUnmarshaller
61 {
62     /**
63      * Default constructor.
64      *
65      * @throws JiBXException on configuration error
66      */

67     public DomListMapper() throws JiBXException {
68         super();
69     }
70     
71     /* (non-Javadoc)
72      * @see org.jibx.runtime.IMarshaller#isExtension(int)
73      */

74
75     public boolean isExtension(int index) {
76         return false;
77     }
78
79     /* (non-Javadoc)
80      * @see org.jibx.runtime.IMarshaller#marshal(java.lang.Object,
81      * org.jibx.runtime.IMarshallingContext)
82      */

83     
84     public void marshal(Object JavaDoc obj, IMarshallingContext ictx)
85         throws JiBXException {
86         
87         // make sure the parameters are as expected
88
if (!(obj instanceof List JavaDoc)) {
89             throw new JiBXException("Mapped object not a java.util.List");
90         } else {
91             try {
92                     
93                 // marshal content list with no indentation
94
m_xmlWriter = ictx.getXmlWriter();
95                 int indent = ictx.getIndent();
96                 ictx.setIndent(-1);
97                 m_defaultNamespaceURI = null;
98                 for (Iterator JavaDoc iter = ((List JavaDoc)obj).iterator(); iter.hasNext();) {
99                     Object JavaDoc item = iter.next();
100                     if (item instanceof Node JavaDoc) {
101                         marshalNode((Node JavaDoc)item);
102                     } else {
103                         throw new JiBXException
104                             ("List item is not an org.w3c.dom.Node");
105                     }
106                 }
107                 ictx.setIndent(indent);
108                 
109             } catch (IOException JavaDoc e) {
110                 throw new JiBXException("Error writing to document", e);
111             }
112         }
113     }
114
115     /* (non-Javadoc)
116      * @see org.jibx.runtime.IUnmarshaller#isPresent(org.jibx.runtime.IUnmarshallingContext)
117      */

118      
119     public boolean isPresent(IUnmarshallingContext ctx) throws JiBXException {
120         if (!(ctx instanceof UnmarshallingContext)) {
121             throw new JiBXException
122                 ("Unmarshalling context not of expected type");
123         } else {
124             return ((UnmarshallingContext)ctx).currentEvent() !=
125                 UnmarshallingContext.END_TAG;
126         }
127     }
128
129     /* (non-Javadoc)
130      * @see org.jibx.runtime.IUnmarshaller#unmarshal(java.lang.Object,
131      * org.jibx.runtime.IUnmarshallingContext)
132      */

133      
134     public Object JavaDoc unmarshal(Object JavaDoc obj, IUnmarshallingContext ictx)
135         throws JiBXException {
136         
137         // verify the entry conditions
138
boolean created = false;
139         List JavaDoc list = null;
140         if (obj == null) {
141             list = new ArrayList JavaDoc();
142             created = true;
143         } else if (obj instanceof List JavaDoc) {
144             list = (List JavaDoc)obj;
145         } else {
146             throw new JiBXException("Supplied object is not a java.util.List");
147         }
148         if (!(ictx instanceof UnmarshallingContext)) {
149             throw new JiBXException
150                 ("Unmarshalling context not of expected type");
151         }
152         
153         // unmarshal content to document model
154
m_unmarshalContext = (UnmarshallingContext)ictx;
155         try {
156             Node JavaDoc node;
157             while ((node = unmarshalNode()) != null) {
158                 list.add(node);
159             }
160             if (created && list.isEmpty()) {
161                 return null;
162             } else {
163                 return list;
164             }
165         } catch (IOException JavaDoc e) {
166             throw new JiBXException("Error reading from document", e);
167         }
168     }
169 }
Popular Tags