KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > encoding > ser > MapDeserializer


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55
56 package org.jboss.axis.encoding.ser;
57
58 import org.jboss.axis.encoding.DeserializationContext;
59 import org.jboss.axis.encoding.Deserializer;
60 import org.jboss.axis.encoding.DeserializerImpl;
61 import org.jboss.axis.encoding.DeserializerTarget;
62 import org.jboss.axis.message.SOAPHandler;
63 import org.jboss.axis.utils.Messages;
64 import org.jboss.logging.Logger;
65 import org.xml.sax.Attributes JavaDoc;
66 import org.xml.sax.SAXException JavaDoc;
67
68 import javax.xml.namespace.QName JavaDoc;
69 import java.util.HashMap JavaDoc;
70 import java.util.Map JavaDoc;
71
72 /*
73  * A <code>MapSerializer</code> is be used to deserialize
74  * deserialize Maps using the <code>SOAP-ENC</code>
75  * encoding style.<p>
76  *
77  * @author Glen Daniels (gdaniels@macromedia.com)
78  * Modified by @author Rich scheuerle <scheu@us.ibm.com>
79  */

80
81 public class MapDeserializer extends DeserializerImpl
82 {
83
84    private static Logger log = Logger.getLogger(MapDeserializer.class.getName());
85
86    // Fixed objects to act as hints to the set() callback
87
public static final Object JavaDoc KEYHINT = new Object JavaDoc();
88    public static final Object JavaDoc VALHINT = new Object JavaDoc();
89    public static final Object JavaDoc NILHINT = new Object JavaDoc();
90
91
92    /**
93     * This method is invoked after startElement when the element requires
94     * deserialization (i.e. the element is not an href and the value is not nil.)
95     * <p/>
96     * Simply creates map.
97     *
98     * @param namespace is the namespace of the element
99     * @param localName is the name of the element
100     * @param prefix is the prefix of the element
101     * @param attributes are the attributes on the element...used to get the type
102     * @param context is the DeserializationContext
103     */

104    public void onStartElement(String JavaDoc namespace, String JavaDoc localName,
105                               String JavaDoc prefix, Attributes JavaDoc attributes,
106                               DeserializationContext context)
107            throws SAXException JavaDoc
108    {
109       if (log.isDebugEnabled())
110       {
111          log.debug("Enter MapDeserializer::startElement()");
112       }
113
114       if (context.isNil(attributes))
115       {
116          return;
117       }
118
119       // Create a hashmap to hold the deserialized values.
120
setValue(new HashMap JavaDoc());
121
122       if (log.isDebugEnabled())
123       {
124          log.debug("Exit: MapDeserializer::startElement()");
125       }
126    }
127
128    /**
129     * onStartChild is called on each child element.
130     *
131     * @param namespace is the namespace of the child element
132     * @param localName is the local name of the child element
133     * @param prefix is the prefix used on the name of the child element
134     * @param attributes are the attributes of the child element
135     * @param context is the deserialization context.
136     * @return is a Deserializer to use to deserialize a child (must be
137     * a derived class of SOAPHandler) or null if no deserialization should
138     * be performed.
139     */

140    public SOAPHandler onStartChild(String JavaDoc namespace,
141                                    String JavaDoc localName,
142                                    String JavaDoc prefix,
143                                    Attributes JavaDoc attributes,
144                                    DeserializationContext context)
145            throws SAXException JavaDoc
146    {
147
148       if (log.isDebugEnabled())
149       {
150          log.debug("Enter: MapDeserializer::onStartChild()");
151       }
152
153       if (localName.equals("item"))
154       {
155          ItemHandler handler = new ItemHandler(this);
156
157          // This item must be complete before we're complete...
158
addChildDeserializer(handler);
159
160          if (log.isDebugEnabled())
161          {
162             log.debug("Exit: MapDeserializer::onStartChild()");
163          }
164
165          return handler;
166       }
167
168       return this;
169    }
170
171    /**
172     * The registerValueTarget code above causes this set function to be invoked when
173     * each value is known.
174     *
175     * @param value is the value of an element
176     * @param hint is the key
177     */

178    public void setChildValue(Object JavaDoc value, Object JavaDoc hint) throws SAXException JavaDoc
179    {
180       if (log.isDebugEnabled())
181       {
182          log.debug(Messages.getMessage("gotValue00", "MapDeserializer", "" + value));
183       }
184       ((Map JavaDoc)this.value).put(hint, value);
185    }
186
187    /**
188     * A deserializer for an <item>. Handles getting the key and
189     * value objects from their own deserializers, and then putting
190     * the values into the HashMap we're building.
191     */

192    class ItemHandler extends DeserializerImpl
193    {
194       Object JavaDoc key;
195       Object JavaDoc myValue;
196       int numSet = 0;
197       MapDeserializer md = null;
198
199       ItemHandler(MapDeserializer md)
200       {
201          this.md = md;
202       }
203
204       /**
205        * Callback from our deserializers. The hint indicates
206        * whether the passed "val" argument is the key or the value
207        * for this mapping.
208        */

209       public void setChildValue(Object JavaDoc val, Object JavaDoc hint) throws SAXException JavaDoc
210       {
211          if (hint == KEYHINT)
212          {
213             key = val;
214          }
215          else if (hint == VALHINT)
216          {
217             myValue = val;
218          }
219          else if (hint != NILHINT)
220          {
221             return;
222          }
223          numSet++;
224          if (numSet == 2)
225             md.setChildValue(myValue, key);
226       }
227
228       public SOAPHandler onStartChild(String JavaDoc namespace,
229                                       String JavaDoc localName,
230                                       String JavaDoc prefix,
231                                       Attributes JavaDoc attributes,
232                                       DeserializationContext context)
233               throws SAXException JavaDoc
234       {
235          QName JavaDoc typeQName = context.getTypeFromAttributes(namespace,
236                  localName,
237                  attributes);
238          Deserializer dser = context.getDeserializerForType(typeQName);
239
240          // If no deserializer, use the base DeserializerImpl.
241
if (dser == null)
242             dser = new DeserializerImpl();
243
244          // When the child value is ready, we
245
// want our set method to be invoked.
246
// To do this register a DeserializeTarget on the
247
// new Deserializer.
248
DeserializerTarget dt = null;
249          if (context.isNil(attributes))
250          {
251             dt = new DeserializerTarget(this, NILHINT);
252          }
253          else if (localName.equals("key"))
254          {
255             dt = new DeserializerTarget(this, KEYHINT);
256          }
257          else if (localName.equals("value"))
258          {
259             dt = new DeserializerTarget(this, VALHINT);
260          }
261          else
262          {
263             // Do nothing
264
}
265          if (dt != null)
266          {
267             dser.registerValueTarget(dt);
268          }
269
270          // We need this guy to complete for us to complete.
271
addChildDeserializer(dser);
272
273          return (SOAPHandler)dser;
274       }
275    }
276 }
277
Popular Tags