KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
28 import java.io.*;
29
30 import org.kxml.*;
31 import org.kxml.io.*;
32 import org.kxml.parser.*;
33
34 import com.scalagent.ksoap.marshal.Marshal;
35
36 public class SoapReader {
37   ClassMap classMap;
38   Hashtable idMap = new Hashtable();
39   public AbstractXmlParser parser;
40   
41   class FwdRef {
42     FwdRef next;
43     Object JavaDoc obj;
44     int index;
45   }
46
47   public SoapReader(AbstractXmlParser parser,
48                     ClassMap classMap) {
49     this.parser = parser;
50     this.classMap = classMap;
51   }
52
53   public Object JavaDoc read() throws IOException {
54     if (KSoapTracing.dbgReader)
55       KSoapTracing.log(KSoapTracing.DEBUG,
56                        "SoapReader.read()");
57     String JavaDoc name = null;
58     SoapObject sO = null;
59
60     while (true) {
61       parser.skip();
62       int type = parser.peek().getType();
63
64       if (type == Xml.END_TAG || type == Xml.END_DOCUMENT) {
65         parser.skip();
66         parser.read(Xml.END_TAG, null, null);
67         break;
68       } else if (type != Xml.START_TAG) {
69         KSoapTracing.log(KSoapTracing.ERROR,
70                          "Unexpected event: " + parser.peek());
71         throw new IOException("Unexpected event: " + parser.peek());
72       }
73
74       StartTag start = (StartTag) parser.peek();
75      
76       Object JavaDoc o = read(null,
77                       -1,
78                       start.getNamespace(),
79                       start.getName(),
80                       new PropertyInfo("object",new Object JavaDoc()));
81
82       if (o != null) {
83         sO.addProperty(start.getName(),o);
84         parser.read();
85       } else {
86         sO = new SoapObject(start.getNamespace(),start.getName());
87         parser.read();
88         parser.skip();
89       }
90       name = start.getName();
91     }
92     if (KSoapTracing.dbgReader)
93       KSoapTracing.log(KSoapTracing.DEBUG,
94                        "SoapReader.read() : sO=" + sO);
95     return sO;
96   }
97
98   public Object JavaDoc read(Object JavaDoc owner,
99                      int index,
100                      String JavaDoc namespace,
101                      String JavaDoc name,
102                      PropertyInfo expected)
103     throws IOException {
104     
105     if (KSoapTracing.dbgReader)
106       KSoapTracing.log(KSoapTracing.DEBUG,
107                        "SoapReader.read(" +owner+ "," +
108                        index + "," +
109                        namespace + "," +
110                        name + "," +
111                        expected + ")");
112
113     Object JavaDoc obj = null;
114     StartTag start = (StartTag) parser.peek();
115     Attribute attr = start.getAttribute(classMap.xsi,"nil");
116       
117     if (attr == null)
118       attr = start.getAttribute(classMap.xsi,"null");
119       
120     if (KSoapTracing.dbgReader)
121       KSoapTracing.log(KSoapTracing.DEBUG,
122                        "SoapReader.read attr=" + attr);
123     if (attr != null && stringToBoolean(attr.getValue())) {
124       obj = null;
125       parser.read();
126       parser.skip();
127       parser.read(Xml.END_TAG, null, null);
128     } else {
129       attr = start.getAttribute(classMap.xsi,"type");
130       if (KSoapTracing.dbgReader)
131         KSoapTracing.log(KSoapTracing.DEBUG,
132                          "SoapReader.read attr1=" + attr);
133       if (attr != null) {
134         String JavaDoc type = attr.getValue();
135         int cut = type.indexOf(':');
136         name = type.substring(cut + 1);
137         String JavaDoc prefix = "";
138         if (cut > 0)
139           prefix = type.substring(0,cut);
140         namespace = start.getPrefixMap().getNamespace(prefix);
141         if (KSoapTracing.dbgReader)
142           KSoapTracing.log(KSoapTracing.DEBUG,
143                            "SoapReader.read type=" + type +
144                            " name=" + name +
145                            " prefix=" + prefix +
146                            " namespace=" + namespace);
147       } else if (name == null && namespace == null) {
148         if (start.getAttribute(classMap.enc,"arrayType") != null) {
149           namespace = classMap.enc;
150           name = "Array";
151         }
152       }
153       Marshal marshal = ClassMap.getSoapMarshal(new SoapPrimitive(namespace,name,null,null));
154       if (KSoapTracing.dbgReader)
155         KSoapTracing.log(KSoapTracing.DEBUG,
156                          "SoapReader.read(...) marshal=" + marshal);
157       if (marshal != null)
158         obj = marshal.readInstance(this,namespace,name,expected);
159     }
160     if (KSoapTracing.dbgReader)
161       KSoapTracing.log(KSoapTracing.DEBUG,
162                        "SoapReader.read(...) obj=" +obj);
163     return obj;
164   }
165
166   protected void readObject(SoapObject obj) throws IOException {
167     if (KSoapTracing.dbgReader)
168       KSoapTracing.log(KSoapTracing.DEBUG,
169                        "SoapReader.readObject(" + obj + ")");
170     parser.read();
171
172     int testIndex = -1;
173     int sourceIndex = 0;
174     int cnt = obj.getPropertyCount();
175     PropertyInfo info = null;
176
177     while (true) {
178       parser.skip();
179       if (parser.peek().getType() == Xml.END_TAG) break;
180
181       StartTag start = (StartTag) parser.peek();
182       String JavaDoc name = start.getName();
183       int countdown = cnt;
184
185       while (true) {
186         if (countdown-- == 0) {
187           KSoapTracing.log(KSoapTracing.ERROR,
188                            "Unknwon Property: " + name);
189           throw new RuntimeException JavaDoc("Unknwon Property: " + name);
190         }
191         
192         if (++testIndex >= cnt) testIndex = 0;
193         
194         info = obj.getPropertyInfo(testIndex);
195         if (info != null) {
196           if (info.name == null) {
197             if (testIndex == sourceIndex)
198               break;
199           } else {
200             if (info.name.equals(name))
201               break;
202           }
203         }
204       }
205       
206       obj.setProperty(testIndex,
207                       read(obj,testIndex,null,null,info));
208       sourceIndex = 0;
209     }
210     
211     parser.read(Xml.END_TAG,null,null);
212   }
213     
214   private boolean stringToBoolean(String JavaDoc s) {
215     if (s == null) return false;
216     s = s.trim().toLowerCase();
217     return (s.equals("1") || s.equals("true"));
218   }
219 }
220
Popular Tags