KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > remote > soap > encoding > LZArraySerializer


1 /* *****************************************************************************
2  * LZArraySerializer.java
3  * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10
11 /*
12  * The Apache Software License, Version 1.1
13  *
14  *
15  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
16  * reserved.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  *
22  * 1. Redistributions of source code must retain the above copyright
23  * notice, this list of conditions and the following disclaimer.
24  *
25  * 2. Redistributions in binary form must reproduce the above copyright
26  * notice, this list of conditions and the following disclaimer in
27  * the documentation and/or other materials provided with the
28  * distribution.
29  *
30  * 3. The end-user documentation included with the redistribution,
31  * if any, must include the following acknowledgment:
32  * "This product includes software developed by the
33  * Apache Software Foundation (http://www.apache.org/)."
34  * Alternately, this acknowledgment may appear in the software itself,
35  * if and wherever such third-party acknowledgments normally appear.
36  *
37  * 4. The names "Axis" and "Apache Software Foundation" must
38  * not be used to endorse or promote products derived from this
39  * software without prior written permission. For written
40  * permission, please contact apache@apache.org.
41  *
42  * 5. Products derived from this software may not be called "Apache",
43  * nor may "Apache" appear in their name, without prior written
44  * permission of the Apache Software Foundation.
45  *
46  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
47  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
48  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
49  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
50  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
51  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
52  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
53  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
54  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
55  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
56  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57  * SUCH DAMAGE.
58  * ====================================================================
59  *
60  * This software consists of voluntary contributions made by many
61  * individuals on behalf of the Apache Software Foundation. For more
62  * information on the Apache Software Foundation, please see
63  * <http://www.apache.org/>.
64  */

