KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.axis.encoding.ser;
18
19 import org.apache.axis.components.logger.LogFactory;
20 import org.apache.axis.encoding.DeserializationContext;
21 import org.apache.axis.encoding.Deserializer;
22 import org.apache.axis.encoding.DeserializerImpl;
23 import org.apache.axis.encoding.DeserializerTarget;
24 import org.apache.axis.message.SOAPHandler;
25 import org.apache.axis.utils.Messages;
26 import org.apache.commons.logging.Log;
27 import org.xml.sax.Attributes JavaDoc;
28 import org.xml.sax.SAXException JavaDoc;
29
30 import javax.xml.namespace.QName JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Map JavaDoc;
33
34 /*
35  * A <code>MapSerializer</code> is be used to deserialize
36  * deserialize Maps using the <code>SOAP-ENC</code>
37  * encoding style.<p>
38  *
39  * @author Glen Daniels (gdaniels@apache.org)
40  * Modified by @author Rich scheuerle <scheu@us.ibm.com>
41  */

42 public class MapDeserializer extends DeserializerImpl {
43
44     protected static Log log =
45         LogFactory.getLog(MapDeserializer.class.getName());
46
47     // Fixed objects to act as hints to the set() callback
48
public static final Object JavaDoc KEYHINT = new Object JavaDoc();
49     public static final Object JavaDoc VALHINT = new Object JavaDoc();
50     public static final Object JavaDoc NILHINT = new Object JavaDoc();
51
52
53     /**
54      * This method is invoked after startElement when the element requires
55      * deserialization (i.e. the element is not an href and the value is not nil.)
56      *
57      * Simply creates map.
58      * @param namespace is the namespace of the element
59      * @param localName is the name of the element
60      * @param prefix is the prefix of the element
61      * @param attributes are the attributes on the element...used to get the type
62      * @param context is the DeserializationContext
63      */

64     public void onStartElement(String JavaDoc namespace, String JavaDoc localName,
65                                String JavaDoc prefix, Attributes JavaDoc attributes,
66                                DeserializationContext context)
67         throws SAXException JavaDoc {
68         if (log.isDebugEnabled()) {
69             log.debug("Enter MapDeserializer::startElement()");
70         }
71         
72         if (context.isNil(attributes)) {
73             return;
74         }
75         
76         // Create a hashmap to hold the deserialized values.
77
setValue(new HashMap JavaDoc());
78         
79         if (log.isDebugEnabled()) {
80             log.debug("Exit: MapDeserializer::startElement()");
81         }
82     }
83     
84     /**
85      * onStartChild is called on each child element.
86      *
87      * @param namespace is the namespace of the child element
88      * @param localName is the local name of the child element
89      * @param prefix is the prefix used on the name of the child element
90      * @param attributes are the attributes of the child element
91      * @param context is the deserialization context.
92      * @return is a Deserializer to use to deserialize a child (must be
93      * a derived class of SOAPHandler) or null if no deserialization should
94      * be performed.
95      */

96     public SOAPHandler onStartChild(String JavaDoc namespace,
97                                     String JavaDoc localName,
98                                     String JavaDoc prefix,
99                                     Attributes JavaDoc attributes,
100                                     DeserializationContext context)
101         throws SAXException JavaDoc {
102
103         if (log.isDebugEnabled()) {
104             log.debug("Enter: MapDeserializer::onStartChild()");
105         }
106
107         if(localName.equals("item")) {
108             ItemHandler handler = new ItemHandler(this);
109             
110             // This item must be complete before we're complete...
111
addChildDeserializer(handler);
112             
113             if (log.isDebugEnabled()) {
114                 log.debug("Exit: MapDeserializer::onStartChild()");
115             }
116     
117             return handler;
118         }
119         
120         return this;
121     }
122     
123     /**
124      * The registerValueTarget code above causes this set function to be invoked when
125      * each value is known.
126      * @param value is the value of an element
127      * @param hint is the key
128      */

129     public void setChildValue(Object JavaDoc value, Object JavaDoc hint) throws SAXException JavaDoc
130     {
131         if (log.isDebugEnabled()) {
132             log.debug(Messages.getMessage("gotValue00", "MapDeserializer", "" + value));
133         }
134         ((Map JavaDoc)this.value).put(hint, value);
135     }
136     
137     /**
138      * A deserializer for an <item>. Handles getting the key and
139      * value objects from their own deserializers, and then putting
140      * the values into the HashMap we're building.
141      *
142      */

143     class ItemHandler extends DeserializerImpl {
144         Object JavaDoc key;
145         Object JavaDoc myValue;
146         int numSet = 0;
147         MapDeserializer md = null;
148
149         ItemHandler(MapDeserializer md) {
150             this.md = md;
151         }
152         /**
153          * Callback from our deserializers. The hint indicates
154          * whether the passed "val" argument is the key or the value
155          * for this mapping.
156          */

157         public void setChildValue(Object JavaDoc val, Object JavaDoc hint) throws SAXException JavaDoc
158         {
159             if (hint == KEYHINT) {
160                 key = val;
161             } else if (hint == VALHINT) {
162                 myValue = val;
163             } else if (hint != NILHINT) {
164                 return;
165             }
166             numSet++;
167             if (numSet == 2)
168                 md.setChildValue(myValue, key);
169         }
170         
171         public SOAPHandler onStartChild(String JavaDoc namespace,
172                                     String JavaDoc localName,
173                                     String JavaDoc prefix,
174                                     Attributes JavaDoc attributes,
175                                     DeserializationContext context)
176         throws SAXException JavaDoc
177         {
178             QName JavaDoc typeQName = context.getTypeFromAttributes(namespace,
179                                                             localName,
180                                                             attributes);
181             Deserializer dser = context.getDeserializerForType(typeQName);
182
183             // If no deserializer, use the base DeserializerImpl.
184
if (dser == null)
185                 dser = new DeserializerImpl();
186
187             // When the child value is ready, we
188
// want our set method to be invoked.
189
// To do this register a DeserializeTarget on the
190
// new Deserializer.
191
DeserializerTarget dt = null;
192             if (context.isNil(attributes)) {
193                 dt = new DeserializerTarget(this, NILHINT);
194             } else if (localName.equals("key")) {
195                 dt = new DeserializerTarget(this, KEYHINT);
196             } else if (localName.equals("value")) {
197                 dt = new DeserializerTarget(this, VALHINT);
198             } else {
199                 // Do nothing
200
}
201             if (dt != null) {
202                 dser.registerValueTarget(dt);
203             }
204             
205             // We need this guy to complete for us to complete.
206
addChildDeserializer(dser);
207             
208             return (SOAPHandler)dser;
209         }
210     }
211 }
212
Popular Tags