KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > impl > DatatypeConverterImpl


1 /*
2  * Copyright 2003,2004 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 package org.apache.ws.jaxme.impl;
18
19 import java.io.IOException JavaDoc;
20 import java.math.BigDecimal JavaDoc;
21 import java.math.BigInteger JavaDoc;
22 import java.text.ParsePosition JavaDoc;
23 import java.util.Calendar JavaDoc;
24
25 import javax.xml.XMLConstants JavaDoc;
26 import javax.xml.bind.DatatypeConverterInterface;
27 import javax.xml.namespace.NamespaceContext JavaDoc;
28 import javax.xml.namespace.QName JavaDoc;
29
30 import org.apache.ws.jaxme.util.Base64Binary;
31 import org.apache.ws.jaxme.util.Duration;
32 import org.apache.ws.jaxme.util.HexBinary;
33 import org.apache.ws.jaxme.xs.util.XsDateFormat;
34 import org.apache.ws.jaxme.xs.util.XsDateTimeFormat;
35 import org.apache.ws.jaxme.xs.util.XsTimeFormat;
36
37 /**
38  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
39  */

40 public class DatatypeConverterImpl implements DatatypeConverterInterface {
41     public String JavaDoc parseString(String JavaDoc arg0) {
42         return arg0;
43     }
44     
45     public BigInteger JavaDoc parseInteger(String JavaDoc arg0) {
46         return new BigInteger JavaDoc(arg0);
47     }
48     
49     public int parseInt(String JavaDoc arg0) {
50         return Integer.parseInt(arg0);
51     }
52     
53     public long parseLong(String JavaDoc arg0) {
54         return Long.parseLong(arg0);
55     }
56     
57     public short parseShort(String JavaDoc arg0) {
58         return Short.parseShort(arg0);
59     }
60     
61     public BigDecimal JavaDoc parseDecimal(String JavaDoc arg0) {
62         return new BigDecimal JavaDoc(arg0);
63     }
64     
65     public float parseFloat(String JavaDoc arg0) {
66         return Float.parseFloat(arg0);
67     }
68     
69     public double parseDouble(String JavaDoc arg0) {
70         return Double.parseDouble(arg0);
71     }
72     
73     public boolean parseBoolean(String JavaDoc arg0) {
74         return Boolean.valueOf(arg0).booleanValue();
75     }
76     
77     public byte parseByte(String JavaDoc arg0) {
78         return Byte.parseByte(arg0);
79     }
80     
81     public QName JavaDoc parseQName(String JavaDoc arg0, NamespaceContext JavaDoc arg1) {
82         int offset = arg0.indexOf(':');
83         String JavaDoc uri;
84         String JavaDoc localName;
85         switch (offset) {
86             case -1:
87                 localName = arg0;
88                 uri = arg1.getNamespaceURI("");
89                 if (uri == null) {
90                     // Should not happen, indicates an error in the NamespaceContext implementation
91
throw new IllegalArgumentException JavaDoc("The default prefix is not bound.");
92                 }
93                 break;
94                case 0:
95                    throw new IllegalArgumentException JavaDoc("Default prefix must be indicated by not using a colon: " + arg0);
96                   default:
97                       String JavaDoc prefix = arg0.substring(0, offset);
98                       localName = arg0.substring(offset+1);
99                       uri = arg1.getNamespaceURI(prefix);
100                       if (uri == null) {
101                           throw new IllegalArgumentException JavaDoc("The prefix " + prefix + " is not bound.");
102                       }
103         }
104         return new QName JavaDoc(uri, localName);
105     }
106
107     public Calendar JavaDoc parseDateTime(String JavaDoc arg0) {
108         XsDateTimeFormat format = new XsDateTimeFormat();
109         ParsePosition JavaDoc pos = new ParsePosition JavaDoc(0);
110         Calendar JavaDoc cal = (Calendar JavaDoc) format.parseObject(arg0, pos);
111         if (cal == null) {
112             throw new IllegalArgumentException JavaDoc("Failed to parse dateTime " + arg0 +
113                                                " at:" + arg0.substring(pos.getErrorIndex()));
114         }
115         return cal;
116     }
117
118     public byte[] parseBase64Binary(String JavaDoc arg0) {
119         try {
120             return Base64Binary.decode(arg0);
121         } catch (IOException JavaDoc e) {
122             throw new IllegalArgumentException JavaDoc("Failed to parse " + arg0 + ": " +
123                     e.getMessage());
124         }
125     }
126     
127     public byte[] parseHexBinary(String JavaDoc arg0) {
128         return HexBinary.decode(arg0);
129     }
130     
131     private static final long MAX_UNSIGNED_INT = (((long) Integer.MAX_VALUE) * 2) + 1;
132     public long parseUnsignedInt(String JavaDoc arg0) {
133         long l = Long.parseLong(arg0);
134         if (l < 0) {
135             throw new IllegalArgumentException JavaDoc("Failed to parse UnsignedInt " + arg0 + ": result is negative");
136         }
137         if (l > MAX_UNSIGNED_INT) {
138             throw new IllegalArgumentException JavaDoc("Failed to parse UnsignedInt " + arg0 +
139                     ": result exceeds maximum value " + MAX_UNSIGNED_INT);
140         }
141         return l;
142     }
143     
144     private static final int MAX_UNSIGNED_SHORT = Short.MAX_VALUE * 2 + 1;
145     public int parseUnsignedShort(String JavaDoc arg0) {
146         int i = Integer.parseInt(arg0);
147         if (i < 0) {
148             throw new IllegalArgumentException JavaDoc("Failed to parse UnsignedShort " + arg0 + ": result is negative");
149         }
150         if (i > MAX_UNSIGNED_SHORT) {
151             throw new IllegalArgumentException JavaDoc("Failed to parse UnsignedShort " + arg0 +
152                     ": result exceeds maximum value " + MAX_UNSIGNED_SHORT);
153         }
154         return i;
155     }
156
157     public Calendar JavaDoc parseTime(String JavaDoc arg0) {
158         XsTimeFormat format = new XsTimeFormat();
159         ParsePosition JavaDoc pos = new ParsePosition JavaDoc(0);
160         Calendar JavaDoc cal = (Calendar JavaDoc) format.parseObject(arg0, pos);
161         if (cal == null) {
162             throw new IllegalArgumentException JavaDoc("Failed to parse time " + arg0 +
163                                                " at:" + arg0.substring(pos.getErrorIndex()));
164         }
165         return cal;
166     }
167
168     public Calendar JavaDoc parseDate(String JavaDoc arg0) {
169         XsDateFormat format = new XsDateFormat();
170         ParsePosition JavaDoc pos = new ParsePosition JavaDoc(0);
171         Calendar JavaDoc cal = (Calendar JavaDoc) format.parseObject(arg0, pos);
172         if (cal == null) {
173             throw new IllegalArgumentException JavaDoc("Failed to parse date " + arg0 +
174                                                " at:" + arg0.substring(pos.getErrorIndex()));
175         }
176         return cal;
177     }
178
179     public String JavaDoc parseAnySimpleType(String JavaDoc arg0) {
180         return arg0;
181     }
182     
183     public Duration parseDuration(String JavaDoc pDuration) {
184         return Duration.valueOf(pDuration);
185     }
186     
187     public String JavaDoc printString(String JavaDoc arg0) {
188         return arg0;
189     }
190     
191     public String JavaDoc printInteger(BigInteger JavaDoc arg0) {
192         return arg0.toString();
193     }
194     
195     public String JavaDoc printInt(int arg0) {
196         return Integer.toString(arg0);
197     }
198     
199     public String JavaDoc printLong(long arg0) {
200         return Long.toString(arg0);
201     }
202     
203     public String JavaDoc printShort(short arg0) {
204         return Short.toString(arg0);
205     }
206     
207     public String JavaDoc printDecimal(BigDecimal JavaDoc arg0) {
208         return arg0.toString();
209     }
210     
211     public String JavaDoc printFloat(float arg0) {
212         return Float.toString(arg0);
213     }
214     
215     public String JavaDoc printDouble(double arg0) {
216         return Double.toString(arg0);
217     }
218
219     public String JavaDoc printBoolean(boolean arg0) {
220         return (arg0 ? Boolean.TRUE : Boolean.FALSE).toString();
221     }
222
223     public String JavaDoc printByte(byte arg0) {
224         return Byte.toString(arg0);
225     }
226
227     public String JavaDoc printQName(QName JavaDoc arg0, NamespaceContext JavaDoc arg1) {
228         String JavaDoc prefix = arg1.getPrefix(arg0.getNamespaceURI());
229         if (prefix == null) {
230             throw new IllegalArgumentException JavaDoc("The namespace URI " +
231                                                arg0.getNamespaceURI() +
232                                                " is not bound.");
233         } else if (XMLConstants.DEFAULT_NS_PREFIX.equals(prefix)) {
234             return arg0.getLocalPart();
235         } else {
236             return prefix + ":" + arg0.getLocalPart();
237         }
238     }
239
240     public String JavaDoc printDateTime(Calendar JavaDoc arg0) {
241         return new XsDateTimeFormat().format(arg0);
242     }
243
244     public String JavaDoc printBase64Binary(byte[] arg0) {
245         return Base64Binary.encode(arg0);
246     }
247
248     public String JavaDoc printHexBinary(byte[] arg0) {
249         return HexBinary.encode(arg0);
250     }
251
252     public String JavaDoc printUnsignedInt(long arg0) {
253         return Long.toString(arg0);
254     }
255
256     public String JavaDoc printUnsignedShort(int arg0) {
257         return Integer.toString(arg0);
258     }
259
260     public String JavaDoc printTime(Calendar JavaDoc arg0) {
261         return new XsTimeFormat().format(arg0);
262     }
263
264     public String JavaDoc printDate(Calendar JavaDoc arg0) {
265         return new XsDateFormat().format(arg0);
266     }
267
268     public String JavaDoc printAnySimpleType(String JavaDoc arg0) {
269         return arg0;
270     }
271
272     public String JavaDoc printDuration(Duration pDuration) {
273         return pDuration.toString();
274     }
275 }
276
277
278
Popular Tags