001: /* *****************************************************************************
002: * SWFSimpleDeserializer.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 org.openlaszlo.iv.flash.api.action.Actions;
068: import org.openlaszlo.iv.flash.api.action.Program;
069: import org.openlaszlo.iv.flash.util.FlashBuffer;
070: import org.openlaszlo.xml.internal.DataCommon;
071: import org.openlaszlo.xml.internal.DataContext;
072: import java.io.CharArrayWriter;
073: import javax.xml.namespace.QName;
074: import org.apache.axis.message.MessageElement;
075: import org.apache.axis.encoding.DeserializerImpl;
076: import org.apache.axis.encoding.DeserializationContext;
077: import org.apache.axis.message.SOAPHandler;
078: import org.apache.axis.utils.Messages;
079: import org.apache.log4j.Logger;
080: import org.xml.sax.Attributes;
081: import org.xml.sax.SAXException;
082: import java.math.BigInteger;
083: import java.math.BigDecimal;
084:
085: import org.apache.axis.Message;
086: import org.apache.axis.MessageContext;
087: import javax.xml.soap.SOAPMessage;
088:
089: // Lifted from SimpleDeserializer
090: public class SWFSimpleDeserializer extends DeserializerImpl {
091: public static Logger mLogger = Logger
092: .getLogger(SWFSimpleDeserializer.class);
093:
094: static int BUFSIZE = 8192;
095:
096: private final CharArrayWriter val = new CharArrayWriter();
097:
098: public static final Boolean TRUE = new Boolean(true);
099: public static final Boolean FALSE = new Boolean(false);
100:
101: public QName xmlType;
102: public Class javaType;
103:
104: public SWFSimpleDeserializer(Class javaType, QName xmlType) {
105: this .xmlType = xmlType;
106: this .javaType = javaType;
107: }
108:
109: public void characters(char[] chars, int start, int end)
110: throws SAXException {
111: val.write(chars, start, end);
112: }
113:
114: public void onEndElement(String namespace, String localName,
115: DeserializationContext context) throws SAXException {
116:
117: //----------------------------------------------------------------------
118: // FIXME: [2007-07-13 pkang] does this handle SOAP 1.2 fault format?
119: // If we're deserializing fault, just pass back the string value.
120: // SOAP 1.1: <faultstring>
121: //----------------------------------------------------------------------
122: if ("".equals(namespace)) {
123: if ("faultstring".equals(localName)
124: || "faultactor".equals(localName)) {
125: value = val.toString();
126: return;
127: }
128: }
129:
130: Program program = new Program(new FlashBuffer(BUFSIZE));
131:
132: //if (isNil || val == null) { -- FIX http://nagoya.apache.org/bugzilla/show_bug.cgi?id=11945
133: if (isNil) {
134: program.push((Object) null);
135: value = program;
136: return;
137: }
138:
139: String source = val.toString();
140: try {
141: if (javaType == int.class || javaType == Integer.class) {
142: program.push(Integer.parseInt(source));
143: } else if (javaType == long.class || javaType == Long.class) {
144: // push as int
145: int n = Long.valueOf(source).intValue();
146: program.push(n);
147: } else if (javaType == short.class
148: || javaType == Short.class) {
149: // push as int
150: int n = Short.valueOf(source).intValue();
151: program.push(n);
152: } else if (javaType == byte.class || javaType == Byte.class) {
153: // push as int
154: int n = Byte.valueOf(source).intValue();
155: program.push(n);
156: } else if (javaType == BigInteger.class) {
157: // push as int
158: int n = BigInteger.valueOf(Long.parseLong(source))
159: .intValue();
160: program.push(n);
161: } else if (javaType == BigDecimal.class) {
162: // push as int
163: int n = BigDecimal.valueOf(Long.parseLong(source))
164: .intValue();
165: program.push(n);
166: } else if (javaType == boolean.class
167: || javaType == Boolean.class) {
168: switch (source.charAt(0)) {
169: case '0':
170: case 'f':
171: case 'F':
172: program.push(FALSE);
173: break;
174: case '1':
175: case 't':
176: case 'T':
177: program.push(TRUE);
178: break;
179: default:
180: program.push(TRUE);
181: break;
182: }
183: } else if (javaType == float.class
184: || javaType == Float.class) {
185: if (source.equals("NaN")) {
186: program.push(Float.NaN);
187: } else if (source.equals("INF")) {
188: program.push(Float.POSITIVE_INFINITY);
189: } else if (source.equals("-INF")) {
190: program.push(Float.NEGATIVE_INFINITY);
191: } else {
192: program.push(Float.parseFloat(source));
193: }
194: } else if (javaType == double.class
195: || javaType == Double.class) {
196: if (source.equals("NaN")) {
197: program.push(new Double(Double.NaN));
198: } else if (source.equals("INF")) {
199: program.push(new Double(Double.POSITIVE_INFINITY));
200: } else if (source.equals("-INF")) {
201: program.push(new Double(Double.NEGATIVE_INFINITY));
202: } else {
203: program.push(Double.valueOf(source));
204: }
205: } else if (javaType == String.class) {
206: // treat as a string by default
207: DataContext dc = new DataContext();
208: dc.setEncoding("UTF-8");
209: DataCommon.pushStringData(source, program.body(), dc);
210: } else {
211: // catch all
212: mLogger.warn(
213: /* (non-Javadoc)
214: * @i18n.test
215: * @org-mes="treating " + p[0] + " like string: " + p[1]
216: */
217: org.openlaszlo.i18n.LaszloMessages
218: .getMessage(SWFSimpleDeserializer.class
219: .getName(), "051018-210", new Object[] {
220: javaType, source }));
221: program.push(source);
222: }
223: } catch (Exception e) {
224: mLogger.error("Exception", e);
225: throw new SAXException(e.getMessage());
226: }
227:
228: value = program;
229: }
230:
231: /**
232: * There should not be nested elements.
233: */
234: public SOAPHandler onStartChild(String namespace, String localName,
235: String prefix, Attributes attributes,
236: DeserializationContext context) throws SAXException {
237: throw new SAXException(Messages.getMessage("cantHandle00",
238: "SimpleDeserializer"));
239: }
240: }
|