KickJava   Java API By Example, From Geeks To Geeks.

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


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

64
65 package org.openlaszlo.remote.soap.encoding;
66
67 import org.openlaszlo.iv.flash.api.action.Actions;
68 import org.openlaszlo.iv.flash.api.action.Program;
69 import org.openlaszlo.iv.flash.util.FlashBuffer;
70 import java.io.CharArrayWriter JavaDoc;
71 import javax.xml.namespace.QName JavaDoc;
72 import org.apache.axis.message.MessageElement;
73 import org.apache.axis.encoding.DeserializerImpl;
74 import org.apache.axis.encoding.DeserializationContext;
75 import org.apache.axis.message.SOAPHandler;
76 import org.apache.axis.utils.Messages;
77 import org.apache.log4j.Logger;
78 import org.xml.sax.Attributes JavaDoc;
79 import org.xml.sax.SAXException JavaDoc;
80 import java.math.BigInteger JavaDoc;
81 import java.math.BigDecimal JavaDoc;
82
83 import org.apache.axis.Message;
84 import org.apache.axis.MessageContext;
85 import javax.xml.soap.SOAPMessage JavaDoc;
86
87
88 // Lifted from SimpleDeserializer
89
public class SWFSimpleDeserializer extends DeserializerImpl
90 {
91     public static Logger mLogger =
92         Logger.getLogger(SWFSimpleDeserializer.class);
93
94     static int BUFSIZE = 8192;
95
96     private final CharArrayWriter JavaDoc val = new CharArrayWriter JavaDoc();
97
98     public static final Boolean JavaDoc TRUE = new Boolean JavaDoc(true);
99     public static final Boolean JavaDoc FALSE = new Boolean JavaDoc(false);
100
101     public QName JavaDoc xmlType;
102     public Class JavaDoc javaType;
103
104     public SWFSimpleDeserializer(Class JavaDoc javaType, QName JavaDoc xmlType) {
105         this.xmlType = xmlType;
106         this.javaType = javaType;
107     }
108
109     public void characters(char [] chars, int start, int end)
110         throws SAXException JavaDoc {
111         val.write(chars,start,end);
112     }
113
114     public void onEndElement(String JavaDoc namespace, String JavaDoc localName,
115                              DeserializationContext context)
116         throws SAXException JavaDoc {
117
118         //----------------------------------------------------------------------
119
// FIXME: [2004-07-13 pkang] does this handle SOAP 1.2 fault format?
120
// If we're deserializing fault, just pass back the string value.
121
// SOAP 1.1: <faultstring>
122
//----------------------------------------------------------------------
123
if ("".equals(namespace)) {
124             if ( "faultstring".equals(localName) ||
125                  "faultactor".equals(localName) ) {
126                 value = val.toString();
127                 return;
128             }
129         }
130
131         Program program = new Program( new FlashBuffer(BUFSIZE) );
132
133         //if (isNil || val == null) { -- FIX http://nagoya.apache.org/bugzilla/show_bug.cgi?id=11945
134
if (isNil) {
135             program.push((Object JavaDoc)null);
136             value = program;
137             return;
138         }
139
140         String JavaDoc source = val.toString();
141         try {
142             if ( javaType == int.class || javaType == Integer JavaDoc.class) {
143                 program.push(Integer.parseInt(source));
144             } else if (javaType == long.class || javaType == Long JavaDoc.class) {
145                 // push as int
146
int n = Long.valueOf(source).intValue();
147                 program.push(n);
148             } else if (javaType == short.class || javaType == Short JavaDoc.class) {
149                 // push as int
150
int n = Short.valueOf(source).intValue();
151                 program.push(n);
152             } else if (javaType == byte.class || javaType == Byte JavaDoc.class) {
153                 // push as int
154
int n = Byte.valueOf(source).intValue();
155                 program.push(n);
156             } else if (javaType == BigInteger JavaDoc.class) {
157                 // push as int
158
int n = BigInteger.valueOf(Long.parseLong(source)).intValue();
159                 program.push(n);
160             } else if (javaType == BigDecimal JavaDoc.class) {
161                 // push as int
162
int n = BigDecimal.valueOf(Long.parseLong(source)).intValue();
163                 program.push(n);
164             } else if (javaType == boolean.class || javaType == Boolean JavaDoc.class) {
165                 switch (source.charAt(0)) {
166                 case '0': case 'f': case 'F':
167                     program.push(FALSE);
168                     break;
169                 case '1': case 't': case 'T':
170                     program.push(TRUE);
171                     break;
172                 default:
173                     program.push(TRUE);
174                     break;
175                 }
176             } else if (javaType == float.class || javaType == Float JavaDoc.class) {
177                 if (source.equals("NaN")) {
178                     program.push(Float.NaN);
179                 } else if (source.equals("INF")) {
180                     program.push(Float.POSITIVE_INFINITY);
181                 } else if (source.equals("-INF")) {
182                     program.push(Float.NEGATIVE_INFINITY);
183                 } else {
184                     program.push(Float.parseFloat(source));
185                 }
186             } else if (javaType == double.class || javaType == Double JavaDoc.class) {
187                 if (source.equals("NaN")) {
188                     program.push(new Double JavaDoc(Double.NaN));
189                 } else if (source.equals("INF")) {
190                     program.push(new Double JavaDoc(Double.POSITIVE_INFINITY));
191                 } else if (source.equals("-INF")) {
192                     program.push(new Double JavaDoc(Double.NEGATIVE_INFINITY));
193                 } else {
194                     program.push(Double.valueOf(source));
195                 }
196             } else if (javaType == String JavaDoc.class) {
197                 // treat as a string by default
198
program.push(source);
199             } else {
200                 // catch all
201
mLogger.warn("treating " + javaType + " like string: " + source);
202                 program.push(source);
203             }
204         } catch (Exception JavaDoc e) {
205             mLogger.error("Exception", e);
206             throw new SAXException JavaDoc(e.getMessage());
207         }
208
209         value = program;
210     }
211
212     /**
213      * There should not be nested elements.
214      */

215     public SOAPHandler onStartChild(String JavaDoc namespace, String JavaDoc localName,
216                                     String JavaDoc prefix, Attributes JavaDoc attributes,
217                                     DeserializationContext context)
218         throws SAXException JavaDoc {
219         throw new SAXException JavaDoc(Messages.getMessage("cantHandle00",
220                                                    "SimpleDeserializer"));
221     }
222 }
223
Popular Tags