KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > soap > encoding > soapenc > ArraySerializer


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2000 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 "SOAP" 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 and was
52  * originally based on software copyright (c) 2000, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.apache.soap.encoding.soapenc;
59
60 import java.beans.*;
61 import java.io.*;
62 import java.util.*;
63 import java.lang.reflect.*;
64 import org.w3c.dom.*;
65 import org.apache.soap.util.*;
66 import org.apache.soap.util.xml.*;
67 import org.apache.soap.*;
68 import org.apache.soap.rpc.*;
69
70 /**
71  * A <code>ArraySerializer</code> can be used to serialize and deserialize
72  * arrays using the <code>SOAP-ENC</code> encoding style.
73  *
74  * @author Matthew J. Duftler (duftler@us.ibm.com)
75  */

76 public class ArraySerializer implements Serializer, Deserializer
77 {
78   public void marshall(String JavaDoc inScopeEncStyle, Class JavaDoc javaType, Object JavaDoc src,
79                        Object JavaDoc context, Writer sink, NSStack nsStack,
80                        XMLJavaMappingRegistry xjmr, SOAPContext ctx)
81     throws IllegalArgumentException JavaDoc, IOException
82   {
83     nsStack.pushScope();
84
85     String JavaDoc lengthStr = src != null
86                        ? Array.getLength(src) + ""
87                        : "";
88     Class JavaDoc componentType = javaType.getComponentType();
89     QName elementType = xjmr.queryElementType(componentType,
90                                               Constants.NS_URI_SOAP_ENC);
91
92     if (src == null)
93     {
94       SoapEncUtils.generateNullArray(inScopeEncStyle,
95                                      javaType,
96                                      context,
97                                      sink,
98                                      nsStack,
99                                      xjmr,
100                                      elementType,
101                                      lengthStr);
102     }
103     else
104     {
105       SoapEncUtils.generateArrayHeader(inScopeEncStyle,
106                                        javaType,
107                                        context,
108                                        sink,
109                                        nsStack,
110                                        xjmr,
111                                        elementType,
112                                        lengthStr);
113
114       sink.write(StringUtils.lineSeparator);
115
116       int length = Array.getLength(src);
117
118       for (int i = 0; i < length; i++)
119       {
120         nsStack.pushScope();
121
122         Object JavaDoc value = Array.get(src, i);
123
124         if (value == null)
125         {
126           SoapEncUtils.generateNullStructure(inScopeEncStyle, componentType,
127                                              "item", sink, nsStack, xjmr);
128         }
129         else
130         {
131           Class JavaDoc actualComponentType = value.getClass();
132
133           xjmr.marshall(Constants.NS_URI_SOAP_ENC, actualComponentType, value, "item",
134                         sink, nsStack, ctx);
135         }
136
137         sink.write(StringUtils.lineSeparator);
138         nsStack.popScope();
139       }
140
141       sink.write("</" + context + '>');
142     }
143
144     nsStack.popScope();
145   }
146
147   public Bean unmarshall(String JavaDoc inScopeEncStyle, QName elementType, Node src,
148                          XMLJavaMappingRegistry xjmr, SOAPContext ctx)
149     throws IllegalArgumentException JavaDoc
150   {
151     Element root = (Element)src;
152     String JavaDoc name = root.getTagName();
153     QName arrayItemType = new QName("", "");
154     Object JavaDoc array = getNewArray(inScopeEncStyle, root, arrayItemType, xjmr);
155
156     if (SoapEncUtils.isNull(root))
157     {
158       return new Bean(array.getClass(), null);
159     }
160
161     Element tempEl = DOMUtils.getFirstChildElement(root);
162     int length = Array.getLength(array);
163
164     for (int i = 0; i < length; i++)
165     {
166       String JavaDoc declEncStyle = DOMUtils.getAttributeNS(tempEl,
167         Constants.NS_URI_SOAP_ENV, Constants.ATTR_ENCODING_STYLE);
168       String JavaDoc actualEncStyle = declEncStyle != null
169                               ? declEncStyle
170                               : inScopeEncStyle;
171       QName declItemType = SoapEncUtils.getTypeQName(tempEl);
172       QName actualItemType = declItemType != null
173                              ? declItemType
174                              : arrayItemType;
175
176       // If it's a local reference, follow it.
177
String JavaDoc href = tempEl.getAttribute(Constants.ATTR_REFERENCE);
178       Element actualEl = tempEl;
179
180       if(href != null && !href.equals("") && (href.charAt(0) == '#'))
181       {
182         href = href.substring(1);
183         actualEl = DOMUtils.getElementByID(src.getOwnerDocument().getDocumentElement(),href);
184         if (actualEl == null) {
185           throw new IllegalArgumentException JavaDoc("No such ID '" + href + "'");
186         }
187       }
188
189       if (!SoapEncUtils.isNull(actualEl))
190       {
191         Bean itemBean = xjmr.unmarshall(actualEncStyle,
192                                         actualItemType,
193                                         actualEl,
194                                         ctx);
195
196         Array.set(array, i, itemBean.value);
197       }
198
199       tempEl = DOMUtils.getNextSiblingElement(tempEl);
200     }
201
202     return new Bean(array.getClass(), array);
203   }
204
205   public static Object JavaDoc getNewArray(String JavaDoc inScopeEncStyle, Element arrayEl,
206                                    QName arrayItemType,
207                                    XMLJavaMappingRegistry xjmr)
208     throws IllegalArgumentException JavaDoc
209   {
210     QName arrayTypeValue = SoapEncUtils.getAttributeValue(arrayEl,
211       Constants.NS_URI_SOAP_ENC, Constants.ATTR_ARRAY_TYPE, "array", true);
212     String JavaDoc arrayTypeValueNamespaceURI = arrayTypeValue.getNamespaceURI();
213     String JavaDoc arrayTypeValueLocalPart = arrayTypeValue.getLocalPart();
214     int leftBracketIndex = arrayTypeValueLocalPart.lastIndexOf('[');
215     int rightBracketIndex = arrayTypeValueLocalPart.lastIndexOf(']');
216
217     if (leftBracketIndex == -1
218         || rightBracketIndex == -1
219         || rightBracketIndex < leftBracketIndex)
220     {
221       throw new IllegalArgumentException JavaDoc("Malformed arrayTypeValue '" +
222                                          arrayTypeValue + "'.");
223     }
224
225     String JavaDoc componentTypeName =
226       arrayTypeValueLocalPart.substring(0, leftBracketIndex);
227
228     if (componentTypeName.endsWith("]"))
229     {
230       throw new IllegalArgumentException JavaDoc("Arrays of arrays are not " +
231                                          "supported '" + arrayTypeValue +
232                                          "'.");
233     }
234
235     arrayItemType.setNamespaceURI(arrayTypeValueNamespaceURI);
236     arrayItemType.setLocalPart(componentTypeName);
237
238     int length = DOMUtils.countKids(arrayEl, Node.ELEMENT_NODE);
239     String JavaDoc lengthStr =
240       arrayTypeValueLocalPart.substring(leftBracketIndex + 1,
241                                         rightBracketIndex);
242
243     if (lengthStr.length() > 0)
244     {
245       if (lengthStr.indexOf(',') != -1)
246       {
247         throw new IllegalArgumentException JavaDoc("Multi-dimensional arrays are " +
248                                            "not supported '" +
249                                            lengthStr + "'.");
250       }
251
252       try
253       {
254         int explicitLength = Integer.parseInt(lengthStr);
255
256         if (length != explicitLength)
257         {
258           throw new IllegalArgumentException JavaDoc("Explicit array length is " +
259                                              "not equal to the number of " +
260                                              "items '" + explicitLength +
261                                              " != " + length + "'.");
262         }
263       }
264       catch (NumberFormatException JavaDoc e)
265       {
266         throw new IllegalArgumentException JavaDoc("Explicit array length is not a " +
267                                            "valid integer '" + lengthStr +
268                                            "'.");
269       }
270     }
271
272     Class JavaDoc componentType = xjmr.queryJavaType(arrayItemType, inScopeEncStyle);
273
274     return Array.newInstance(componentType, length);
275   }
276 }
277
Popular Tags