001: /* *****************************************************************************
002: * LZArraySerializer.java
003: * ****************************************************************************/
004:
005: /* J_LZ_COPYRIGHT_BEGIN *******************************************************
006: * Copyright 2001-2007 Laszlo Systems, Inc. All Rights Reserved. *
007: * Use is subject to license terms. *
008: * J_LZ_COPYRIGHT_END *********************************************************/
009:
010: /*
011: * The Apache Software License, Version 1.1
012: *
013: *
014: * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
015: * reserved.
016: *
017: * Redistribution and use in source and binary forms, with or without
018: * modification, are permitted provided that the following conditions
019: * are met:
020: *
021: * 1. Redistributions of source code must retain the above copyright
022: * notice, this list of conditions and the following disclaimer.
023: *
024: * 2. Redistributions in binary form must reproduce the above copyright
025: * notice, this list of conditions and the following disclaimer in
026: * the documentation and/or other materials provided with the
027: * distribution.
028: *
029: * 3. The end-user documentation included with the redistribution,
030: * if any, must include the following acknowledgment:
031: * "This product includes software developed by the
032: * Apache Software Foundation (http://www.apache.org/)."
033: * Alternately, this acknowledgment may appear in the software itself,
034: * if and wherever such third-party acknowledgments normally appear.
035: *
036: * 4. The names "Axis" and "Apache Software Foundation" must
037: * not be used to endorse or promote products derived from this
038: * software without prior written permission. For written
039: * permission, please contact apache@apache.org.
040: *
041: * 5. Products derived from this software may not be called "Apache",
042: * nor may "Apache" appear in their name, without prior written
043: * permission of the Apache Software Foundation.
044: *
045: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
046: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
047: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
048: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
049: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
050: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
051: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
052: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
053: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
054: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
055: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
056: * SUCH DAMAGE.
057: * ====================================================================
058: *
059: * This software consists of voluntary contributions made by many
060: * individuals on behalf of the Apache Software Foundation. For more
061: * information on the Apache Software Foundation, please see
062: * <http://www.apache.org/>.
063: */
064:
065: package org.openlaszlo.remote.swf.soap.encoding;
066:
067: import javax.xml.namespace.QName;
068: import java.io.IOException;
069: import org.apache.axis.encoding.SerializationContext;
070: import org.apache.axis.encoding.Serializer;
071: import org.apache.axis.wsdl.fromJava.Types;
072: import org.w3c.dom.Element;
073: import org.w3c.dom.Node;
074: import org.w3c.dom.NodeList;
075: import org.xml.sax.Attributes;
076: import org.apache.log4j.Logger;
077: import org.openlaszlo.remote.swf.soap.ArrayWrapper;
078: import org.xml.sax.helpers.AttributesImpl;
079: import java.util.ArrayList;
080: import org.openlaszlo.remote.swf.soap.*;
081: import org.apache.axis.MessageContext;
082: import org.apache.axis.schema.SchemaVersion;
083: import org.apache.axis.soap.SOAPConstants;
084: import org.apache.axis.Constants;
085:
086: public class LZArraySerializer implements Serializer {
087:
088: private static Logger mLogger = Logger
089: .getLogger(LZArraySerializer.class);
090:
091: public static String MECHANISM_TYPE = "LZArrayMechanism";
092:
093: public String getMechanismType() {
094: return MECHANISM_TYPE;
095: }
096:
097: public void serialize(QName name, Attributes attributes,
098: Object value, SerializationContext context)
099: throws IOException {
100: mLogger.debug(
101: /* (non-Javadoc)
102: * @i18n.test
103: * @org-mes="serialize(" + p[0] + "," + p[1] + "," + p[2] + "," + p[3] + ")"
104: */
105: org.openlaszlo.i18n.LaszloMessages.getMessage(
106: LZArraySerializer.class.getName(), "051018-104",
107: new Object[] { name, attributes, value, context }));
108: // Alot of this code copied from
109: // org.apache.axis.encoding.ser.ArraySerializer
110:
111: MessageContext msgContext = context.getMessageContext();
112: SchemaVersion schema = SchemaVersion.SCHEMA_2001;
113: SOAPConstants soap = SOAPConstants.SOAP11_CONSTANTS;
114: boolean encoded = true;
115: if (msgContext != null) {
116: encoded = msgContext.isEncoded();
117: schema = msgContext.getSchemaVersion();
118: soap = msgContext.getSOAPConstants();
119: }
120:
121: ArrayWrapper aw = (ArrayWrapper) ((ArrayList) value).get(0);
122: Element el = aw.getElement();
123: ComplexType ct = aw.getType();
124:
125: NodeList list = el.getChildNodes();
126: int len = getArrayLen(list);
127:
128: String brackets = "";
129: if (encoded) {
130: if (soap == SOAPConstants.SOAP12_CONSTANTS)
131: brackets += len;
132: else
133: brackets += "[" + len + "]";
134:
135: AttributesImpl attrs;
136: if (attributes == null) {
137: attrs = new AttributesImpl();
138: } else {
139: attrs = new AttributesImpl(attributes);
140: }
141:
142: QName componentQName = ct.getArrayItemTypeQName();
143: String compType = context
144: .attributeQName2String(componentQName);
145:
146: if (attrs.getIndex(soap.getEncodingURI(), soap
147: .getAttrItemType()) == -1) {
148: String encprefix = context.getPrefixForURI(soap
149: .getEncodingURI());
150:
151: if (soap != SOAPConstants.SOAP12_CONSTANTS) {
152: compType = compType + brackets;
153:
154: attrs.addAttribute(soap.getEncodingURI(), soap
155: .getAttrItemType(), encprefix
156: + ":arrayType", "CDATA", compType);
157:
158: } else {
159: attrs.addAttribute(soap.getEncodingURI(), soap
160: .getAttrItemType(),
161: encprefix + ":itemType", "CDATA", compType);
162:
163: attrs.addAttribute(soap.getEncodingURI(),
164: "arraySize", encprefix + ":arraySize",
165: "CDATA", brackets);
166: }
167: }
168:
169: // NOTE: comments here copied from axis code. -pk
170: //
171: // Force type to be SOAP_ARRAY for all array serialization.
172: //
173: // There are two choices here:
174: // Force the type to type=SOAP_ARRAY
175: // Pros: More interop test successes.
176: // Cons: Since we have specific type information it
177: // is more correct to use it. Plus the specific
178: // type information may be important on the
179: // server side to disambiguate overloaded operations.
180: // Use the specific type information:
181: // Pros: The specific type information is more correct
182: // and may be useful for operation overloading.
183: // Cons: More interop test failures (as of 2/6/2002).
184: //
185: int typeI = attrs.getIndex(schema.getXsiURI(), "type");
186: if (typeI != -1) {
187: String qname = context.getPrefixForURI(schema
188: .getXsiURI(), "xsi")
189: + ":type";
190: QName soapArray;
191: if (soap == SOAPConstants.SOAP12_CONSTANTS) {
192: soapArray = Constants.SOAP_ARRAY12;
193: } else {
194: soapArray = Constants.SOAP_ARRAY;
195: }
196:
197: attrs
198: .setAttribute(typeI, schema.getXsiURI(),
199: "type", qname, "CDATA", context
200: .qName2String(soapArray));
201: }
202: attributes = attrs;
203: }
204:
205: context.startElement(name, attributes);
206: for (int i = 0; i < list.getLength(); i++) {
207: // Now copy each child element in the array. -pk
208: Node node = (Node) list.item(i);
209: if (node.getNodeType() == Node.ELEMENT_NODE) {
210: context.writeDOMElement((Element) node);
211: }
212: }
213: context.endElement();
214: }
215:
216: /**
217: * @return number of items in array.
218: */
219: public int getArrayLen(NodeList list) {
220: int count = 0;
221: for (int i = 0; i < list.getLength(); i++) {
222: Node node = (Node) list.item(i);
223: if (node.getNodeType() == Node.ELEMENT_NODE)
224: ++count;
225: }
226: return count;
227: }
228:
229: /**
230: * Unimplemented. I think it's used by AXIS for WSDL2Java.
231: */
232: public Element writeSchema(Class javaType, Types types)
233: throws Exception {
234: throw new Exception(
235: /* (non-Javadoc)
236: * @i18n.test
237: * @org-mes="unimplemented"
238: */
239: org.openlaszlo.i18n.LaszloMessages.getMessage(
240: LZArraySerializer.class.getName(), "051018-244"));
241: }
242: }
|