KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > scalagent > ksoap > SoapEnvelope


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2003 - ScalAgent Distributed Technologies
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * The present code contributor is ScalAgent Distributed Technologies.
21  *
22  * Initial developer(s): Nicolas Tachker (ScalAgent)
23  * Contributor(s):
24  */

25 package com.scalagent.ksoap;
26
27 import java.io.*;
28
29 import org.kxml.*;
30 import org.kxml.io.*;
31 import org.kxml.parser.*;
32
33 import com.scalagent.ksoap.marshal.*;
34
35 public class SoapEnvelope {
36     
37   Object JavaDoc body;
38   ClassMap classMap;
39   String JavaDoc encodingStyle;
40
41   public SoapEnvelope() {
42     classMap = new ClassMap();
43     new MarshalBase64().register(classMap);
44     new MarshalHashtable().register(classMap);
45     new MarshalVector().register(classMap);
46   }
47
48   public void setEncodingStyle(String JavaDoc encodingStyle) {
49     this.encodingStyle = encodingStyle;
50   }
51
52   public void setBody(SoapObject body) {
53     this.body = body;
54   }
55  
56   public Object JavaDoc getBody() {
57     return body;
58   }
59
60   public void read(AbstractXmlParser parser) throws IOException {
61     if (KSoapTracing.dbgReader)
62       KSoapTracing.log(KSoapTracing.DEBUG,
63                        "SoapEnvelope.read(" + parser + ")");
64
65     readHead(parser);
66     readBody(parser);
67     readTail(parser);
68   }
69
70   public void readHead(AbstractXmlParser parser) throws IOException {
71     if (KSoapTracing.dbgReader)
72       KSoapTracing.log(KSoapTracing.DEBUG,
73                        "SoapEnvelope.readHead(" + parser + ")");
74
75     parser.skip();
76     StartTag tag = null;
77     Attribute attr = null;
78     try {
79       tag = (StartTag) parser.read(Xml.START_TAG, classMap.env,"Envelope");
80       attr = tag.getAttribute(classMap.env,"encodingStyle");
81     } catch (ParseException exc) {
82       // read text befor head
83
// Tomcat cookies,....
84
parser.read();
85       parser.skip();
86       tag = (StartTag) parser.read(Xml.START_TAG, classMap.env,"Envelope");
87       attr = tag.getAttribute(classMap.env,"encodingStyle");
88     }
89     if (attr != null) encodingStyle = attr.getValue();
90
91     parser.skip();
92     
93     if (parser.peek(Xml.START_TAG,classMap.env,"Header")) {
94       parser.read();
95       parser.skip();
96
97       while (parser.peek().getType() != Xml.END_TAG) {
98         parser.ignoreTree();
99         parser.skip();
100       }
101       
102       parser.read(Xml.END_TAG,classMap.env,"Header");
103     }
104     
105     parser.skip();
106     tag = (StartTag) parser.read(Xml.START_TAG,classMap.env,"Body");
107     attr = tag.getAttribute(classMap.env,"encodingStyle");
108     if (attr != null) encodingStyle = attr.getValue();
109   }
110
111
112   public void readBody(AbstractXmlParser parser) throws IOException {
113     if (KSoapTracing.dbgReader)
114       KSoapTracing.log(KSoapTracing.DEBUG,
115                        "SoapEnvelope.readBody(" + parser + ")");
116
117     parser.skip();
118     
119     if (parser.peek(Xml.START_TAG,classMap.env,"Fault")) {
120       SoapFault fault = new SoapFault();
121       fault.parse(parser);
122       body = fault;
123       if (KSoapTracing.dbgReader)
124         KSoapTracing.log(KSoapTracing.DEBUG,
125                          "SoapEnvelope.readBody : Fault = " + fault);
126     } else {
127       if (KSoapTracing.dbgReader)
128         KSoapTracing.log(KSoapTracing.DEBUG,
129                          "SoapEnvelope.readBody befor SoapReader(...).read()");
130       body = new SoapReader(parser,classMap).read();
131     }
132   }
133
134   public void readTail(AbstractXmlParser parser) throws IOException {
135     if (KSoapTracing.dbgReader)
136       KSoapTracing.log(KSoapTracing.DEBUG,
137                        "SoapEnvelope.readdTail(" + parser + ")");
138     parser.skip();
139     parser.read(Xml.END_TAG,classMap.env,"Body");
140     parser.skip();
141     parser.read(Xml.END_TAG,classMap.env,"Envelope");
142   }
143
144   public void write(AbstractXmlWriter writer) throws IOException {
145     if (KSoapTracing.dbgWriter)
146       KSoapTracing.log(KSoapTracing.DEBUG,
147                        "SoapEnvelope.write(" + writer + ")");
148     writer.startTag(classMap.prefixMap,classMap.env,"Envelope");
149     writer.startTag(classMap.env,"Body");
150     writer.attribute(classMap.env,"encodingStyle",
151                      encodingStyle == null ? classMap.enc : encodingStyle);
152     new SoapWriter(writer,classMap).write((SoapObject) body);
153     writer.endTag();
154     writer.endTag();
155   }
156 }
157
Popular Tags