KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 Copyright (c) 2003, 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.util.ArrayList JavaDoc;
32
33 import org.jibx.runtime.IAliasable;
34 import org.jibx.runtime.IMarshallable;
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.MarshallingContext;
41 import org.jibx.runtime.impl.UnmarshallingContext;
42
43 /**
44  * <p>Custom marshaller/unmarshaller for <code>Object[]</code> instances. This
45  * handles mapping arrays typed as <code>java.lang.Object[]</code>, where each
46  * item in the array must be of a mapped type. If a name is specified by the
47  * mapping definition that name is used as a wrapper around the elements
48  * representing the items in the array; otherwise, the elements are just handled
49  * inline.</p>
50  *
51  * @author Dennis M. Sosnoski
52  * @version 1.0
53  */

54
55 public class ObjectArrayMapper
56     implements IMarshaller, IUnmarshaller, IAliasable {
57     
58     private static final Object JavaDoc[] DUMMY_ARRAY = {};
59     
60     private String JavaDoc m_uri;
61     private int m_index;
62     private String JavaDoc m_name;
63     private ArrayList JavaDoc m_holder;
64     
65     /**
66      * Default constructor. This just sets up for an XML representation with no
67      * element wrapping the actual item structures. It'll be used by JiBX when
68      * no name information is supplied by the mapping which references this
69      * custom marshaller/unmarshaller.
70      */

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

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

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

107     
108     public void marshal(Object JavaDoc obj, IMarshallingContext ictx)
109         throws JiBXException {
110         
111         // make sure the parameters are as expected
112
if (obj == null) {
113             if (m_name == null) {
114                 throw new JiBXException
115                     ("null array not allowed without wrapper");
116             }
117         } else if (!(DUMMY_ARRAY.getClass().isInstance(obj))) {
118             throw new JiBXException("Invalid object type for marshaller");
119         } else if (!(ictx instanceof MarshallingContext)) {
120             throw new JiBXException("Invalid object type for marshaller");
121         } else {
122             
123             // start by generating start tag for container
124
MarshallingContext ctx = (MarshallingContext)ictx;
125             Object JavaDoc[] array = (Object JavaDoc[])obj;
126             if (m_name != null) {
127                 ctx.startTag(m_index, m_name);
128             }
129         
130             // loop through all entries in array
131
for (int i = 0; i < array.length; i++) {
132                 Object JavaDoc item = array[i];
133                 if (item instanceof IMarshallable) {
134                     ((IMarshallable)item).marshal(ctx);
135                 } else if (item == null) {
136                     throw new JiBXException("Null value at offset " + i +
137                         " not supported");
138                 } else {
139                     throw new JiBXException("Array value of type " +
140                         item.getClass().getName() + " at offset " + i +
141                         " is not marshallable");
142                 }
143             }
144         
145             // finish with end tag for container element
146
if (m_name != null) {
147                 ctx.endTag(m_index, m_name);
148             }
149                 
150         }
151     }
152
153     /* (non-Javadoc)
154      * @see org.jibx.runtime.IUnmarshaller#isPresent(org.jibx.runtime.IUnmarshallingContext)
155      */

156      
157     public boolean isPresent(IUnmarshallingContext ctx) throws JiBXException {
158         return ctx.isAt(m_uri, m_name);
159     }
160
161     /* (non-Javadoc)
162      * @see org.jibx.runtime.IUnmarshaller#unmarshal(java.lang.Object,
163      * org.jibx.runtime.IUnmarshallingContext)
164      */

165      
166     public Object JavaDoc unmarshal(Object JavaDoc obj, IUnmarshallingContext ictx)
167         throws JiBXException {
168         
169         // make sure we're at the appropriate start tag
170
UnmarshallingContext ctx = (UnmarshallingContext)ictx;
171         if (m_name != null) {
172             if (ctx.isAt(m_uri, m_name)) {
173                 ctx.parsePastStartTag(m_uri, m_name);
174             } else {
175                 return null;
176             }
177         }
178         
179         // create new array if needed
180
if (m_holder == null) {
181             m_holder = new ArrayList JavaDoc();
182         }
183         
184         // process all items present in document
185
while (!ctx.isEnd()) {
186             Object JavaDoc item = ctx.unmarshalElement();
187             m_holder.add(item);
188         }
189         
190         // discard close tag if used
191
if (m_name != null) {
192             ctx.parsePastEndTag(m_uri, m_name);
193         }
194         
195         // return array containing all items
196
Object JavaDoc[] result = m_holder.toArray();
197         m_holder.clear();
198         return result;
199     }
200 }
Popular Tags