KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example14 > HashMapper


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 example14;
30
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import org.jibx.runtime.IAliasable;
36 import org.jibx.runtime.IMarshallable;
37 import org.jibx.runtime.IMarshaller;
38 import org.jibx.runtime.IMarshallingContext;
39 import org.jibx.runtime.IUnmarshaller;
40 import org.jibx.runtime.IUnmarshallingContext;
41 import org.jibx.runtime.JiBXException;
42 import org.jibx.runtime.impl.MarshallingContext;
43 import org.jibx.runtime.impl.UnmarshallingContext;
44
45 public class HashMapper implements IMarshaller, IUnmarshaller, IAliasable
46 {
47     private static final String JavaDoc SIZE_ATTRIBUTE_NAME = "size";
48     private static final String JavaDoc ENTRY_ELEMENT_NAME = "entry";
49     private static final String JavaDoc KEY_ATTRIBUTE_NAME = "key";
50     private static final int DEFAULT_SIZE = 10;
51     
52     private String JavaDoc m_uri;
53     private int m_index;
54     private String JavaDoc m_name;
55     
56     public HashMapper() {
57         m_uri = null;
58         m_index = 0;
59         m_name = "hashmap";
60     }
61     
62     public HashMapper(String JavaDoc uri, int index, String JavaDoc name) {
63         m_uri = uri;
64         m_index = index;
65         m_name = name;
66     }
67     
68     /* (non-Javadoc)
69      * @see org.jibx.runtime.IMarshaller#isExtension(int)
70      */

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

80     
81     public void marshal(Object JavaDoc obj, IMarshallingContext ictx)
82         throws JiBXException {
83         
84         // make sure the parameters are as expected
85
if (!(obj instanceof HashMap JavaDoc)) {
86             throw new JiBXException("Invalid object type for marshaller");
87         } else if (!(ictx instanceof MarshallingContext)) {
88             throw new JiBXException("Invalid object type for marshaller");
89         } else {
90             
91             // start by generating start tag for container
92
MarshallingContext ctx = (MarshallingContext)ictx;
93             HashMap JavaDoc map = (HashMap JavaDoc)obj;
94             ctx.startTagAttributes(m_index, m_name).
95                 attribute(m_index, SIZE_ATTRIBUTE_NAME, map.size()).
96                 closeStartContent();
97             
98             // loop through all entries in hashmap
99
Iterator JavaDoc iter = map.entrySet().iterator();
100             while (iter.hasNext()) {
101                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iter.next();
102                 ctx.startTagAttributes(m_index, ENTRY_ELEMENT_NAME);
103                 if (entry.getKey() != null) {
104                     ctx.attribute(m_index, KEY_ATTRIBUTE_NAME,
105                         entry.getKey().toString());
106                 }
107                 ctx.closeStartContent();
108                 if (entry.getValue() instanceof IMarshallable) {
109                     ((IMarshallable)entry.getValue()).marshal(ctx);
110                     ctx.endTag(m_index, ENTRY_ELEMENT_NAME);
111                 } else {
112                     throw new JiBXException("Mapped value is not marshallable");
113                 }
114             }
115             
116             // finish with end tag for container element
117
ctx.endTag(m_index, m_name);
118         }
119     }
120
121     /* (non-Javadoc)
122      * @see org.jibx.runtime.IUnmarshaller#isPresent(org.jibx.runtime.IUnmarshallingContext)
123      */

124      
125     public boolean isPresent(IUnmarshallingContext ctx) throws JiBXException {
126         return ctx.isAt(m_uri, m_name);
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         // make sure we're at the appropriate start tag
138
UnmarshallingContext ctx = (UnmarshallingContext)ictx;
139         if (!ctx.isAt(m_uri, m_name)) {
140             ctx.throwStartTagNameError(m_uri, m_name);
141         }
142         
143         // create new hashmap if needed
144
int size = ctx.attributeInt(m_uri, SIZE_ATTRIBUTE_NAME, DEFAULT_SIZE);
145         HashMap JavaDoc map = (HashMap JavaDoc)obj;
146         if (map == null) {
147             map = new HashMap JavaDoc(size);
148         }
149         
150         // process all entries present in document
151
ctx.parsePastStartTag(m_uri, m_name);
152         while (ctx.isAt(m_uri, ENTRY_ELEMENT_NAME)) {
153             Object JavaDoc key = ctx.attributeText(m_uri, KEY_ATTRIBUTE_NAME, null);
154             ctx.parsePastStartTag(m_uri, ENTRY_ELEMENT_NAME);
155             Object JavaDoc value = ctx.unmarshalElement();
156             map.put(key, value);
157             ctx.parsePastEndTag(m_uri, ENTRY_ELEMENT_NAME);
158         }
159         ctx.parsePastEndTag(m_uri, m_name);
160         return map;
161     }
162 }
Popular Tags