KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > scalagent > ksoap > marshal > MarshalHashtable


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.marshal;
26
27 import java.io.*;
28 import java.util.*;
29
30 import com.scalagent.ksoap.*;
31 import org.kxml.Xml;
32 import org.kxml.parser.*;
33
34
35 public class MarshalHashtable implements Marshal {
36   public static final PropertyInfo OBJECT_TYPE =
37       new PropertyInfo("object",new Object JavaDoc());
38
39   public Object JavaDoc readInstance(SoapReader reader,
40                              String JavaDoc namespace,
41                              String JavaDoc name,
42                              PropertyInfo expected) throws IOException {
43     if (KSoapTracing.dbg)
44       KSoapTracing.log(KSoapTracing.DEBUG,
45                        "MarshalHashtable.readInstance(" + reader + "," +
46                        namespace + "," +
47                        name + "," +
48                        expected + ")");
49     Hashtable instance = new Hashtable();
50     AbstractXmlParser xmlParser = reader.parser;
51
52     xmlParser.read();
53     xmlParser.skip();
54     while (xmlParser.peek().getType() != Xml.END_TAG) {
55       SoapObject item = new ItemSoapObject(instance);
56       
57       xmlParser.read();
58       xmlParser.skip();
59       
60       Object JavaDoc key =
61         reader.read(item,0,null,null,OBJECT_TYPE);
62       reader.parser.skip();
63       if (key != null)
64         item.setProperty(0,key);
65       
66       Object JavaDoc value =
67         reader.read(item,1,null,null,OBJECT_TYPE);
68       reader.parser.skip();
69       if (value != null)
70         item.setProperty(1,value);
71       
72       xmlParser.read();
73       xmlParser.skip();
74     }
75     
76     reader.parser.read();
77
78     if (KSoapTracing.dbg)
79       KSoapTracing.log(KSoapTracing.DEBUG,
80                        "MarshalHashtable : instance=" + instance);
81     return instance;
82   }
83   
84   public void writeInstance(SoapWriter writer,
85                             Object JavaDoc instance) throws IOException {
86     if (KSoapTracing.dbg)
87       KSoapTracing.log(KSoapTracing.DEBUG,
88                        "MarshalHashtable.writeInstance(" + writer +
89                        "," + instance + ")");
90
91     Hashtable h = (Hashtable) instance;
92     SoapObject item = new SoapObject(null, null);
93     item.addProperty(new PropertyInfo("key",OBJECT_TYPE),null);
94     item.addProperty(new PropertyInfo("value",OBJECT_TYPE),null);
95     for (Enumeration keys = h.keys(); keys.hasMoreElements();) {
96       writer.writer.startTag("item");
97       Object JavaDoc key = keys.nextElement();
98       item.setProperty(0,key);
99       item.setProperty(1,h.get(key));
100       writer.writeObjectBody(item);
101       writer.writer.endTag();
102     }
103   }
104
105   class ItemSoapObject extends SoapObject {
106     Hashtable h;
107     int resolvedIndex = -1;
108     
109     ItemSoapObject(Hashtable h) {
110       super(null, null);
111       this.h = h;
112       addProperty(new PropertyInfo("key",OBJECT_TYPE),null);
113       addProperty(new PropertyInfo("value",OBJECT_TYPE),null);
114     }
115     
116     public void setProperty(int index, Object JavaDoc value) {
117       if (resolvedIndex == -1) {
118         super.setProperty(index,value);
119         resolvedIndex = index;
120       } else {
121         Object JavaDoc resolved =
122           resolvedIndex == 0
123           ? getProperty (0)
124           : getProperty (1);
125         
126         if (index == 0)
127           h.put(value,resolved);
128         else
129           h.put(resolved,value);
130       }
131     }
132   }
133   
134   public void register(ClassMap cm) {
135     cm.addMapping("http://xml.apache.org/xml-soap",
136                   "Map",
137                   new Hashtable().getClass(),
138                   this);
139   }
140 }
141
Popular Tags