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