KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* *****************************************************************************
2  * SWFObjectDeserializer.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  * Copyright 2001-2004 The Apache Software Foundation.
12  *
13  * Licensed under the Apache License, Version 2.0 (the "License");
14  * you may not use this file except in compliance with the License.
15  * You may obtain a copy of the License at
16  *
17  * http://www.apache.org/licenses/LICENSE-2.0
18  *
19  * Unless required by applicable law or agreed to in writing, software
20  * distributed under the License is distributed on an "AS IS" BASIS,
21  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22  * See the License for the specific language governing permissions and
23  * limitations under the License.
24  */

25 package org.openlaszlo.remote.soap.encoding;
26
27 import org.openlaszlo.iv.flash.util.FlashBuffer;
28 import org.openlaszlo.iv.flash.api.action.Actions;
29 import org.openlaszlo.iv.flash.api.action.Program;
30 import org.apache.axis.Constants;
31 import org.apache.axis.components.logger.LogFactory;
32 import org.apache.axis.encoding.DeserializationContext;
33 import org.apache.axis.encoding.Deserializer;
34 import org.apache.axis.encoding.DeserializerImpl;
35 import org.apache.axis.encoding.DeserializerTarget;
36 import org.apache.axis.message.SOAPHandler;
37 import org.apache.axis.utils.ClassUtils;
38 import org.apache.axis.utils.JavaUtils;
39 import org.apache.axis.utils.Messages;
40 import org.apache.axis.wsdl.symbolTable.SchemaUtils;
41 import org.apache.commons.logging.Log;
42 import org.apache.axis.soap.SOAPConstants;
43 import org.apache.axis.MessageContext;
44
45 import org.apache.axis.utils.DOM2Writer;
46
47 import org.xml.sax.Attributes JavaDoc;
48 import org.xml.sax.SAXException JavaDoc;
49
50 import javax.xml.namespace.QName JavaDoc;
51 import java.io.StringWriter JavaDoc;
52 import java.util.ArrayList JavaDoc;
53 import java.util.HashMap JavaDoc;
54 import java.util.Iterator JavaDoc;
55 import java.util.Map JavaDoc;
56 import java.util.StringTokenizer JavaDoc;
57
58 import org.apache.axis.message.MessageElement;
59 import org.apache.log4j.Logger;
60 import org.openlaszlo.iv.flash.util.FlashBuffer;
61
62
63 public class SWFObjectDeserializer extends DeserializerImpl
64 {
65     public static Logger mLogger =
66         Logger.getLogger(SWFObjectDeserializer.class);
67
68     static int BUFSIZE = 8192;
69
70     String JavaDoc mClassName = "";
71     String JavaDoc mClassNameSpace = "";
72
73     HashMap JavaDoc mMembers = new HashMap JavaDoc();
74
75     public void onStartElement(String JavaDoc namespace, String JavaDoc localName,
76                                String JavaDoc prefix, Attributes JavaDoc attributes,
77                                DeserializationContext context)
78         throws SAXException JavaDoc {
79
80         if (mLogger.isDebugEnabled()) {
81             mLogger.debug("Enter: SWFObjectDeserializer::onStartChild"
82                           + "( namespace: " + namespace
83                           + ", localname: " + localName
84                           + ", prefix: " + prefix
85                           + ")");
86         }
87
88         // Use the xsi:type setting on the attribute if it exists.
89
QName JavaDoc itemType =
90             context.getTypeFromAttributes(namespace, localName, attributes);
91
92         if (itemType == null) {
93             // FIXME: [2004-07-11 pkang] what do we do in this case? ideally
94
// treat the rest this as an element.
95
mLogger.debug("itemType is null");
96         } else {
97             mClassName = itemType.getLocalPart();
98             mClassNameSpace = itemType.getNamespaceURI();
99         }
100     }
101
102
103     public SOAPHandler onStartChild(String JavaDoc namespace, String JavaDoc localName,
104                                     String JavaDoc prefix, Attributes JavaDoc attributes,
105                                     DeserializationContext context)
106         throws SAXException JavaDoc
107     {
108         if (mLogger.isDebugEnabled()) {
109             mLogger.debug("Enter: SWFObjectDeserializer::onStartChild"
110                           + "( namespace: " + namespace
111                           + ", localname: " + localName
112                           + ", prefix: " + prefix
113                           + ")");
114         }
115
116         // Use the xsi:type setting on the attribute if it exists.
117
QName JavaDoc itemType =
118             context.getTypeFromAttributes(namespace, localName, attributes);
119
120         // Get the deserializer for the type.
121
Deserializer dSer = null;
122         if (itemType != null && (context.getCurElement().getHref() == null)) {
123             dSer = context.getDeserializerForType(itemType);
124         }
125
126
127         if (dSer == null) {
128             dSer = new SWFObjectDeserializer();
129         }
130
131         // Register the callback value target, and keep track of this index so
132
// we know when it has been set.
133
dSer.registerValueTarget(new DeserializerTarget(this, localName));
134         
135         // The framework handles knowing when the value is complete, as long as
136
// we tell it about each child we're waiting on.
137
addChildDeserializer(dSer);
138
139         return (SOAPHandler)dSer;
140     }
141
142
143     public void setChildValue(Object JavaDoc value, Object JavaDoc hint) throws SAXException JavaDoc
144     {
145         mMembers.put(hint, value);
146     }
147
148     public void valueComplete() throws SAXException JavaDoc {
149
150         if (mLogger.isDebugEnabled()) {
151             mLogger.debug("Enter: SWFObjectDeserializer::valueComplete()");
152         }
153
154         //----------------------------------------------------------------------
155
// FIXME [2005-03-11 pkang]: if there are targets and this is not an
156
// href, create the object. This is to get around a weird problem
157
// deserializing an object returned by
158
// //depot/qa/test/private/photospace test case.
159
// If top-level SOAP response node is an href, valueComplete() to get
160
// called twice. Understand this code better.
161
//----------------------------------------------------------------------
162
if (targets != null && !isHref) {
163
164             if (componentsReady()) {
165
166                 FlashBuffer fbuf = new FlashBuffer(BUFSIZE);
167                 Program program = new Program( fbuf );
168
169                 // push all members
170
Iterator JavaDoc iter = mMembers.entrySet().iterator();
171                 String JavaDoc keys = "";
172                 while (iter.hasNext()) {
173                     Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iter.next();
174                     String JavaDoc k = (String JavaDoc)entry.getKey();
175                     Program v = (Program)entry.getValue();
176                     program.push(k);
177                     // copy the body of each member's program
178
fbuf.writeFOB(v.body());
179                 }
180
181                 program.push("__LZclassnamespace");
182                 program.push(mClassNameSpace);
183                 program.push("__LZclassname");
184                 program.push(mClassName);
185                 program.push(mMembers.size() + 2);
186                 program.body().writeByte(Actions.InitObject);
187
188                 // Call _root.LzSOAPService.__LZnormObj(). This function will set the
189
// object's prototype to one that exists in the namespace and will
190
// return the object so it stays in the stack
191
program.push(1);
192                 program.push("_root");
193                 program.getVar();
194                 program.push("LzSOAPService");
195                 program.body().writeByte(Actions.GetMember);
196                 program.push("__LZnormObj");
197                 program.callMethod();
198
199                 value = program;
200             }
201
202         }
203         
204         super.valueComplete();
205
206     }
207 }
208
Popular Tags