KickJava   Java API By Example, From Geeks To Geeks.

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


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.Vector JavaDoc;
32
33 /**
34  * Deserializer for SOAP Vectors for compatibility with SOAP 2.2.
35  *
36  * @author Carsten Ziegeler (cziegeler@apache.org)
37  * Modified by @author Rich scheuerle <scheu@us.ibm.com>
38  */

39 public class VectorDeserializer extends DeserializerImpl
40 {
41     protected static Log log =
42         LogFactory.getLog(VectorDeserializer.class.getName());
43
44     public int curIndex = 0;
45
46     /**
47      * This method is invoked after startElement when the element requires
48      * deserialization (i.e. the element is not an href and the value is not nil.)
49      *
50      * Simply creates
51      * @param namespace is the namespace of the element
52      * @param localName is the name of the element
53      * @param prefix is the prefix of the element
54      * @param attributes are the attributes on the element...used to get the type
55      * @param context is the DeserializationContext
56      */

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

89     public SOAPHandler onStartChild(String JavaDoc namespace,
90                                     String JavaDoc localName,
91                                     String JavaDoc prefix,
92                                     Attributes JavaDoc attributes,
93                                     DeserializationContext context)
94         throws SAXException JavaDoc {
95         if (log.isDebugEnabled()) {
96             log.debug("Enter: VectorDeserializer::onStartChild()");
97         }
98         
99         if (attributes == null)
100             throw new SAXException JavaDoc(Messages.getMessage("noType01"));
101
102         // If the xsi:nil attribute, set the value to null and return since
103
// there is nothing to deserialize.
104
if (context.isNil(attributes)) {
105             setChildValue(null, new Integer JavaDoc(curIndex++));
106             return null;
107         }
108
109         // Get the type
110
QName JavaDoc itemType = context.getTypeFromAttributes(namespace,
111                                                        localName,
112                                                        attributes);
113         // Get the deserializer
114
Deserializer dSer = null;
115         if (itemType != null) {
116            dSer = context.getDeserializerForType(itemType);
117         }
118         if (dSer == null) {
119             dSer = new DeserializerImpl();
120         }
121
122         // When the value is deserialized, inform us.
123
// Need to pass the index because multi-ref stuff may
124
// result in the values being deserialized in a different order.
125
dSer.registerValueTarget(new DeserializerTarget(this, new Integer JavaDoc(curIndex)));
126         curIndex++;
127
128         if (log.isDebugEnabled()) {
129             log.debug("Exit: VectorDeserializer::onStartChild()");
130         }
131         
132         // Let the framework know that we aren't complete until this guy
133
// is complete.
134
addChildDeserializer(dSer);
135         
136         return (SOAPHandler)dSer;
137     }
138     
139     /**
140      * The registerValueTarget code above causes this set function to be invoked when
141      * each value is known.
142      * @param value is the value of an element
143      * @param hint is an Integer containing the index
144      */

145     public void setChildValue(Object JavaDoc value, Object JavaDoc hint) throws SAXException JavaDoc
146     {
147         if (log.isDebugEnabled()) {
148             log.debug(Messages.getMessage("gotValue00", "VectorDeserializer", "" + value));
149         }
150         int offset = ((Integer JavaDoc)hint).intValue();
151         Vector JavaDoc v = (Vector JavaDoc)this.value;
152         
153         // If the vector is too small, grow it
154
if (offset >= v.size()) {
155             v.setSize(offset+1);
156         }
157         v.setElementAt(value, offset);
158     }
159 }
160
Popular Tags