KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
32 import org.kxml.io.*;
33 import org.kxml.parser.*;
34
35
36 public class MarshalVector implements Marshal {
37   public static final PropertyInfo OBJECT_TYPE =
38       new PropertyInfo("object",new Object JavaDoc());
39
40   public Object JavaDoc readInstance(SoapReader reader,
41                              String JavaDoc namespace,
42                              String JavaDoc name,
43                              PropertyInfo expected) throws IOException {
44
45     if (KSoapTracing.dbg)
46       KSoapTracing.log(KSoapTracing.DEBUG,
47                        "MarshalVector.readInstance(" + reader + "," +
48                        namespace + "," +
49                        name + "," +
50                        expected + ")");
51
52     Vector instance = new Vector();
53     int i = 0;
54     AbstractXmlParser xmlParser = reader.parser;
55     SoapObject sO = new SoapObject(null,null);
56     sO.addProperty(new PropertyInfo("item",OBJECT_TYPE),null);
57
58     StartTag start = (StartTag) xmlParser.peek();
59     String JavaDoc toParse = start.getAttribute(ClassMap.enc,"arrayType").getValue();
60     int cut = toParse.indexOf(":Map[");
61     toParse = toParse.substring(cut+5);
62     cut = toParse.indexOf("]");
63     toParse = toParse.substring(0,cut);
64     int size = Integer.parseInt(toParse);
65
66     xmlParser.read();
67     xmlParser.skip();
68
69     while (xmlParser.peek().getType() != Xml.END_TAG) {
70       start = (StartTag) xmlParser.peek();
71       Attribute attr = start.getAttribute(ClassMap.xsi,"type");
72       if (attr != null) {
73         String JavaDoc type = attr.getValue();
74         cut = type.indexOf(':');
75         name = type.substring(cut + 1);
76         String JavaDoc prefix = "";
77         if (cut > 0)
78           prefix = type.substring(0,cut);
79         namespace = start.getPrefixMap().getNamespace(prefix);
80       }
81       
82       Object JavaDoc value =
83         reader.read(null,-1,namespace,name,OBJECT_TYPE);
84       reader.parser.skip();
85
86       if (value != null) {
87         sO.setProperty(i++,value);
88         if (size == i) {
89           xmlParser.read();
90           xmlParser.skip();
91           break;
92         } else {
93           continue;
94         }
95       }
96       xmlParser.read();
97       xmlParser.skip();
98     }
99
100     for (int j = 0; j < sO.getPropertyCount(); j++)
101       instance.addElement(sO.getProperty(j));
102     
103     if (KSoapTracing.dbg)
104       KSoapTracing.log(KSoapTracing.DEBUG,
105                        "MarshalVector : instance=" + instance);
106     return instance;
107   }
108   
109   public void writeInstance(SoapWriter writer,
110                             Object JavaDoc instance) throws IOException {
111     if (KSoapTracing.dbg)
112       KSoapTracing.log(KSoapTracing.DEBUG,
113                        "MarshalVector.writeInstance(" + writer +
114                        "," + instance + ")");
115
116     Vector vector = (Vector) instance;
117     int cnt = vector.size();
118
119     SoapPrimitive sp =
120       ClassMap.getSoapPrimitive(instance.getClass().getName());
121     if (cnt > 0) {
122       sp = ClassMap.getSoapPrimitive(vector.firstElement().getClass().getName());
123     }
124
125     writer.writer.attribute(ClassMap.enc,"arrayType",
126                             "xsd:anyType[" + cnt + "]");
127
128     SoapObject so = new SoapObject(null,null);
129     for (int i = 0; i < cnt; i++) {
130       Object JavaDoc elem = vector.elementAt(i);
131       so.addProperty(new PropertyInfo("item",elem),null);
132       so.setProperty(0,elem);
133       writer.writeObjectBody(so);
134     }
135   }
136   
137   public void register(ClassMap cm) {
138     cm.addMapping(ClassMap.enc,
139                   "Array",
140                   ClassMap.VECTOR_CLASS,
141                   this);
142   }
143 }
144
Popular Tags