001: /* *****************************************************************************
002: * JSONArrayDeserializer.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:
026: package org.openlaszlo.remote.json.soap.encoding;
027:
028: import org.apache.axis.encoding.DeserializationContext;
029: import org.apache.axis.encoding.ser.ArrayDeserializer;
030: import org.apache.axis.message.SOAPHandler;
031: import org.openlaszlo.sc.ScriptCompiler;
032:
033: import java.util.ArrayList;
034: import org.apache.log4j.Logger;
035: import org.xml.sax.Attributes;
036: import org.xml.sax.SAXException;
037:
038: public class JSONArrayDeserializer extends ArrayDeserializer {
039: public static Logger mLogger = Logger
040: .getLogger(JSONArrayDeserializer.class);
041:
042: // array item tag to use (in LFC, see valueToElement() in
043: // data/LzDataElement.as)
044: String mItemTag = null;
045:
046: /**
047: * onStartChild is called on each child element.
048: * @param namespace is the namespace of the child element
049: * @param localName is the local name of the child element
050: * @param prefix is the prefix used on the name of the child element
051: * @param attributes are the attributes of the child element
052: * @param context is the deserialization context.
053: * @return is a Deserializer to use to deserialize a child (must be
054: * a derived class of SOAPHandler) or null if no deserialization should
055: * be performed.
056: */
057: public SOAPHandler onStartChild(String namespace, String localName,
058: String prefix, Attributes attributes,
059: DeserializationContext context) throws SAXException {
060:
061: if (mItemTag == null)
062: mItemTag = localName;
063:
064: return super .onStartChild(namespace, localName, prefix,
065: attributes, context);
066:
067: }
068:
069: /**
070: * When valueComplete() is invoked on the array,
071: * first convert the array value into the expected array.
072: * Then call super.valueComplete() to inform referents
073: * that the array value is ready.
074: **/
075: public void valueComplete() throws SAXException {
076:
077: /*
078: // How to print a stack trace to the log:
079: RuntimeException st = new RuntimeException();
080: java.io.StringWriter sw = new java.io.StringWriter();
081: java.io.PrintWriter ow = new java.io.PrintWriter(sw);
082: st.printStackTrace(ow);
083: ow.flush();
084: mLogger.debug(sw.toString());
085: */
086:
087: if (componentsReady()) {
088: try {
089: ArrayList list = (ArrayList) value;
090: StringBuffer body = new StringBuffer();
091: int alen = list.size();
092: int count = 0;
093: body.append("LzSOAPService.__LZarray([");
094: while (count < alen) {
095: body.append((String) (list.get(count)));
096: if (++count < alen) {
097: body.append(",");
098: }
099: }
100:
101: body
102: .append("]," + ScriptCompiler.quote(mItemTag)
103: + ")");
104: value = body.toString();
105:
106: mLogger
107: .debug("JSONArrayDeserializer valueComplete value.class = "
108: + value.getClass() + " value=" + value);
109:
110: } catch (RuntimeException e) {
111: // We must ignore exceptions from convert for Arrays with null - why?
112: }
113: }
114: super.valueComplete();
115: }
116: }
|