KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > remote > soap > LZSOAPUtils


1 /* *****************************************************************************
2  * LZSOAPUtils.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 package org.openlaszlo.remote.soap;
11
12 import javax.xml.namespace.QName JavaDoc;
13 import org.openlaszlo.iv.flash.util.*;
14 import org.openlaszlo.iv.flash.api.action.*;
15 import org.openlaszlo.iv.flash.api.*;
16 import org.openlaszlo.xml.internal.DataCommon;
17 import org.openlaszlo.xml.internal.DataContext;
18 import java.io.IOException JavaDoc;
19 import java.io.StringReader JavaDoc;
20 import javax.xml.parsers.DocumentBuilder JavaDoc;
21 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
22 import javax.xml.parsers.ParserConfigurationException JavaDoc;
23 import org.apache.log4j.Logger;
24 import org.w3c.dom.Element JavaDoc;
25 import org.w3c.dom.NodeList JavaDoc;
26 import org.xml.sax.InputSource JavaDoc;
27 import org.xml.sax.SAXException JavaDoc;
28 import org.apache.axis.utils.XMLUtils;
29
30
31 public class LZSOAPUtils {
32
33     private static Logger mLogger = Logger.getLogger(LZSOAPUtils.class);
34
35     /**
36      * Get prefix if it exists.
37      *
38      * @param tag tag string.
39      * @return prefix part of tag name, if it exists, else empty string.
40      */

41     static public String JavaDoc getPrefix(String JavaDoc tag) {
42         int index = tag.indexOf(':');
43         if (index == -1) return "";
44         return tag.substring(0, index);
45     }
46
47     /**
48      * Get local part of tag.
49      *
50      * @param tag tag string.
51      * @return local part of tag.
52      */

53     static public String JavaDoc getLocal(String JavaDoc tag) {
54         int index = tag.indexOf(':');
55         if (index == -1) return tag;
56         return tag.substring(index+1);
57     }
58
59     static public Element JavaDoc xmlStringToElement(String JavaDoc xml)
60         throws IOException JavaDoc, SAXException JavaDoc {
61
62         DocumentBuilder JavaDoc builder = null;
63         try {
64             builder = XMLUtils.getDocumentBuilder();
65             return builder.parse(new InputSource JavaDoc( new StringReader JavaDoc(xml)))
66                 .getDocumentElement();
67         } catch (ParserConfigurationException JavaDoc e) {
68             throw new SAXException JavaDoc("can't create document builder");
69         } finally {
70             if (builder != null) {
71                 XMLUtils.releaseDocumentBuilder(builder);
72             }
73         }
74
75     }
76
77     static public void pushString(Program program, String JavaDoc str, DataContext dc) {
78         if (str == null) {
79             pushNull(program.body());
80             return;
81         }
82         DataCommon.pushStringData(str, program.body(), dc);
83     }
84
85     static public void pushNull(FlashBuffer body) {
86         body.writeByte(Actions.PushData);
87         body.writeWord(0+1);
88         body.writeByte(2);
89     }
90
91     static public void pushQName(Program program, QName JavaDoc qname, DataContext dc) {
92         FlashBuffer body = program.body();
93         if (qname == null) {
94             pushNull(body);
95             return;
96         }
97         // this can be optimized
98
DataCommon.pushStringData(qname.getNamespaceURI(), body, dc);
99         DataCommon.pushStringData(qname.getLocalPart(), body, dc);
100         program.push(2);
101         DataCommon.pushStringData("_root.QName", body, dc);
102         program.newObject();
103     }
104
105
106     /**
107      * Get first element by namespace within element. If namespace is null, get
108      * first element in owner element's namespace.
109      *
110      * @param ns namespace.
111      * @param element owner element to search tags from.
112      * @param tag tag name.
113      * @return first element found, or null if none matched.
114      */

115     static Element JavaDoc getFirstElementByTagNameNS(String JavaDoc ns, Element JavaDoc element, String JavaDoc tag) {
116         if (element == null)
117             return null;
118
119         NodeList JavaDoc list;
120         if (ns == null)
121             list = element.getElementsByTagName(tag);
122         else
123             list = element.getElementsByTagNameNS(ns, tag);
124
125         if (list.getLength() == 0)
126             return null;
127         return (Element JavaDoc)list.item(0);
128     }
129
130     /**
131      * See getFirstElementByTagNameNS().
132      */

133     static Element JavaDoc getFirstElementByTagName(Element JavaDoc element, String JavaDoc tag) {
134         return getFirstElementByTagNameNS(null, element, tag);
135     }
136
137 }
138
Popular Tags