KickJava   Java API By Example, From Geeks To Geeks.

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


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.DeserializerImpl;
60 import org.jboss.logging.Logger;
61 import org.xml.sax.Attributes JavaDoc;
62 import org.xml.sax.SAXException JavaDoc;
63
64 import javax.xml.namespace.QName JavaDoc;
65 import java.lang.reflect.Array JavaDoc;
66 import java.lang.reflect.Constructor JavaDoc;
67 import java.util.ArrayList JavaDoc;
68 import java.util.List JavaDoc;
69 import java.util.StringTokenizer JavaDoc;
70
71 /**
72  * Deserializer for xsd:list
73  *
74  * @author Thomas Diesler (thomas.diesler@jboss.org)
75  */

76 public class ListDeserializer extends DeserializerImpl
77 {
78    private static Logger log = Logger.getLogger(ListDeserializer.class.getName());
79
80    private List JavaDoc valueList = new ArrayList JavaDoc();
81
82    private Class JavaDoc javaType;
83    private QName JavaDoc xmlType;
84
85    public ListDeserializer(Class JavaDoc javaType, QName JavaDoc xmlType)
86    {
87       this.javaType = javaType;
88       this.xmlType = xmlType;
89    }
90
91    /**
92     * This method is invoked after startElement when the element requires
93     * deserialization (i.e. the element is not an href and the value is not nil.)
94     * <p/>
95     * Simply creates
96     *
97     * @param namespace is the namespace of the element
98     * @param localName is the name of the element
99     * @param prefix is the prefix of the element
100     * @param attributes are the attributes on the element...used to get the type
101     * @param context is the DeserializationContext
102     */

103    public void onStartElement(String JavaDoc namespace, String JavaDoc localName,
104                               String JavaDoc prefix, Attributes JavaDoc attributes,
105                               DeserializationContext context)
106            throws SAXException JavaDoc
107    {
108
109       if (log.isDebugEnabled())
110       {
111          log.debug("Enter: ListDeserializer::startElement()");
112       }
113
114       if (context.isNil(attributes))
115       {
116          return;
117       }
118
119       if (log.isDebugEnabled())
120       {
121          log.debug("Exit: ListDeserializer::startElement()");
122       }
123    }
124
125    /**
126     * onEndElement is called by endElement. It is not called
127     * if the element has an href.
128     *
129     * @param namespace is the namespace of the child element
130     * @param localName is the local name of the child element
131     * @param context is the deserialization context
132     */

133    public void onEndElement(String JavaDoc namespace, String JavaDoc localName, DeserializationContext context) throws SAXException JavaDoc
134    {
135
136       if (log.isDebugEnabled())
137       {
138          log.debug("Enter: ListDeserializer::endElement()");
139       }
140
141       Class JavaDoc componentType = javaType.getComponentType();
142
143       Constructor JavaDoc ctor = null;
144       try
145       {
146          // Get component type string constructor
147
ctor = componentType.getConstructor(new Class JavaDoc[]{String JavaDoc.class});
148       }
149       catch (NoSuchMethodException JavaDoc e)
150       {
151          throw new IllegalStateException JavaDoc("Cannot obtain string constructor for: " + componentType);
152       }
153
154       // Build the return object array
155
Object JavaDoc[] objArr = (Object JavaDoc[])Array.newInstance(componentType, valueList.size());
156
157       // Fill the object array
158
String JavaDoc strValue = null;
159       try
160       {
161          for (int i = 0; i < valueList.size(); i++)
162          {
163             strValue = (String JavaDoc)valueList.get(i);
164             Object JavaDoc obj = ctor.newInstance(new Object JavaDoc[]{strValue});
165             objArr[i] = obj;
166          }
167
168          value = objArr;
169       }
170       catch (Exception JavaDoc e)
171       {
172          throw new IllegalArgumentException JavaDoc("Cannot construct object with: " + strValue);
173       }
174
175
176       if (log.isDebugEnabled())
177       {
178          log.debug("Exit: ListDeserializer::endElement()");
179       }
180    }
181
182    /**
183     * Parse the content and construct an Object array from it.
184     */

185    public void characters(char[] p1, int p2, int p3) throws SAXException JavaDoc
186    {
187       super.characters(p1, p2, p3);
188
189       String JavaDoc strContent = new String JavaDoc(p1, p2, p3);
190
191       StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(strContent);
192       while (st.hasMoreTokens())
193       {
194          String JavaDoc nextToken = st.nextToken();
195          Object JavaDoc item = getValueAsObject(nextToken);
196          valueList.add(item);
197       }
198    }
199
200    /**
201     * Get the Object for this token
202     */

203    private Object JavaDoc getValueAsObject(String JavaDoc token)
204    {
205       return token;
206    }
207 }
208
Popular Tags