KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > common > util > CommonConverter


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
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 package com.blandware.atleap.common.util;
17
18 import org.apache.commons.beanutils.ConversionException;
19 import org.apache.commons.beanutils.Converter;
20 import org.apache.commons.lang.StringUtils;
21
22 import java.text.DateFormat JavaDoc;
23 import java.text.SimpleDateFormat JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.Locale JavaDoc;
26
27
28 /**
29  * <p>This class converts a java.util.Date to a String
30  * and a String to a java.util.Date and Object to Object.
31  * It is used by Struts BeanUtils when copying properties. Registered
32  * for use in StartupListener.
33  * </p>
34  * <p><a HREF="CommonConverter.java.htm"><i>View Source</i></a>
35  * </p>
36  *
37  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
38  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
39  * @author Matt Raible <a HREF="mailto:matt@raibledesigns.com">&lt;matt@raibledesigns.com&gt;</a>
40  * @version $Revision: 1.10 $ $Date: 2005/08/02 14:53:32 $
41  * @see org.apache.commons.beanutils.BeanUtils
42  */

43 public class CommonConverter implements Converter {
44
45     protected Locale JavaDoc locale;
46     protected DateFormat JavaDoc formatter;
47
48     /**
49      * Creates new instance of CommonConverter with default locale
50      */

51     public CommonConverter() {
52         this(Locale.getDefault());
53     }
54
55     /**
56      * Creates new instance of Common converter with specified locale
57      *
58      * @param locale Locale to use
59      */

60     public CommonConverter(Locale JavaDoc locale) {
61         this.locale = locale;
62         this.formatter = new SimpleDateFormat JavaDoc(DateUtil.getDatePattern(locale), locale);
63     }
64
65     /**
66      * Converts an object to specified class (<code>Date</code>, <code>String</code>
67      * or <code>Object</code>). In all other cases a <code>ConversionException</code>
68      * will be thrown. The only exception is <code>null</code> value: it will
69      * give <code>null</code> regardless of <code>type</code>.
70      *
71      * @param type the class to which the object need to be converted (this can be
72      * <code>Date</code>, <code>String</code> or <code>Object</code>)
73      * @param value the object that need to be converted
74      * @return converted object
75      */

76     public Object JavaDoc convert(Class JavaDoc type, Object JavaDoc value) {
77         if ( value == null ) {
78             return null;
79         } else if ( type == Date JavaDoc.class ) {
80             return convertToDate(type, value);
81         } else if ( type == String JavaDoc.class ) {
82             return convertToString(type, value);
83         } else if ( type == Object JavaDoc.class ) {
84             return convertToObject(type, value);
85         }
86
87         throw new ConversionException("Could not convert " +
88                 value.getClass().getName() + " to " +
89                 type.getName());
90     }
91
92     /**
93      * Converts specified object to <code>Date</code>
94      *
95      * @param type the class to which the object need to be converted
96      * @param value the object that need to be converted
97      * @return converted object
98      */

99     protected Object JavaDoc convertToDate(Class JavaDoc type, Object JavaDoc value) {
100         if ( value instanceof Date JavaDoc ) {
101             return value;
102         }
103
104         if ( value instanceof String JavaDoc ) {
105             try {
106                 if ( StringUtils.isEmpty(value.toString()) ) {
107                     return null;
108                 }
109
110                 return formatter.parse((String JavaDoc) value);
111             } catch ( Exception JavaDoc pe ) {
112                 throw new ConversionException("Error converting String to Date");
113             }
114         }
115
116         throw new ConversionException("Could not convert " +
117                 value.getClass().getName() + " to " +
118                 type.getName());
119     }
120
121     /**
122      * Converts specified object to <code>String</code>
123      *
124      * @param type the class to which the object need to be converted
125      * @param value the object that need to be converted
126      * @return converted object
127      */

128     protected Object JavaDoc convertToString(Class JavaDoc type, Object JavaDoc value) {
129         if ( value instanceof Date JavaDoc ) {
130             try {
131                 return formatter.format(value);
132             } catch ( Exception JavaDoc e ) {
133                 throw new ConversionException("Error converting Date to String");
134             }
135         }
136
137         return value.toString();
138     }
139
140     /**
141      * Converts specified object to <code>Object</code>
142      *
143      * @param type the class to which the object need to be converted
144      * @param value the object that need to be converted
145      * @return converted object
146      */

147     protected Object JavaDoc convertToObject(Class JavaDoc type, Object JavaDoc value) {
148         return value;
149     }
150
151 }
152
Popular Tags