KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xmlrpc > DefaultTypeFactory


1 /*
2  * Copyright 1999,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.xmlrpc;
19
20
21 import java.text.ParseException JavaDoc;
22
23 import org.apache.commons.codec.binary.Base64;
24 import org.apache.commons.codec.DecoderException;
25 import org.apache.xmlrpc.util.DateTool;
26
27 /**
28  * The default implementation of the <code>TypeFactory</code>
29  * interface. Provides the following mappings:
30  *
31  * <table cellpadding="3" cellspacing="2" border="1" width="100%">
32  * <tr><th>XML-RPC data type</th> <th>Java class</th></tr>
33  * <tr><td>&lt;i4&gt; or &lt;int&gt;</td> <td>java.lang.Integer</td></tr>
34  * <tr><td>&lt;boolean&gt;</td> <td>java.lang.Boolean</td></tr>
35  * <tr><td>&lt;string&gt;</td> <td>java.lang.String</td></tr>
36  * <tr><td>&lt;double&gt;</td> <td>java.lang.Double</td></tr>
37  * <tr><td>&lt;dateTime.iso8601&gt;</td> <td>java.util.Date</td></tr>
38  * <tr><td>&lt;base64&gt;</td> <td>byte[ ]</td></tr>
39  * </table>
40  *
41  * @author <a HREF="mailto:andrew@kungfoocoder.org">Andrew Evers</a>
42  * @see org.apache.xmlrpc.TypeFactory
43  * @since 1.2
44  */

45 public class DefaultTypeFactory
46     implements TypeFactory
47 {
48     /**
49      * Thread-safe wrapper for the <code>DateFormat</code> object used
50      * to parse date/time values.
51      */

52     private static DateTool dateTool = new DateTool();
53     private static final Base64 base64Codec = new Base64();
54
55     /**
56      * Creates a new instance.
57      */

58     public DefaultTypeFactory()
59     {
60     }
61
62     public Object JavaDoc createInteger(String JavaDoc cdata)
63     {
64         return new Integer JavaDoc(cdata.trim());
65     }
66
67     public Object JavaDoc createBoolean(String JavaDoc cdata)
68     {
69         return ("1".equals(cdata.trim ())
70                ? Boolean.TRUE : Boolean.FALSE);
71     }
72
73     public Object JavaDoc createDouble(String JavaDoc cdata)
74     {
75         return new Double JavaDoc(cdata.trim ());
76
77     }
78
79     public Object JavaDoc createDate(String JavaDoc cdata)
80     {
81         try
82         {
83             return dateTool.parse(cdata.trim());
84         }
85         catch (ParseException JavaDoc p)
86         {
87             throw new RuntimeException JavaDoc(p.getMessage());
88         }
89     }
90
91     public Object JavaDoc createBase64(String JavaDoc cdata)
92     {
93         try
94         {
95             return base64Codec.decode((Object JavaDoc) cdata.getBytes());
96         }
97         catch (DecoderException e) {
98             //TODO: consider throwing an exception here?
99
return new byte[0];
100         }
101     }
102
103     public Object JavaDoc createString(String JavaDoc cdata)
104     {
105         return cdata;
106     }
107 }
108
Popular Tags