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
    18
    19  package org.apache.commons.beanutils.converters;
    20
    21
    22  import java.util.List;
    23  import org.apache.commons.beanutils.ConversionException;
    24
    25
    26  /***
    27   * <p>Standard {@link org.apache.commons.beanutils.Converter} implementation that converts an incoming
    28   * String into a primitive array of char.  On a conversion failure, returns
    29   * a specified default value or throws a {@link ConversionException} depending
    30   * on how this instance is constructed.</p>
    31   *
    32   * @author Craig R. McClanahan
    33   * @version $Revision: 556229 $ $Date: 2007-07-14 07:11:19 +0100 (Sat, 14 Jul 2007) $
    34   * @since 1.4
    35   * @deprecated Replaced by the new {@link ArrayConverter} implementation
    36   */
    37
    38  public final class CharacterArrayConverter extends AbstractArrayConverter {
    39
    40
    41      // ----------------------------------------------------------- Constructors
    42
    43
    44      /***
    45       * Create a {@link org.apache.commons.beanutils.Converter} that will throw
    46       * a {@link ConversionException} if a conversion error occurs.
    47       */
    48      public CharacterArrayConverter() {
    49
    50          this.defaultValue = null;
    51          this.useDefault = false;
    52
    53      }
    54
    55
    56      /***
    57       * Create a {@link org.apache.commons.beanutils.Converter} that will return
    58       * the specified default value if a conversion error occurs.
    59       *
    60       * @param defaultValue The default value to be returned
    61       */
    62      public CharacterArrayConverter(Object defaultValue) {
    63
    64          this.defaultValue = defaultValue;
    65          this.useDefault = true;
    66
    67      }
    68
    69
    70      // ------------------------------------------------------- Static Variables
    71
    72
    73      /***
    74       * <p>Model object for type comparisons.</p>
    75       */
    76      private static final char[] MODEL = new char[0];
    77
    78
    79      // --------------------------------------------------------- Public Methods
    80
    81
    82      /***
    83       * Convert the specified input object into an output object of the
    84       * specified type.
    85       *
    86       * @param type Data type to which this value should be converted
    87       * @param value The input value to be converted
    88       * @return the converted value
    89       *
    90       * @exception ConversionException if conversion cannot be performed
    91       *  successfully
    92       */
    93      public Object convert(Class type, Object value) {
    94
    95          // Deal with a null value
    96          if (value == null) {
    97              if (useDefault) {
    98                  return (defaultValue);
    99              } else {
    100                 throw new ConversionException("No value specified");
    101             }
    102         }
    103
    104         // Deal with the no-conversion-needed case
    105         if (MODEL.getClass() == value.getClass()) {
    106             return (value);
    107         }
    108
    109         // Deal with input value as a String array
    110         if (strings.getClass() == value.getClass()) {
    111             try {
    112                 String[] values = (String[]) value;
    113                 char[] results = new char[values.length];
    114                 for (int i = 0; i < values.length; i++) {
    115                     results[i] = values[i].charAt(0);
    116                 }
    117                 return (results);
    118             } catch (Exception e) {
    119                 if (useDefault) {
    120                     return (defaultValue);
    121                 } else {
    122                     throw new ConversionException(value.toString(), e);
    123                 }
    124             }
    125         }
    126
    127         // Parse the input value as a String into elements
    128         // and convert to the appropriate type
    129         try {
    130             List list = parseElements(value.toString());
    131             char[] results = new char[list.size()];
    132             for (int i = 0; i < results.length; i++) {
    133                 results[i] = ((String) list.get(i)).charAt(0);
    134             }
    135             return (results);
    136         } catch (Exception e) {
    137             if (useDefault) {
    138                 return (defaultValue);
    139             } else {
    140                 throw new ConversionException(value.toString(), e);
    141             }
    142         }
    143
    144     }
    145
    146
    147 }