KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.*;
29
30 import org.kxml.*;
31 import org.kxml.io.*;
32
33 import com.scalagent.ksoap.marshal.Marshal;
34
35 public class SoapWriter {
36
37   public AbstractXmlWriter writer;
38   public ClassMap classMap;
39
40   public SoapWriter(AbstractXmlWriter writer,
41                     ClassMap classMap) {
42     this.writer = writer;
43     this.classMap = classMap;
44   }
45
46   public void write(SoapObject obj) throws IOException {
47     if (KSoapTracing.dbgWriter)
48       KSoapTracing.log(KSoapTracing.DEBUG,"SoapWriter.write(" + obj + ")");
49     
50     writer.startTag(classMap.prefixMap,
51                     obj.getNamespace(),
52                     obj.getName());
53
54     if (KSoapTracing.dbgWriter)
55       KSoapTracing.log(KSoapTracing.DEBUG,"SoapWriter.write : nameSpace=" +
56                        obj.getNamespace() + ", action=" + obj.getName());
57     writer.attribute("id","o0");
58     writer.attribute(classMap.enc,"root","1");
59     writeObjectBody(obj);
60     writer.endTag();
61   }
62
63   public void writeObjectBody(SoapObject obj) throws IOException {
64     if (KSoapTracing.dbgWriter)
65       KSoapTracing.log(KSoapTracing.DEBUG,"SoapWriter.writeObjectBody(" + obj + ")");
66     PropertyInfo info = null;
67     int cnt = obj.getPropertyCount();
68     for (int i = 0; i < cnt; i++) {
69       info = obj.getPropertyInfo(i);
70       if (info != null) {
71         writer.startTag(info.name);
72         writeProperty(obj.getProperty(i),info);
73         writer.endTag();
74       }
75     }
76   }
77
78   public void writeProperty(Object JavaDoc obj,
79                                PropertyInfo propertyInfo) throws IOException {
80     if (KSoapTracing.dbgWriter)
81       KSoapTracing.log(KSoapTracing.DEBUG,"SoapWriter.writeProperty(" + obj +
82                        "," + propertyInfo + ")");
83     if (obj == null) {
84       writer.attribute(classMap.xsi,"null","true");
85       return;
86     }
87
88     if (obj instanceof SoapObject)
89       writeObjectBody((SoapObject) obj);
90     else {
91       SoapPrimitive sp = ClassMap.getSoapPrimitive(obj.getClass().getName());
92       if (sp != null) {
93         Marshal marshal = sp.getMarshal();
94         if (marshal != null) {
95           String JavaDoc prefix = writer.getPrefixMap().getPrefix(sp.getNameSpace());
96           writer.attribute(classMap.xsi,"type",prefix+":"+sp.getName());
97           marshal.writeInstance(this,obj);
98         } else {
99           KSoapTracing.log(KSoapTracing.ERROR,
100                            "can't writeProperty (undefined).");
101           throw new IOException("can't writeProperty (undefined).");
102         }
103       }
104     }
105   }
106 }
107
Popular Tags