View Javadoc

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 org.apache.commons.beanutils.ConvertUtils;
19  import org.apache.commons.betwixt.expression.Context;
20  
21  /*** 
22   * String <-> object conversion strategy that delegates to ConvertUtils.
23   *
24   * @author Robert Burrell Donkin
25   * @since 0.5
26   */
27  public class ConvertUtilsObjectStringConverter extends ObjectStringConverter {
28      
29      /***
30        * Converts an object to a string representation using ConvertUtils.
31        *
32        * @param object the object to be converted, possibly null
33        * @param type the property class of the object, not null
34        * @param flavour a string allow symantic differences in formatting 
35        * to be communicated (ignored)
36        * @param context not null
37        * @return a String representation, not null
38        */
39      public String objectToString(Object object, Class type, String flavour, Context context) {
40          if ( object != null ) {
41              String text = ConvertUtils.convert( object );
42              if ( text != null ) {
43                  return text;
44              }
45          }
46          return "";
47      }
48      
49      /***
50        * Converts an object to a string representation using ConvertUtils.
51        * This implementation ignores null and empty string values (rather than converting them).
52        * 
53        * @param value the String to be converted, not null
54        * @param type the property class to be returned (if possible), not null
55        * @param flavour a string allow symantic differences in formatting 
56        * to be communicated (ignored)
57        * @param context not null
58        * @return an Object converted from the String, not null
59        */
60      public Object stringToObject(String value, Class type, String flavour, Context context) {
61          if (value == null || "".equals(value))
62          {
63              return null;    
64          }
65          
66          return ConvertUtils.convert( value, type );
67      }
68  }