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