KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.kxml.parser.*;
29 import com.scalagent.ksoap.*;
30
31 public class MarshalDefault implements Marshal {
32   
33   public Object JavaDoc readInstance(SoapReader reader,
34                              String JavaDoc namespace,
35                              String JavaDoc name,
36                              PropertyInfo expected) throws IOException {
37     if (KSoapTracing.dbg)
38       KSoapTracing.log(KSoapTracing.DEBUG,
39                        "MarshalDefault.readInstance(" + reader + "," +
40                        namespace + "," +
41                        name + "," +
42                        expected + ")");
43     
44     reader.parser.read();
45     String JavaDoc text = reader.parser.readText();
46     reader.parser.read();
47     switch (name.charAt(0)) {
48     case 's': return text;
49     case 'i': return new Integer JavaDoc(Integer.parseInt(text));
50     case 'l': return new Long JavaDoc(Long.parseLong(text));
51     case 'b': return new Boolean JavaDoc(stringToBoolean(text));
52     default:
53       KSoapTracing.log(KSoapTracing.ERROR,
54                        "MarshalDefault.readInstance(...) EXCEPTION");
55       throw new RuntimeException JavaDoc();
56     }
57   }
58
59   public void writeInstance(SoapWriter writer,
60                             Object JavaDoc instance) throws IOException {
61     if (KSoapTracing.dbg)
62       KSoapTracing.log(KSoapTracing.DEBUG,
63                        "MarshalDefault.writeInstance(" + writer +
64                        "," + instance + ")");
65     writer.writer.write(instance.toString());
66   }
67   
68   public void register(ClassMap cm) {
69     cm.addMapping(cm.xsd,"int",ClassMap.INTEGER_CLASS,this);
70     cm.addMapping(cm.xsd,"long",ClassMap.LONG_CLASS,this);
71     cm.addMapping(cm.xsd,"string",ClassMap.STRING_CLASS,this);
72     cm.addMapping(cm.xsd,"boolean",ClassMap.BOOLEAN_CLASS,this);
73   }
74
75   static boolean stringToBoolean(String JavaDoc s) {
76     if (s == null) return false;
77     s = s.trim().toLowerCase();
78     return (s.equals("1") || s.equals("true"));
79   }
80 }
81
Popular Tags