65
66 package org.openlaszlo.remote.soap.encoding;
67
68 import javax.xml.namespace.QName JavaDoc;
69 import java.io.IOException JavaDoc;
70 import org.apache.axis.encoding.SerializationContext;
71 import org.apache.axis.encoding.Serializer;
72 import org.apache.axis.wsdl.fromJava.Types;
73 import org.w3c.dom.Element JavaDoc;
74 import org.w3c.dom.Node JavaDoc;
75 import org.w3c.dom.NodeList JavaDoc;
76 import org.xml.sax.Attributes JavaDoc;
77 import org.apache.log4j.Logger;
78 import org.openlaszlo.remote.soap.ArrayWrapper;
79 import org.xml.sax.helpers.AttributesImpl JavaDoc;
80 import java.util.ArrayList JavaDoc;
81 import org.openlaszlo.remote.soap.*;
82 import org.apache.axis.MessageContext;
83 import org.apache.axis.schema.SchemaVersion;
84 import org.apache.axis.soap.SOAPConstants;
85 import org.apache.axis.Constants;
86
87 public class LZArraySerializer implements Serializer {
88
89     private static Logger mLogger = Logger.getLogger(LZArraySerializer.class);
90
91     public static String JavaDoc MECHANISM_TYPE = "LZArrayMechanism";
92
93     public String JavaDoc getMechanismType() { return MECHANISM_TYPE; }
94
95     public void serialize(QName JavaDoc name, Attributes JavaDoc attributes,
96                           Object JavaDoc value, SerializationContext context)
97         throws IOException JavaDoc
98     {
99         mLogger.debug("serialize(" + name + "," + attributes +
100                       "," + value + "," + context + ")");
101
102         // Alot of this code copied from
103
// org.apache.axis.encoding.ser.ArraySerializer
104

105         MessageContext msgContext = context.getMessageContext();
106         SchemaVersion schema = SchemaVersion.SCHEMA_2001;
107         SOAPConstants soap = SOAPConstants.SOAP11_CONSTANTS;
108         boolean encoded = true;
109         if (msgContext != null) {
110             encoded = msgContext.isEncoded();
111             schema = msgContext.getSchemaVersion();
112             soap = msgContext.getSOAPConstants();
113         }
114
115         ArrayWrapper aw = (ArrayWrapper)((ArrayList JavaDoc)value).get(0);
116         Element JavaDoc el = aw.getElement();
117         ComplexType ct = aw.getType();
118
119         NodeList JavaDoc list = el.getChildNodes();
120         int len = getArrayLen(list);
121
122         String JavaDoc brackets = "";
123         if (encoded) {
124             if (soap == SOAPConstants.SOAP12_CONSTANTS)
125                 brackets += len;
126             else
127                 brackets += "[" + len + "]";
128
129             AttributesImpl JavaDoc attrs;
130             if (attributes == null) {
131                 attrs = new AttributesImpl JavaDoc();
132             } else {
133                 attrs = new AttributesImpl JavaDoc(attributes);
134             }
135
136             QName JavaDoc componentQName = ct.getArrayItemTypeQName();
137             String JavaDoc compType = context.attributeQName2String(componentQName);
138
139             if (attrs.getIndex(soap.getEncodingURI(), soap.getAttrItemType()) == -1) {
140                 String JavaDoc encprefix =
141                     context.getPrefixForURI(soap.getEncodingURI());
142
143                 if (soap != SOAPConstants.SOAP12_CONSTANTS) {
144                     compType = compType + brackets;
145                     
146                     attrs.addAttribute(soap.getEncodingURI(),
147                                        soap.getAttrItemType(),
148                                        encprefix + ":arrayType",
149                                        "CDATA",
150                                        compType);
151
152                 } else {
153                     attrs.addAttribute(soap.getEncodingURI(),
154                                        soap.getAttrItemType(),
155                                        encprefix + ":itemType",
156                                        "CDATA",
157                                        compType);
158
159                     attrs.addAttribute(soap.getEncodingURI(),
160                                        "arraySize",
161                                        encprefix + ":arraySize",
162                                        "CDATA",
163                                        brackets);
164                 }
165             }
166
167             // NOTE: comments here copied from axis code. -pk
168
//
169
// Force type to be SOAP_ARRAY for all array serialization.
170
//
171
// There are two choices here:
172
// Force the type to type=SOAP_ARRAY
173
// Pros: More interop test successes.
174
// Cons: Since we have specific type information it
175
// is more correct to use it. Plus the specific
176
// type information may be important on the
177
// server side to disambiguate overloaded operations.
178
// Use the specific type information:
179
// Pros: The specific type information is more correct
180
// and may be useful for operation overloading.
181
// Cons: More interop test failures (as of 2/6/2002).
182
//
183
int typeI = attrs.getIndex(schema.getXsiURI(),
184                                        "type");
185             if (typeI != -1) {
186                 String JavaDoc qname =
187                       context.getPrefixForURI(schema.getXsiURI(),
188                                               "xsi") + ":type";
189                 QName JavaDoc soapArray;
190                 if (soap == SOAPConstants.SOAP12_CONSTANTS) {
191                     soapArray = Constants.SOAP_ARRAY12;
192                 } else {
193                     soapArray = Constants.SOAP_ARRAY;
194                 }
195
196                 attrs.setAttribute(typeI,
197                                    schema.getXsiURI(),
198                                    "type",
199                                    qname,
200                                    "CDATA",
201                                    context.qName2String(soapArray));
202             }
203             attributes = attrs;
204         }
205
206         context.startElement(name, attributes);
207         for (int i=0; i < list.getLength(); i++) {
208             // Now copy each child element in the array. -pk
209
Node JavaDoc node = (Node JavaDoc)list.item(i);
210             if ( node.getNodeType() == Node.ELEMENT_NODE ) {
211                 context.writeDOMElement((Element JavaDoc)node);
212             }
213         }
214         context.endElement();
215     }
216
217     /**
218      * @return number of items in array.
219      */

220     public int getArrayLen(NodeList JavaDoc list){
221         int count=0;
222         for (int i=0; i < list.getLength(); i++) {
223             Node JavaDoc node = (Node JavaDoc)list.item(i);
224             if ( node.getNodeType() == Node.ELEMENT_NODE ) ++count;
225         }
226         return count;
227     }
228
229
230     /**
231      * Unimplemented. I think it's used by AXIS for WSDL2Java.
232      */

233     public Element JavaDoc writeSchema(Class JavaDoc javaType, Types types) throws Exception JavaDoc {
234         throw new Exception JavaDoc("unimplemented");
235     }
236 }
237
Popular Tags