001: /* *****************************************************************************
002: * JSONObjectDeserializer.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: * Copyright 2001-2007 The Apache Software Foundation.
012: *
013: * Licensed under the Apache License, Version 2.0 (the "License");
014: * you may not use this file except in compliance with the License.
015: * You may obtain a copy of the License at
016: *
017: * http://www.apache.org/licenses/LICENSE-2.0
018: *
019: * Unless required by applicable law or agreed to in writing, software
020: * distributed under the License is distributed on an "AS IS" BASIS,
021: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
022: * See the License for the specific language governing permissions and
023: * limitations under the License.
024: */
025: package org.openlaszlo.remote.json.soap.encoding;
026:
027: import org.openlaszlo.remote.json.soap.ObjectWrapper;
028: import org.openlaszlo.sc.ScriptCompiler;
029: import org.openlaszlo.remote.json.soap.LZSOAPUtils;
030: import org.apache.axis.Constants;
031: import org.apache.axis.components.logger.LogFactory;
032: import org.apache.axis.encoding.DeserializationContext;
033: import org.apache.axis.encoding.Deserializer;
034: import org.apache.axis.encoding.DeserializerImpl;
035: import org.apache.axis.encoding.DeserializerTarget;
036: import org.apache.axis.message.SOAPHandler;
037: import org.apache.axis.utils.ClassUtils;
038: import org.apache.axis.utils.JavaUtils;
039: import org.apache.axis.utils.Messages;
040: import org.apache.axis.wsdl.symbolTable.SchemaUtils;
041: import org.apache.commons.logging.Log;
042: import org.apache.axis.soap.SOAPConstants;
043: import org.apache.axis.MessageContext;
044:
045: import org.apache.axis.utils.DOM2Writer;
046:
047: import org.xml.sax.Attributes;
048: import org.xml.sax.SAXException;
049:
050: import javax.xml.namespace.QName;
051: import java.io.StringWriter;
052: import java.util.ArrayList;
053: import java.util.HashMap;
054: import java.util.Iterator;
055: import java.util.Map;
056: import java.util.StringTokenizer;
057:
058: import org.apache.axis.message.MessageElement;
059: import org.apache.log4j.Logger;
060: import org.openlaszlo.iv.flash.util.FlashBuffer;
061:
062: public class JSONObjectDeserializer extends DeserializerImpl {
063: public static Logger mLogger = Logger
064: .getLogger(JSONObjectDeserializer.class);
065:
066: String mClassName = "";
067: String mClassNameSpace = "";
068:
069: HashMap mMembers = new HashMap();
070:
071: public void onStartElement(String namespace, String localName,
072: String prefix, Attributes attributes,
073: DeserializationContext context) throws SAXException {
074:
075: if (mLogger.isDebugEnabled()) {
076: mLogger.debug(
077: /* (non-Javadoc)
078: * @i18n.test
079: * @org-mes="Enter: JSONObjectDeserializer::onStartChild" + "( namespace: " + p[0] + ", localname: " + p[1] + ", prefix: " + p[2] + ")"
080: */
081: org.openlaszlo.i18n.LaszloMessages.getMessage(
082: JSONObjectDeserializer.class.getName(),
083: "051018-87", new Object[] { namespace, localName,
084: prefix }));
085: }
086:
087: // Use the xsi:type setting on the attribute if it exists.
088: QName itemType = context.getTypeFromAttributes(namespace,
089: localName, attributes);
090:
091: if (itemType == null) {
092: // FIXME: [2007-07-11 pkang] what do we do in this case? ideally
093: // treat the rest this as an element.
094: mLogger.debug(
095: /* (non-Javadoc)
096: * @i18n.test
097: * @org-mes="itemType is null"
098: */
099: org.openlaszlo.i18n.LaszloMessages.getMessage(
100: JSONObjectDeserializer.class.getName(),
101: "051018-104"));
102: } else {
103: mClassName = itemType.getLocalPart();
104: mClassNameSpace = itemType.getNamespaceURI();
105: }
106: }
107:
108: public SOAPHandler onStartChild(String namespace, String localName,
109: String prefix, Attributes attributes,
110: DeserializationContext context) throws SAXException {
111: if (mLogger.isDebugEnabled()) {
112: mLogger.debug(
113: /* (non-Javadoc)
114: * @i18n.test
115: * @org-mes="Enter: JSONObjectDeserializer::onStartChild" + "( namespace: " + p[0] + ", localname: " + p[1] + ", prefix: " + p[2] + ")"
116: */
117: org.openlaszlo.i18n.LaszloMessages.getMessage(
118: JSONObjectDeserializer.class.getName(),
119: "051018-87", new Object[] { namespace, localName,
120: prefix }));
121: }
122:
123: // Use the xsi:type setting on the attribute if it exists.
124: QName itemType = context.getTypeFromAttributes(namespace,
125: localName, attributes);
126:
127: // Get the deserializer for the type.
128: Deserializer dSer = null;
129: if (itemType != null
130: && (context.getCurElement().getHref() == null)) {
131: dSer = context.getDeserializerForType(itemType);
132: }
133:
134: if (dSer == null) {
135: dSer = new JSONObjectDeserializer();
136: }
137:
138: // Register the callback value target, and keep track of this index so
139: // we know when it has been set.
140: dSer
141: .registerValueTarget(new DeserializerTarget(this ,
142: localName));
143:
144: // The framework handles knowing when the value is complete, as long as
145: // we tell it about each child we're waiting on.
146: addChildDeserializer(dSer);
147:
148: return (SOAPHandler) dSer;
149: }
150:
151: public void setChildValue(Object value, Object hint)
152: throws SAXException {
153: mMembers.put(hint, value);
154: }
155:
156: public void valueComplete() throws SAXException {
157:
158: if (mLogger.isDebugEnabled()) {
159: mLogger.debug(
160: /* (non-Javadoc)
161: * @i18n.test
162: * @org-mes="Enter: JSONObjectDeserializer::valueComplete()"
163: */
164: org.openlaszlo.i18n.LaszloMessages.getMessage(
165: JSONObjectDeserializer.class.getName(),
166: "051018-170"));
167: }
168:
169: //----------------------------------------------------------------------
170: // FIXME [2005-03-11 pkang]: if there are targets and this is not an
171: // href, create the object. This is to get around a weird problem
172: // deserializing an object returned by
173: // //depot/qa/test/private/photospace test case.
174: // If top-level SOAP response node is an href, valueComplete() to get
175: // called twice. Understand this code better.
176: //----------------------------------------------------------------------
177: if (targets != null && !isHref) {
178:
179: if (componentsReady()) {
180:
181: StringBuffer body = new StringBuffer();
182:
183: // push all members
184: Iterator iter = mMembers.entrySet().iterator();
185: String keys = "";
186: body.append("LzSOAPService.__LZnormObj(");
187: body.append("{");
188: int count = 0;
189: while (iter.hasNext()) {
190: Map.Entry entry = (Map.Entry) iter.next();
191: String k = (String) entry.getKey();
192:
193: Object v = entry.getValue();
194:
195: if (count++ > 0) {
196: body.append(",");
197: }
198: body.append(ScriptCompiler.quote(k) + ": ");
199: if (v == null) {
200: body.append("null");
201: } else {
202: if (v.getClass().isArray()) {
203: Object[] p = (Object[]) v;
204: body.append("null");
205: for (int z = 0; z < p.length; z++) {
206: Object ow = p[z];
207: mLogger.debug("[5.5.2] p[" + z + "]: "
208: + ow.toString());
209: }
210:
211: } else {
212: // copy the body of each member's program
213: body.append(v.toString());
214: }
215: }
216: }
217:
218: body.append(",");
219: body.append("__LZclassnamespace: ");
220: body.append(ScriptCompiler.quote(mClassNameSpace));
221: body.append(",");
222: body.append("__LZclassname: ");
223: body.append(ScriptCompiler.quote(mClassName));
224: body.append("})");
225:
226: // Call LzSOAPService.__LZnormObj(). This function will set the
227: // object's prototype to one that exists in the namespace and will
228: // return the object so it stays in the stack
229:
230: value = body.toString();
231: }
232:
233: }
234: super.valueComplete();
235:
236: }
237: }
|