KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > util > JRValueStringUtils


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library 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. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.engine.util;
29
30 import java.io.ByteArrayInputStream JavaDoc;
31 import java.io.ByteArrayOutputStream JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.NotSerializableException JavaDoc;
34 import java.io.ObjectInputStream JavaDoc;
35 import java.io.ObjectOutputStream JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Map JavaDoc;
38
39 import org.w3c.tools.codec.Base64Decoder;
40 import org.w3c.tools.codec.Base64Encoder;
41 import org.w3c.tools.codec.Base64FormatException;
42
43 import net.sf.jasperreports.engine.JRRuntimeException;
44
45
46 /**
47  * Utility class used to serialize/deserialize value objects to/from String values.
48  * <p>
49  * Specific logic is used to convert to and from Strings values of the following types:
50  * <ul>
51  * <li><code>java.lang.String</code></li>
52  * <li><code>java.lang.Character</code></li>
53  * <li><code>java.lang.Boolean</code></li>
54  * <li><code>java.lang.Byte</code></li>
55  * <li><code>java.lang.Short</code></li>
56  * <li><code>java.lang.Integer</code></li>
57  * <li><code>java.lang.Long</code></li>
58  * <li><code>java.lang.Float</code></li>
59  * <li><code>java.lang.Double</code></li>
60  * <li><code>java.math.BigInteger</code></li>
61  * <li><code>java.math.BigDecimal</code></li>
62  * <li><code>java.util.Date</code></li>
63  * <li><code>java.sql.Timestamp</code></li>
64  * <li><code>java.sql.Time</code></li>
65  * </ul>
66  * </p>
67  * <p>
68  * Object of other types are serialized and the resulting binary data is converted into a String
69  * using the BASE64 encoding.
70  * </p>
71  *
72  * @author Lucian Chirita (lucianc@users.sourceforge.net)
73  * @version $Id: JRValueStringUtils.java 1355 2006-08-04 17:31:54 +0300 (Fri, 04 Aug 2006) lucianc $
74  */

