KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > betwixt > strategy > DefaultObjectStringConverter


1 /*
2  * Copyright 2001-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 package org.apache.commons.betwixt.strategy;
17
18 import java.text.ParseException JavaDoc;
19 import java.text.SimpleDateFormat JavaDoc;
20 import java.util.Locale JavaDoc;
21
22 import org.apache.commons.beanutils.ConversionException;
23 import org.apache.commons.betwixt.expression.Context;
24
25 /**
26  * <p>Default string &lt;-&gt; object conversion strategy.</p>
27  * <p>
28  * This delegates to ConvertUtils except when the type
29  * is assignable from <code>java.util.Date</code>
30  * but not from <code>java.sql.Date</code>.
31  * In this case, the format used is (in SimpleDateFormat terms)
32  * <code>EEE MMM dd HH:mm:ss zzz yyyy</code>.
33  * This is the same as the output of the toString method on java.util.Date.
34  * </p>
35  * <p>
36  * This should preserve the existing symantic behaviour whilst allowing round tripping of dates
37  * (given the default settings).
38  * </p>
39  * @author Robert Burrell Donkin
40  * @since 0.5
41  */

42 public class DefaultObjectStringConverter extends ConvertUtilsObjectStringConverter {
43     
44     /** Formats Dates to Strings and Strings to Dates */
45     private static final SimpleDateFormat JavaDoc formatter
46         = new SimpleDateFormat JavaDoc("EEE MMM dd HH:mm:ss zzz yyyy", Locale.UK);
47     
48     /**
49       * Converts an object to a string representation using ConvertUtils.
50       * If the object is a java.util.Date and the type is java.util.Date
51       * but not java.sql.Date
52       * then SimpleDateFormat formatting to
53       * <code>EEE MMM dd HH:mm:ss zzz yyyy</code>
54       * will be used.
55       * (This is the same as java.util.Date toString would return.)
56       *
57       * @param object the object to be converted, possibly null
58       * @param type the property class of the object, not null
59       * @param flavour a string allow symantic differences in formatting
60       * to be communicated (ignored)
61       * @param context convert against this context not null
62       * @return a String representation, not null
63       */

64     public String JavaDoc objectToString(Object JavaDoc object, Class JavaDoc type, String JavaDoc flavour, Context context) {
65         if ( object != null ) {
66             if ( object instanceof java.util.Date JavaDoc && isUtilDate( type ) ) {
67                 
68                 return formatter.format( (java.util.Date JavaDoc) object );
69                 
70             } else {
71                 // use ConvertUtils implementation
72
return super.objectToString( object, type, flavour, context );
73             }
74         }
75         return "";
76     }
77     
78     /**
79       * Converts an object to a string representation using ConvertUtils.
80       *
81       * @param value the String to be converted, not null
82       * @param type the property class to be returned (if possible), not null
83       * @param flavour a string allow symantic differences
84       * in formatting to be communicated (ignored)
85       * @param context not null
86       * @return an Object converted from the String, not null
87       */

88     public Object JavaDoc stringToObject(String JavaDoc value, Class JavaDoc type, String JavaDoc flavour, Context context) {
89             if ( isUtilDate( type ) ) {
90                 try {
91                     
92                     return formatter.parse( value );
93                     
94                 } catch ( ParseException JavaDoc ex ) {
95                     handleException( ex );
96                     // this supports any subclasses that do not which to throw exceptions
97
// probably will result in a problem when the method will be invoked
98
// but never mind
99
return value;
100                 }
101             } else {
102                 // use ConvertUtils implementation
103
return super.stringToObject( value, type, flavour, context );
104             }
105     }
106     
107     /**
108       * Allow subclasses to use a different exception handling strategy.
109       * This class throws a <code>org.apache.commons.beanutils.ConversionException</code>
110       * when conversion fails.
111       * @param e the Exception to be handled
112       * @throws org.apache.commons.beanutils.ConversionException when conversion fails
113       */

114     protected void handleException(Exception JavaDoc e) {
115         throw new ConversionException( "String to object conversion failed: " + e.getMessage(), e );
116     }
117     
118     /**
119       * Is the given type a java.util.Date but not a java.sql.Date?
120       * @param type test this class type
121       * @return true is this is a until date but not a sql one
122       */

123     private boolean isUtilDate(Class JavaDoc type) {
124         return ( java.util.Date JavaDoc.class.isAssignableFrom(type)
125              && !java.sql.Date JavaDoc.class.isAssignableFrom(type)
126              && !java.sql.Time JavaDoc.class.isAssignableFrom(type)
127              && !java.sql.Timestamp JavaDoc.class.isAssignableFrom(type) );
128     }
129 }
130
Popular Tags