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 a primitive array of boolean. 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 BooleanArrayConverter 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 BooleanArrayConverter() {
  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 BooleanArrayConverter(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 boolean model[] = new boolean[0];
  55. // --------------------------------------------------------- Public Methods
  56. /**
  57. * Convert the specified input object into an output object of the
  58. * specified type.
  59. *
  60. * @param type Data type to which this value should be converted
  61. * @param value The input value to be converted
  62. *
  63. * @exception ConversionException if conversion cannot be performed
  64. * successfully
  65. */
  66. public Object convert(Class type, Object value) {
  67. // Deal with a null value
  68. if (value == null) {
  69. if (useDefault) {
  70. return (defaultValue);
  71. } else {
  72. throw new ConversionException("No value specified");
  73. }
  74. }
  75. // Deal with the no-conversion-needed case
  76. if (model.getClass() == value.getClass()) {
  77. return (value);
  78. }
  79. // Deal with input value as a String array
  80. if (strings.getClass() == value.getClass()) {
  81. try {
  82. String values[] = (String[]) value;
  83. boolean results[] = new boolean[values.length];
  84. for (int i = 0; i < values.length; i++) {
  85. String stringValue = values[i];
  86. if (stringValue.equalsIgnoreCase("yes") ||
  87. stringValue.equalsIgnoreCase("y") ||
  88. stringValue.equalsIgnoreCase("true") ||
  89. stringValue.equalsIgnoreCase("on") ||
  90. stringValue.equalsIgnoreCase("1")) {
  91. results[i] = true;
  92. } else if (stringValue.equalsIgnoreCase("no") ||
  93. stringValue.equalsIgnoreCase("n") ||
  94. stringValue.equalsIgnoreCase("false") ||
  95. stringValue.equalsIgnoreCase("off") ||
  96. stringValue.equalsIgnoreCase("0")) {
  97. results[i] = false;
  98. } else {
  99. if (useDefault) {
  100. return (defaultValue);
  101. } else {
  102. throw new ConversionException(value.toString());
  103. }
  104. }
  105. }
  106. return (results);
  107. } catch (Exception e) {
  108. if (useDefault) {
  109. return (defaultValue);
  110. } else {
  111. throw new ConversionException(value.toString(), e);
  112. }
  113. }
  114. }
  115. // Parse the input value as a String into elements
  116. // and convert to the appropriate type
  117. try {
  118. List list = parseElements(value.toString());
  119. boolean results[] = new boolean[list.size()];
  120. for (int i = 0; i < results.length; i++) {
  121. String stringValue = (String) list.get(i);
  122. if (stringValue.equalsIgnoreCase("yes") ||
  123. stringValue.equalsIgnoreCase("y") ||
  124. stringValue.equalsIgnoreCase("true") ||
  125. stringValue.equalsIgnoreCase("on") ||
  126. stringValue.equalsIgnoreCase("1")) {
  127. results[i] = true;
  128. } else if (stringValue.equalsIgnoreCase("no") ||
  129. stringValue.equalsIgnoreCase("n") ||
  130. stringValue.equalsIgnoreCase("false") ||
  131. stringValue.equalsIgnoreCase("off") ||
  132. stringValue.equalsIgnoreCase("0")) {
  133. results[i] = false;
  134. } else {
  135. if (useDefault) {
  136. return (defaultValue);
  137. } else {
  138. throw new ConversionException(value.toString());
  139. }
  140. }
  141. }
  142. return (results);
  143. } catch (Exception e) {
  144. if (useDefault) {
  145. return (defaultValue);
  146. } else {
  147. throw new ConversionException(value.toString(), e);
  148. }
  149. }
  150. }
  151. }