KickJava   Java API By Example, From Geeks To Geeks.

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


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>VectorSerializer</code> can be used to serialize (but not
72  * deserialize) Vectors and Enumerations using the <code>SOAP-ENC</code>
73  * encoding style.<p>
74  *
75  * This serializer turns Vectors/Enumerations into SOAP arrays.
76  *
77  * @author Glen Daniels (gdaniels@allaire.com)
78  */

79 public class VectorSerializer implements Serializer, Deserializer
80 {
81   public void marshall(String inScopeEncStyle, Class javaType, Object src,
82                        Object context, Writer sink, NSStack nsStack,
83                        XMLJavaMappingRegistry xjmr, SOAPContext ctx)
84     throws IllegalArgumentException, IOException
85   {
86     nsStack.pushScope();
87
88     if ((src != null) &&
89                 !(src instanceof Vector) &&
90                 !(src instanceof Enumeration))
91       throw new IllegalArgumentException("Tried to pass a '" +
92                         src.getClass().toString() + "' to VectorSerializer");
93
94         String lengthStr;
95         Enumeration enum;
96         
97         if (src instanceof Enumeration) {
98                 /** TODO: Right now we don't include a length on Enumerations,
99                  * due to efficiency concerns. There should be a way to configure
100                  * doing the length calculation (at the cost of traversing the
101                  * Enumeration) for a particular installation/service/call.
102                  */

103                 enum = (Enumeration)src;
104                 lengthStr = "";
105         } else {
106                 Vector v = (Vector)src;
107                 enum = v.elements();
108         
109                 lengthStr = src != null
110                        ? v.size() + ""
111                        : "";
112         }
113
114
115     if (src == null)
116     {
117       SoapEncUtils.generateNullStructure(inScopeEncStyle,
118                                      javaType,
119                                      context,
120                                      sink,
121                                      nsStack,
122                                      xjmr);
123     }
124     else
125     {
126       SoapEncUtils.generateStructureHeader(inScopeEncStyle,
127                                        javaType,
128                                        context,
129                                        sink,
130                                        nsStack,
131                                        xjmr);
132
133       sink.write(StringUtils.lineSeparator);
134
135       for (Enumeration e = enum; e.hasMoreElements(); )
136       {
137         nsStack.pushScope();
138
139         Object value = e.nextElement();
140
141         if (value == null)
142         {
143           SoapEncUtils.generateNullStructure(Constants.NS_URI_SOAP_ENC,
144                                              Object.class, "item", sink,
145                                              nsStack, xjmr);
146         }
147         else
148         {
149           Class actualComponentType = value.getClass();
150
151           xjmr.marshall(Constants.NS_URI_SOAP_ENC, actualComponentType, value,
152                         "item", sink, nsStack, ctx);
153         }
154
155         sink.write(StringUtils.lineSeparator);
156         nsStack.popScope();
157       }
158
159       sink.write("</" + context + '>');
160     }
161
162     nsStack.popScope();
163   }
164
165   public Bean unmarshall(String inScopeEncStyle, QName elementType, Node src,
166                          XMLJavaMappingRegistry xjmr, SOAPContext ctx)
167     throws IllegalArgumentException
168   {
169       Element root = (Element)src;
170       if (SoapEncUtils.isNull(root)) {
171           return new Bean(Vector.class, null);
172       }
173       
174       Vector v = new Vector();
175       
176       Element tempEl = DOMUtils.getFirstChildElement(root);
177       while (tempEl != null) {
178           String declEncStyle = DOMUtils.getAttributeNS(tempEl,
179                                                         Constants.NS_URI_SOAP_ENV, Constants.ATTR_ENCODING_STYLE);
180           String actualEncStyle = declEncStyle != null
181                                   ? declEncStyle
182                                     : inScopeEncStyle;
183
184           // If it's a local reference, follow it.
185
String href = tempEl.getAttribute(Constants.ATTR_REFERENCE);
186           Element actualEl = tempEl;
187           if(href != null && !href.equals("") && (href.charAt(0) == '#'))
188           {
189             href = href.substring(1);
190             actualEl = DOMUtils.getElementByID(src.getOwnerDocument().getDocumentElement(),href);
191             if (actualEl == null) {
192               throw new IllegalArgumentException("No such ID '" + href + "'");
193             }
194           }
195                                     
196           QName declItemType = SoapEncUtils.getTypeQName(actualEl);
197           QName actualItemType = declItemType;
198
199           Bean itemBean = xjmr.unmarshall(actualEncStyle, actualItemType,
200                                                   actualEl, ctx);
201
202           v.addElement(itemBean.value);
203
204           tempEl = DOMUtils.getNextSiblingElement(tempEl);
205       }
206       
207       return new Bean(Vector.class, v);
208   }
209
210 }
211
Popular Tags