View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
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 de.smartics.properties.spi.core.convert;
17  
18  import java.net.URL;
19  import java.util.Arrays;
20  import java.util.List;
21  
22  import org.apache.commons.beanutils.ConvertUtils;
23  import org.apache.commons.beanutils.Converter;
24  import org.apache.commons.beanutils.converters.ArrayConverter;
25  
26  import de.smartics.properties.api.core.domain.PropertyDescriptor;
27  import de.smartics.properties.api.core.domain.PropertyValueConversionException;
28  
29  /**
30   * Converter of property values to and from Strings using Apache's
31   * commons-beanutils.
32   */
33  public final class BeanUtilsPropertyValueConverter implements
34      PropertyValueConverter
35  {
36    // ********************************* Fields *********************************
37  
38    // --- constants ------------------------------------------------------------
39  
40    /**
41     * The class version identifier.
42     */
43    private static final long serialVersionUID = 1L;
44  
45    // --- members --------------------------------------------------------------
46  
47    // ****************************** Initializer *******************************
48  
49    // ****************************** Constructors ******************************
50  
51    // ****************************** Inner Classes *****************************
52  
53    // ********************************* Methods ********************************
54  
55    // --- init -----------------------------------------------------------------
56  
57    // --- get&set --------------------------------------------------------------
58  
59    // --- business -------------------------------------------------------------
60  
61    @Override
62    public Object convert(final PropertyDescriptor descriptor, final Object value)
63      throws PropertyValueConversionException
64    {
65      try
66      {
67        final Class<?> type = descriptor.getType().getType();
68  
69        final Object convertedValue;
70        if (List.class.isAssignableFrom(type))
71        {
72          final ListPropertyHelper helper = new ListPropertyHelper(descriptor);
73          final Class<?> elementType = helper.getElementType();
74          ConverterUtils.ensureConverter(descriptor, elementType);
75          final Converter elementConverter = ConvertUtils.lookup(elementType);
76          final Class<?> arrayType = helper.getArrayTypeOfElementType();
77          final ArrayConverter converter =
78              new ArrayConverter(arrayType, elementConverter);
79          configure(converter, elementType);
80          final Object[] array = (Object[]) converter.convert(arrayType, value);
81          convertedValue = Arrays.asList(array);
82        }
83        else
84        {
85          ConverterUtils.ensureConverter(descriptor, type);
86          convertedValue = ConvertUtils.convert(value, type);
87        }
88  
89        return convertedValue;
90      }
91      catch (final RuntimeException e)
92      {
93        throw new PropertyValueConversionException(e, descriptor, value);
94      }
95    }
96  
97    private void configure(final ArrayConverter converter,
98        final Class<?> elementType)
99    {
100     if (URL.class == elementType)
101     {
102       converter.setAllowedChars(new char[] { '.', ':', '-', '_', '#', '+', '*',
103                                             '~', '?', '/', '!', '"', '$', '%',
104                                             '&', '(', ')', '[', ']', '{', '}',
105                                             '=', ',', ';', '@' });
106       converter.setDelimiter('|');
107     }
108 
109   }
110 
111   // --- object basics --------------------------------------------------------
112 
113 }