KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
34
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 content list 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 that implements <code>java.util.List</code> (the actual
47  * runtime type - the declared type is ignored and can be anything). When
48  * unmarshalling it will create an instance of <code>java.util.ArrayList</code>
49  * if a list is not passed in and any content is present, then return all the
50  * content up to the close tag for the enclosing element in the list. When
51  * marshalling, it will simply write out any content directly.</p>
52  *
53  * @author Dennis M. Sosnoski
54  * @version 1.0
55  */

56
57 public class Dom4JListMapper extends Dom4JMapperBase
58     implements IMarshaller, IUnmarshaller
59 {
60     /* (non-Javadoc)
61      * @see org.jibx.runtime.IMarshaller#isExtension(int)
62      */

63     
64     public boolean isExtension(int index) {
65         return false;
66     }
67
68     /* (non-Javadoc)
69      * @see org.jibx.runtime.IMarshaller#marshal(java.lang.Object,
70      * org.jibx.runtime.IMarshallingContext)
71      */

72     
73     public void marshal(Object JavaDoc obj, IMarshallingContext ictx)
74         throws JiBXException {
75         
76         // make sure the parameters are as expected
77
if (!(obj instanceof List JavaDoc)) {
78             throw new JiBXException("Mapped object not a java.util.List");
79         } else {
80             try {
81                     
82                 // marshal all content with no indentation
83
m_xmlWriter = ictx.getXmlWriter();
84                 int indent = ictx.getIndent();
85                 ictx.setIndent(-1);
86                 m_defaultNamespaceURI = null;
87                 marshalContent((List JavaDoc)obj);
88                 ictx.setIndent(indent);
89                 
90             } catch (IOException JavaDoc e) {
91                 throw new JiBXException("Error writing to document", e);
92             }
93         }
94     }
95
96     /* (non-Javadoc)
97      * @see org.jibx.runtime.IUnmarshaller#isPresent(org.jibx.runtime.IUnmarshallingContext)
98      */

99      
100     public boolean isPresent(IUnmarshallingContext ctx) throws JiBXException {
101         if (!(ctx instanceof UnmarshallingContext)) {
102             throw new JiBXException
103                 ("Unmarshalling context not of expected type");
104         } else {
105             return ((UnmarshallingContext)ctx).currentEvent() !=
106                 UnmarshallingContext.END_TAG;
107         }
108     }
109
110     /* (non-Javadoc)
111      * @see org.jibx.runtime.IUnmarshaller#unmarshal(java.lang.Object,
112      * org.jibx.runtime.IUnmarshallingContext)
113      */

114      
115     public Object JavaDoc unmarshal(Object JavaDoc obj, IUnmarshallingContext ictx)
116         throws JiBXException {
117         
118         // verify the entry conditions
119
boolean created = false;
120         List JavaDoc list = null;
121         if (obj == null) {
122             list = new ArrayList JavaDoc();
123             created = true;
124         } else if (obj instanceof List JavaDoc) {
125             list = (List JavaDoc)obj;
126         } else {
127             throw new JiBXException("Supplied object is not a java.util.List");
128         }
129         if (!(ictx instanceof UnmarshallingContext)) {
130             throw new JiBXException
131                 ("Unmarshalling context not of expected type");
132         }
133         
134         // unmarshal content to document model
135
m_unmarshalContext = (UnmarshallingContext)ictx;
136         try {
137             unmarshalContent(list);
138             if (created && list.isEmpty()) {
139                 return null;
140             } else {
141                 return list;
142             }
143         } catch (IOException JavaDoc e) {
144             throw new JiBXException("Error reading from document", e);
145         }
146     }
147 }
Popular Tags