75 public class JRValueStringUtils
76 {
77     
78     protected static interface ValueSerializer
79     {
80         String JavaDoc serialize(Object JavaDoc value);
81         
82         Object JavaDoc deserialize(String JavaDoc data);
83     }
84     
85     private static final Map JavaDoc serializers;
86     private static final ValueSerializer defaultSerializer;
87     
88     static
89     {
90         serializers = getSerializers();
91         defaultSerializer = new DefaultSerializer();
92     }
93     
94     
95     /**
96      * Converts a value into a String representation.
97      *
98      * @param valueClass the type of the value
99      * @param value the value
100      * @return the String representation of the value
101      */

102     public static String JavaDoc serialize(String JavaDoc valueClass, Object JavaDoc value)
103     {
104         String JavaDoc data;
105         if (value == null)
106         {
107             data = null;
108         }
109         else
110         {
111             ValueSerializer serializer = getSerializer(valueClass);
112             data = serializer.serialize(value);
113         }
114         return data;
115     }
116
117     
118     /**
119      * Converts a String back into a value.
120      *
121      * @param valueClass the type of the value
122      * @param data the String representation of the value
123      * @return the value
124      */

125     public static Object JavaDoc deserialize(String JavaDoc valueClass, String JavaDoc data)
126     {
127         Object JavaDoc value;
128         if (data == null)
129         {
130             value = null;
131         }
132         else
133         {
134             ValueSerializer serializer = getSerializer(valueClass);
135             value = serializer.deserialize(data);
136         }
137         return value;
138     }
139
140     protected static ValueSerializer getSerializer(String JavaDoc valueClass)
141     {
142         ValueSerializer serializer = (ValueSerializer) serializers.get(valueClass);
143         if (serializer == null)
144         {
145             serializer = defaultSerializer;
146         }
147         return serializer;
148     }
149
150     
151     private static Map JavaDoc getSerializers()
152     {
153         Map JavaDoc map = new HashMap JavaDoc();
154         map.put(java.lang.String JavaDoc.class.getName(), new StringSerializer());
155         map.put(java.lang.Character JavaDoc.class.getName(), new CharacterSerializer());
156         map.put(java.lang.Boolean JavaDoc.class.getName(), new BooleanSerializer());
157         map.put(java.lang.Byte JavaDoc.class.getName(), new ByteSerializer());
158         map.put(java.lang.Short JavaDoc.class.getName(), new ShortSerializer());
159         map.put(java.lang.Integer JavaDoc.class.getName(), new IntegerSerializer());
160         map.put(java.lang.Long JavaDoc.class.getName(), new LongSerializer());
161         map.put(java.lang.Float JavaDoc.class.getName(), new FloatSerializer());
162         map.put(java.lang.Double JavaDoc.class.getName(), new DoubleSerializer());
163         map.put(java.math.BigInteger JavaDoc.class.getName(), new BigIntegerSerializer());
164         map.put(java.math.BigDecimal JavaDoc.class.getName(), new BigDecimalSerializer());
165         map.put(java.util.Date JavaDoc.class.getName(), new DateSerializer());
166         map.put(java.sql.Timestamp JavaDoc.class.getName(), new TimestampSerializer());
167         map.put(java.sql.Time JavaDoc.class.getName(), new TimeSerializer());
168         return map;
169     }
170     
171     
172     protected static class StringSerializer implements ValueSerializer
173     {
174         public Object JavaDoc deserialize(String JavaDoc data)
175         {
176             return data;
177         }
178
179         public String JavaDoc serialize(Object JavaDoc value)
180         {
181             return (String JavaDoc) value;
182         }
183     }
184     
185     
186     protected static class CharacterSerializer implements ValueSerializer
187     {
188         public Object JavaDoc deserialize(String JavaDoc data)
189         {
190             if (data.length() != 1)
191             {
192                 throw new JRRuntimeException("Character data \"" + data + "\" should be exactly one character long");
193             }
194             return new Character JavaDoc(data.charAt(0));
195         }
196
197         public String JavaDoc serialize(Object JavaDoc value)
198         {
199             return String.valueOf(new char[]{((Character JavaDoc) value).charValue()});
200         }
201     }
202     
203     
204     protected static class BooleanSerializer implements ValueSerializer
205     {
206         public Object JavaDoc deserialize(String JavaDoc data)
207         {
208             if (data.equals("true"))
209             {
210                 return Boolean.TRUE;
211             }
212             if (data.equals("false"))
213             {
214                 return Boolean.FALSE;
215             }
216             throw new JRRuntimeException("Unkown boolean data \"" + data + "\"");
217         }
218
219         public String JavaDoc serialize(Object JavaDoc value)
220         {
221             return ((Boolean JavaDoc) value).booleanValue() ? "true" : "false";
222         }
223     }
224     
225     
226     protected static class ByteSerializer implements ValueSerializer
227     {
228         public Object JavaDoc deserialize(String JavaDoc data)
229         {
230             try
231             {
232                 return Byte.valueOf(data);
233             }
234             catch (NumberFormatException JavaDoc e)
235             {
236                 throw new JRRuntimeException("Error parsing Byte data \"" + data + "\"", e);
237             }
238         }
239
240         public String JavaDoc serialize(Object JavaDoc value)
241         {
242             return ((Byte JavaDoc) value).toString();
243         }
244     }
245     
246     
247     protected static class ShortSerializer implements ValueSerializer
248     {
249         public Object JavaDoc deserialize(String JavaDoc data)
250         {
251             try
252             {
253                 return Short.valueOf(data);
254             }
255             catch (NumberFormatException JavaDoc e)
256             {
257                 throw new JRRuntimeException("Error parsing Short data \"" + data + "\"", e);
258             }
259         }
260
261         public String JavaDoc serialize(Object JavaDoc value)
262         {
263             return ((Short JavaDoc) value).toString();
264         }
265     }
266     
267     
268     protected static class IntegerSerializer implements ValueSerializer
269     {
270         public Object JavaDoc deserialize(String JavaDoc data)
271         {
272             try
273             {
274                 return Integer.valueOf(data);
275             }
276             catch (NumberFormatException JavaDoc e)
277             {
278                 throw new JRRuntimeException("Error parsing Integer data \"" + data + "\"", e);
279             }
280         }
281
282         public String JavaDoc serialize(Object JavaDoc value)
283         {
284             return ((Integer JavaDoc) value).toString();
285         }
286     }
287     
288     
289     protected static class LongSerializer implements ValueSerializer
290     {
291         public Object JavaDoc deserialize(String JavaDoc data)
292         {
293             try
294             {
295                 return Long.valueOf(data);
296             }
297             catch (NumberFormatException JavaDoc e)
298             {
299                 throw new JRRuntimeException("Error parsing Long data \"" + data + "\"", e);
300             }
301         }
302
303         public String JavaDoc serialize(Object JavaDoc value)
304         {
305             return ((Long JavaDoc) value).toString();
306         }
307     }
308     
309     
310     protected static class FloatSerializer implements ValueSerializer
311     {
312         public Object JavaDoc deserialize(String JavaDoc data)
313         {
314             try
315             {
316                 return Float.valueOf(data);
317             }
318             catch (NumberFormatException JavaDoc e)
319             {
320                 throw new JRRuntimeException("Error parsing Float data \"" + data + "\"", e);
321             }
322         }
323
324         public String JavaDoc serialize(Object JavaDoc value)
325         {
326             return ((Float JavaDoc) value).toString();
327         }
328     }
329     
330     
331     protected static class DoubleSerializer implements ValueSerializer
332     {
333         public Object JavaDoc deserialize(String JavaDoc data)
334         {
335             try
336             {
337                 return Double.valueOf(data);
338             }
339             catch (NumberFormatException JavaDoc e)
340             {
341                 throw new JRRuntimeException("Error parsing Double data \"" + data + "\"", e);
342             }
343         }
344
345         public String JavaDoc serialize(Object JavaDoc value)
346         {
347             return ((Double JavaDoc) value).toString();
348         }
349     }
350     
351     
352     protected static class BigIntegerSerializer implements ValueSerializer
353     {
354         public Object JavaDoc deserialize(String JavaDoc data)
355         {
356             try
357             {
358                 return new java.math.BigInteger JavaDoc(data);
359             }
360             catch (NumberFormatException JavaDoc e)
361             {
362                 throw new JRRuntimeException("Error parsing BigInteger data \"" + data + "\"", e);
363             }
364         }
365
366         public String JavaDoc serialize(Object JavaDoc value)
367         {
368             return ((java.math.BigInteger JavaDoc) value).toString();
369         }
370     }
371     
372     
373     protected static class BigDecimalSerializer implements ValueSerializer
374     {
375         public Object JavaDoc deserialize(String JavaDoc data)
376         {
377             try
378             {
379                 return new java.math.BigDecimal JavaDoc(data);
380             }
381             catch (NumberFormatException JavaDoc e)
382             {
383                 throw new JRRuntimeException("Error parsing BigDecimal data \"" + data + "\"", e);
384             }
385         }
386
387         public String JavaDoc serialize(Object JavaDoc value)
388         {
389             return ((java.math.BigDecimal JavaDoc) value).toString();
390         }
391     }
392     
393     
394     protected static class DateSerializer implements ValueSerializer
395     {
396         public Object JavaDoc deserialize(String JavaDoc data)
397         {
398             try
399             {
400                 long time = Long.parseLong(data);
401                 return new java.util.Date JavaDoc(time);
402             }
403             catch (NumberFormatException JavaDoc e)
404             {
405                 throw new JRRuntimeException("Error parsing Date data \"" + data + "\"", e);
406             }
407         }
408
409         public String JavaDoc serialize(Object JavaDoc value)
410         {
411             return Long.toString(((java.util.Date JavaDoc) value).getTime());
412         }
413     }
414     
415     
416     protected static class TimestampSerializer implements ValueSerializer
417     {
418         public Object JavaDoc deserialize(String JavaDoc data)
419         {
420             try
421             {
422                 return java.sql.Timestamp.valueOf(data);
423             }
424             catch (IllegalArgumentException JavaDoc e)
425             {
426                 throw new JRRuntimeException("Error parsing Timestamp data \"" + data + "\"", e);
427             }
428         }
429
430         public String JavaDoc serialize(Object JavaDoc value)
431         {
432             java.sql.Timestamp JavaDoc timestamp = (java.sql.Timestamp JavaDoc) value;
433             return timestamp.toString();
434         }
435     }
436     
437     
438     protected static class TimeSerializer implements ValueSerializer
439     {
440         public Object JavaDoc deserialize(String JavaDoc data)
441         {
442             try
443             {
444                 return java.sql.Time.valueOf(data);
445             }
446             catch (IllegalArgumentException JavaDoc e)
447             {
448                 throw new JRRuntimeException("Error parsing Time data \"" + data + "\"", e);
449             }
450         }
451
452         public String JavaDoc serialize(Object JavaDoc value)
453         {
454             java.sql.Time JavaDoc timestamp = (java.sql.Time JavaDoc) value;
455             return timestamp.toString();
456         }
457     }
458     
459     
460     protected static class DefaultSerializer implements ValueSerializer
461     {
462         public Object JavaDoc deserialize(String JavaDoc data)
463         {
464             try
465             {
466                 ByteArrayInputStream JavaDoc dataIn = new ByteArrayInputStream JavaDoc(data.getBytes());
467                 ByteArrayOutputStream JavaDoc bytesOut = new ByteArrayOutputStream JavaDoc();
468                 Base64Decoder dec = new Base64Decoder(dataIn, bytesOut);
469                 dec.process();
470                 
471                 ByteArrayInputStream JavaDoc bytesIn = new ByteArrayInputStream JavaDoc(bytesOut.toByteArray());
472                 ObjectInputStream JavaDoc objectIn = new ObjectInputStream JavaDoc(bytesIn);
473                 return objectIn.readObject();
474             }
475             catch (IOException JavaDoc e)
476             {
477                 throw new JRRuntimeException(e);
478             }
479             catch (ClassNotFoundException JavaDoc e)
480             {
481                 throw new JRRuntimeException(e);
482             }
483             catch (Base64FormatException e)
484             {
485                 throw new JRRuntimeException(e);
486             }
487         }
488
489         public String JavaDoc serialize(Object JavaDoc value)
490         {
491             try
492             {
493                 ByteArrayOutputStream JavaDoc bytesOut = new ByteArrayOutputStream JavaDoc();
494                 ObjectOutputStream JavaDoc objectOut = new ObjectOutputStream JavaDoc(bytesOut);
495                 objectOut.writeObject(value);
496                 objectOut.close();
497                 
498                 ByteArrayInputStream JavaDoc bytesIn = new ByteArrayInputStream JavaDoc(bytesOut.toByteArray());
499                 ByteArrayOutputStream JavaDoc dataOut = new ByteArrayOutputStream JavaDoc();
500                 
501                 Base64Encoder enc = new Base64Encoder(bytesIn, dataOut);
502                 enc.process();
503                 
504                 return new String JavaDoc(dataOut.toByteArray(), "UTF-8");
505             }
506             catch (NotSerializableException JavaDoc e)
507             {
508                 throw new JRRuntimeException("Value is not serializable", e);
509             }
510             catch (IOException JavaDoc e)
511             {
512                 throw new JRRuntimeException(e);
513             }
514         }
515     }
516 }
517
Popular Tags