View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */ 
17  package org.apache.commons.betwixt.strategy;
18  
19  import java.lang.reflect.Array;
20  
21  import org.apache.commons.beanutils.ConvertUtils;
22  import org.apache.commons.beanutils.Converter;
23  import org.apache.commons.betwixt.expression.Context;
24  
25  /** 
26   * String <-> object conversion strategy that delegates to ConvertUtils.
27   *
28   * @author Robert Burrell Donkin
29   * @since 0.5
30   */
31  public class ConvertUtilsObjectStringConverter extends ObjectStringConverter {
32      
33      /**
34        * Converts an object to a string representation using ConvertUtils.
35        *
36        * @param object the object to be converted, possibly null
37        * @param type the property class of the object, not null
38        * @param flavour a string allow symantic differences in formatting 
39        * to be communicated (ignored)
40        * @param context not null
41        * @return a String representation, not null
42        */
43      public String objectToString(Object object, Class type, String flavour, Context context) {
44          if ( object != null ) {
45              String text = ConvertUtils.convert( object );
46              if ( text != null ) {
47                  return text;
48              }
49          }
50          return "";
51      }
52      
53      /**
54        * Converts an object to a string representation using ConvertUtils.
55        * This implementation ignores null and empty string values (rather than converting them).
56        * 
57        * @param value the String to be converted, not null
58        * @param type the property class to be returned (if possible), not null
59        * @param flavour a string allow symantic differences in formatting 
60        * to be communicated (ignored)
61        * @param context not null
62        * @return an Object converted from the String, not null
63        */
64      public Object stringToObject(String value, Class type, String flavour, Context context) {
65          if (value == null || "".equals(value))
66          {
67              return "";    
68          }
69          
70          return ConvertUtils.convert( value, type );
71      }
72  }