KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jaxb > DatatypeConverterImpl


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Emil Ong
28  */

29
30 package com.caucho.jaxb;
31
32 import com.caucho.util.Base64;
33 import com.caucho.util.L10N;
34
35 import javax.xml.bind.DatatypeConverterInterface;
36 import javax.xml.namespace.NamespaceContext JavaDoc;
37 import javax.xml.namespace.QName JavaDoc;
38 import java.math.BigDecimal JavaDoc;
39 import java.math.BigInteger JavaDoc;
40 import java.text.ParseException JavaDoc;
41 import java.text.SimpleDateFormat JavaDoc;
42 import java.util.Calendar JavaDoc;
43 import java.util.Date JavaDoc;
44
45 /**
46  */

47 public class DatatypeConverterImpl implements DatatypeConverterInterface {
48   private static final L10N L = new L10N(DatatypeConverterImpl.class);
49
50   // XXX: Technically, iso8601 allows removing : and -
51
private final SimpleDateFormat JavaDoc dateFormat
52     = new SimpleDateFormat JavaDoc("yyyy-MM-dd");
53   private final SimpleDateFormat JavaDoc timeFormat
54     = new SimpleDateFormat JavaDoc("HH:mm:ss");
55   private final SimpleDateFormat JavaDoc dateTimeFormat
56     = new SimpleDateFormat JavaDoc("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
57
58   private final char[] hexDigits = { '0', '1', '2', '3', '4',
59                                      '5', '6', '7', '8', '9',
60                                      'A', 'B', 'C', 'D', 'E', 'F'};
61
62   //
63
// Parsers
64
//
65

66   public String JavaDoc parseAnySimpleType(String JavaDoc lexicalXSDAnySimpleType)
67   {
68     return lexicalXSDAnySimpleType;
69   }
70
71   public byte[] parseBase64Binary(String JavaDoc lexicalXSDBase64Binary)
72   {
73     return Base64.decodeToByteArray(lexicalXSDBase64Binary);
74   }
75
76   public boolean parseBoolean(String JavaDoc lexicalXSDBoolean)
77   {
78     return Boolean.parseBoolean(lexicalXSDBoolean);
79   }
80
81   public byte parseByte(String JavaDoc lexicalXSDByte)
82   {
83     return Byte.parseByte(lexicalXSDByte);
84   }
85
86   public Calendar JavaDoc parseDate(String JavaDoc lexicalXSDDate)
87     throws IllegalArgumentException JavaDoc
88   {
89     try {
90       Date JavaDoc date = dateFormat.parse(lexicalXSDDate);
91       Calendar JavaDoc calendar = (Calendar JavaDoc) dateFormat.getCalendar().clone();
92       calendar.setTime(date);
93
94       return calendar;
95     }
96     catch (ParseException JavaDoc e) {
97       throw new IllegalArgumentException JavaDoc(e);
98     }
99   }
100
101   public Calendar JavaDoc parseDateTime(String JavaDoc lexicalXSDDateTime)
102     throws IllegalArgumentException JavaDoc
103   {
104     try {
105       Date JavaDoc date = dateTimeFormat.parse(lexicalXSDDateTime);
106       Calendar JavaDoc calendar = (Calendar JavaDoc) dateFormat.getCalendar().clone();
107       calendar.setTime(date);
108
109       return calendar;
110     }
111     catch (ParseException JavaDoc e) {
112       throw new IllegalArgumentException JavaDoc(e);
113     }
114   }
115
116   public BigDecimal JavaDoc parseDecimal(String JavaDoc lexicalXSDDecimal)
117   {
118     return new BigDecimal JavaDoc(lexicalXSDDecimal);
119   }
120
121   public double parseDouble(String JavaDoc lexicalXSDDouble)
122   {
123     return Double.parseDouble(lexicalXSDDouble);
124   }
125
126   public float parseFloat(String JavaDoc lexicalXSDFloat)
127   {
128     return Float.parseFloat(lexicalXSDFloat);
129   }
130
131   public byte[] parseHexBinary(String JavaDoc lexicalXSDHexBinary)
132     throws IllegalArgumentException JavaDoc
133   {
134     if (lexicalXSDHexBinary.length() % 2 != 0)
135       throw new IllegalArgumentException JavaDoc();
136
137     byte[] buffer = new byte[lexicalXSDHexBinary.length() / 2];
138
139     for (int i = 0; i < buffer.length; i++) {
140       buffer[i] =
141         Byte.parseByte(lexicalXSDHexBinary.substring(2 * i, 2 * i + 2), 16);
142     }
143
144     return buffer;
145   }
146
147   public int parseInt(String JavaDoc lexicalXSDInt)
148   {
149     return Integer.parseInt(lexicalXSDInt);
150   }
151
152   public BigInteger JavaDoc parseInteger(String JavaDoc lexicalXSDInteger)
153   {
154     return new BigInteger JavaDoc(lexicalXSDInteger);
155   }
156
157   public long parseLong(String JavaDoc lexicalXSDLong)
158   {
159     return Long.parseLong(lexicalXSDLong);
160   }
161
162   public QName JavaDoc parseQName(String JavaDoc lexicalXSDQName, NamespaceContext JavaDoc nsc)
163     throws IllegalArgumentException JavaDoc
164   {
165     String JavaDoc[] parts = lexicalXSDQName.split(":", 2);
166
167     if (parts.length == 1)
168       return new QName JavaDoc(lexicalXSDQName);
169     else {
170       String JavaDoc namespaceURI = nsc.getNamespaceURI(parts[0]);
171
172       if (namespaceURI == null)
173         throw new IllegalArgumentException JavaDoc(L.l("Unknown prefix {0}", parts[0]));
174
175       return new QName JavaDoc(namespaceURI, parts[1], parts[0]);
176     }
177   }
178
179   public short parseShort(String JavaDoc lexicalXSDShort)
180   {
181     return Short.parseShort(lexicalXSDShort);
182   }
183
184   public String JavaDoc parseString(String JavaDoc lexicalXSDString)
185   {
186     return lexicalXSDString;
187   }
188
189   public Calendar JavaDoc parseTime(String JavaDoc lexicalXSDTime)
190     throws IllegalArgumentException JavaDoc
191   {
192     try {
193       Date JavaDoc date = timeFormat.parse(lexicalXSDTime);
194       Calendar JavaDoc calendar = (Calendar JavaDoc) dateFormat.getCalendar().clone();
195       calendar.setTime(date);
196
197       return calendar;
198     }
199     catch (ParseException JavaDoc e) {
200       throw new IllegalArgumentException JavaDoc(e);
201     }
202   }
203
204   public long parseUnsignedInt(String JavaDoc lexicalXSDUnsignedInt)
205   {
206     long x = parseLong(lexicalXSDUnsignedInt);
207
208     if (x < 0)
209       throw new IllegalArgumentException JavaDoc(L.l("Input not unsigned"));
210
211     return x;
212   }
213
214   public int parseUnsignedShort(String JavaDoc lexicalXSDUnsignedShort)
215   {
216     int x = parseInt(lexicalXSDUnsignedShort);
217
218     if (x < 0)
219       throw new IllegalArgumentException JavaDoc(L.l("Input not unsigned"));
220
221     return x;
222   }
223
224   //
225
// Printers
226
//
227

228   public String JavaDoc printAnySimpleType(String JavaDoc val)
229   {
230     return val;
231   }
232
233   public String JavaDoc printBase64Binary(byte[] val)
234   {
235     return Base64.encodeFromByteArray(val);
236   }
237
238   public String JavaDoc printBoolean(boolean val)
239   {
240     return String.valueOf(val);
241   }
242
243   public String JavaDoc printByte(byte val)
244   {
245     return String.valueOf((int) val);
246   }
247
248   public String JavaDoc printDate(Calendar JavaDoc val)
249   {
250     dateFormat.setCalendar(val);
251     return dateFormat.format(val.getTime());
252   }
253
254   public String JavaDoc printDateTime(Calendar JavaDoc val)
255   {
256     dateTimeFormat.setCalendar(val);
257     return dateTimeFormat.format(val.getTime());
258   }
259
260   public String JavaDoc printDecimal(BigDecimal JavaDoc val)
261   {
262     return val.toString();
263   }
264
265   public String JavaDoc printDouble(double val)
266   {
267     return String.valueOf(val);
268   }
269
270   public String JavaDoc printFloat(float val)
271   {
272     return String.valueOf(val);
273   }
274
275   public String JavaDoc printHexBinary(byte[] val)
276   {
277     StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
278
279     for (int i = 0; i < val.length; i++) {
280       sb.append(hexDigits[val[i] >>> 4]);
281       sb.append(hexDigits[val[i] & 0x0f]);
282     }
283
284     return sb.toString();
285   }
286
287   public String JavaDoc printInt(int val)
288   {
289     return String.valueOf(val);
290   }
291
292   public String JavaDoc printInteger(BigInteger JavaDoc val)
293   {
294     return val.toString();
295   }
296
297   public String JavaDoc printLong(long val)
298   {
299     return String.valueOf(val);
300   }
301
302   public String JavaDoc printQName(QName JavaDoc val, NamespaceContext JavaDoc nsc)
303   {
304     // XXX use context?
305
return val.toString();
306   }
307
308   public String JavaDoc printShort(short val)
309   {
310     return String.valueOf(val);
311   }
312
313   public String JavaDoc printString(String JavaDoc val)
314   {
315     return val;
316   }
317
318   public String JavaDoc printTime(Calendar JavaDoc val)
319   {
320     timeFormat.setCalendar(val);
321     return timeFormat.format(val.getTime());
322   }
323
324   public String JavaDoc printUnsignedInt(long val)
325   {
326     return String.valueOf(val);
327   }
328
329   public String JavaDoc printUnsignedShort(int val)
330   {
331     return String.valueOf(val);
332   }
333 }
334
Popular Tags