KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > scalagent > kjoram > ksoap > ConversionHelper


1 /*
2  * Copyright (C) 2002 - INRIA
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA.
18  *
19  * Initial developer(s): Nicolas Tachker (ScalAgent)
20  * Contributor(s):
21  */

22 package com.scalagent.kjoram.ksoap;
23
24 import java.util.Hashtable JavaDoc;
25
26 import com.scalagent.kjoram.excepts.MessageFormatException;
27
28 /**
29  * The <code>ConversionHelper</code> class provides the methods for
30  * getting the message properties and converting them as authorized by
31  * the JMS specification.
32  */

33 class ConversionHelper {
34   static boolean getBoolean(Hashtable JavaDoc props, String JavaDoc name)
35     throws MessageFormatException {
36     if (props == null)
37       return false;
38     
39     Object JavaDoc prop = props.get(name);
40     if (prop == null)
41       return false;
42     else if (prop instanceof Boolean JavaDoc)
43       return ((Boolean JavaDoc) prop).booleanValue();
44     else if (prop instanceof String JavaDoc)
45       return ((String JavaDoc) prop).equals("true");
46     else
47       throw new MessageFormatException("Type " + prop.getClass().getName()
48                                        + " can't be converted to Boolean.");
49   }
50
51   static byte getByte(Hashtable JavaDoc props, String JavaDoc name)
52     throws MessageFormatException {
53     if (props == null)
54       return Byte.parseByte(null);
55     
56     Object JavaDoc prop = props.get(name);
57     if (prop == null)
58       return Byte.parseByte(null);
59     else if (prop instanceof Byte JavaDoc )
60       return ((Byte JavaDoc) prop).byteValue();
61     else if (prop instanceof String JavaDoc)
62       return Byte.parseByte((String JavaDoc) prop);
63     else
64       throw new MessageFormatException("Type " + prop.getClass().getName()
65                                        + " can't be converted to Byte.");
66   }
67   
68   static short getShort(Hashtable JavaDoc props, String JavaDoc name)
69     throws MessageFormatException {
70     if (props == null)
71       return Short.parseShort(null);
72     
73     Object JavaDoc prop = props.get(name);
74     if (prop == null)
75       return Short.parseShort(null);
76     else if (prop instanceof Byte JavaDoc)
77       return Short.parseShort(((Byte JavaDoc) prop).toString());
78     else if (prop instanceof Short JavaDoc)
79       return ((Short JavaDoc) prop).shortValue();
80     else if (prop instanceof String JavaDoc)
81       return Short.parseShort((String JavaDoc) prop);
82     else
83       throw new MessageFormatException("Type " + prop.getClass().getName()
84                                        + " can't be converted to Short.");
85   }
86
87   static int getInt(Hashtable JavaDoc props, String JavaDoc name)
88     throws MessageFormatException {
89     if (props == null)
90       return Integer.parseInt(null);
91
92     Object JavaDoc prop = props.get(name);
93     if (prop == null)
94       return Integer.parseInt(null);
95     else if (prop instanceof Byte JavaDoc)
96       return Integer.parseInt(((Byte JavaDoc) prop).toString());
97     else if (prop instanceof Short JavaDoc)
98       return Integer.parseInt(((Short JavaDoc) prop).toString());
99     else if (prop instanceof Integer JavaDoc)
100       return ((Integer JavaDoc) prop).intValue();
101     else if (prop instanceof String JavaDoc)
102       return Integer.parseInt((String JavaDoc) prop);
103     else
104       throw new MessageFormatException("Type " + prop.getClass().getName()
105                                        + " can't be converted to Integer.");
106   }
107
108   static long getLong(Hashtable JavaDoc props, String JavaDoc name)
109     throws MessageFormatException {
110     if (props == null)
111       return Long.parseLong(null);
112     
113     Object JavaDoc prop = props.get(name);
114     if (prop == null)
115       return Long.parseLong(null);
116     else if (prop instanceof Byte JavaDoc)
117       return Long.parseLong(((Byte JavaDoc) prop).toString());
118     else if (prop instanceof Short JavaDoc)
119       return Long.parseLong(((Short JavaDoc) prop).toString());
120     else if (prop instanceof Integer JavaDoc)
121       return Long.parseLong(((Integer JavaDoc) prop).toString());
122     else if (prop instanceof Long JavaDoc)
123       return ((Long JavaDoc) prop).longValue();
124     else if (prop instanceof String JavaDoc)
125       return Long.parseLong((String JavaDoc) prop);
126     else
127       throw new MessageFormatException("Type " + prop.getClass().getName()
128                                        + " can't be converted to Long.");
129   }
130
131
132   static String JavaDoc getString(Hashtable JavaDoc props, String JavaDoc name) {
133     if (props == null)
134       return null;
135     
136     Object JavaDoc prop = props.get(name);
137     if (prop == null)
138       return null;
139     else if (prop instanceof byte[])
140       return new String JavaDoc((byte[]) prop);
141     else
142       return prop.toString();
143   }
144
145   static char getChar(Hashtable JavaDoc map, String JavaDoc name)
146     throws MessageFormatException {
147     Object JavaDoc value = map.get(name);
148     if (value == null)
149       return ((Character JavaDoc) null).charValue();
150     else if (value instanceof Character JavaDoc)
151       return ((Character JavaDoc) value).charValue();
152     else
153       throw new MessageFormatException("Type " + value.getClass().getName()
154                                        + " can't be converted to Character.");
155   }
156
157   static byte[] getBytes(Hashtable JavaDoc map, String JavaDoc name)
158     throws MessageFormatException {
159     Object JavaDoc value = map.get(name);
160     if (value == null)
161       return (byte[]) value;
162     else if (value instanceof byte[])
163       return (byte[]) value;
164     else
165       throw new MessageFormatException("Type " + value.getClass().getName()
166                                        + " can't be converted to byte[].");
167   }
168 }
169
Popular Tags