1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  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 org.apache.commons.beanutils.converters;
  17. import java.util.List;
  18. import org.apache.commons.beanutils.ConversionException;
  19. import org.apache.commons.beanutils.Converter;
  20. /**
  21. * <p>Standard {@link Converter} implementation that converts an incoming
  22. * String into an array of String. On a conversion failure, returns
  23. * a specified default value or throws a {@link ConversionException} depending
  24. * on how this instance is constructed.</p>
  25. *
  26. * @author Craig R. McClanahan
  27. * @version $Revision: 1.7 $ $Date: 2004/02/28 13:18:34 $
  28. * @since 1.4
  29. */
  30. public final class StringArrayConverter extends AbstractArrayConverter {
  31. // ----------------------------------------------------------- Constructors
  32. /**
  33. * Create a {@link Converter} that will throw a {@link ConversionException}
  34. * if a conversion error occurs.
  35. */
  36. public StringArrayConverter() {
  37. this.defaultValue = null;
  38. this.useDefault = false;
  39. }
  40. /**
  41. * Create a {@link Converter} that will return the specified default value
  42. * if a conversion error occurs.
  43. *
  44. * @param defaultValue The default value to be returned
  45. */
  46. public StringArrayConverter(Object defaultValue) {
  47. this.defaultValue = defaultValue;
  48. this.useDefault = true;
  49. }
  50. // ------------------------------------------------------- Static Variables
  51. /**
  52. * <p>Model object for type comparisons.</p>
  53. */
  54. private static String model[] = new String[0];
  55. /**
  56. * <p> Model object for int arrays.</p>
  57. */
  58. private static int ints[] = new int[0];
  59. // --------------------------------------------------------- Public Methods
  60. /**
  61. * Convert the specified input object into an output object of the
  62. * specified type.
  63. *
  64. * @param type Data type to which this value should be converted
  65. * @param value The input value to be converted
  66. *
  67. * @exception ConversionException if conversion cannot be performed
  68. * successfully
  69. */
  70. public Object convert(Class type, Object value) {
  71. // Deal with a null value
  72. if (value == null) {
  73. if (useDefault) {
  74. return (defaultValue);
  75. } else {
  76. throw new ConversionException("No value specified");
  77. }
  78. }
  79. // Deal with the no-conversion-needed case
  80. if (model.getClass() == value.getClass()) {
  81. return (value);
  82. }
  83. // Deal with the input value as an int array
  84. if (ints.getClass() == value.getClass())
  85. {
  86. int[] values = (int[]) value;
  87. String[] results = new String[values.length];
  88. for (int i = 0; i < values.length; i++)
  89. {
  90. results[i] = Integer.toString(values[i]);
  91. }
  92. return (results);
  93. }
  94. // Parse the input value as a String into elements
  95. // and convert to the appropriate type
  96. try {
  97. List list = parseElements(value.toString());
  98. String results[] = new String[list.size()];
  99. for (int i = 0; i < results.length; i++) {
  100. results[i] = (String) list.get(i);
  101. }
  102. return (results);
  103. } catch (Exception e) {
  104. if (useDefault) {
  105. return (defaultValue);
  106. } else {
  107. throw new ConversionException(value.toString(), e);
  108. }
  109. }
  110. }
  111. }