Source code: org/apache/axis/encoding/ser/VectorDeserializer.java
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;
28 import org.xml.sax.SAXException;
29
30 import javax.xml.namespace.QName;
31 import java.util.Vector;
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 namespace, String localName,
58 String prefix, Attributes attributes,
59 DeserializationContext context)
60 throws SAXException {
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());
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 namespace,
90 String localName,
91 String prefix,
92 Attributes attributes,
93 DeserializationContext context)
94 throws SAXException {
95 if (log.isDebugEnabled()) {
96 log.debug("Enter: VectorDeserializer::onStartChild()");
97 }
98
99 if (attributes == null)
100 throw new SAXException(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(curIndex++));
106 return null;
107 }
108
109 // Get the type
110 QName 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(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 value, Object hint) throws SAXException
146 {
147 if (log.isDebugEnabled()) {
148 log.debug(Messages.getMessage("gotValue00", "VectorDeserializer", "" + value));
149 }
150 int offset = ((Integer)hint).intValue();
151 Vector v = (Vector)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 }