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