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 org.apache.commons.beanutils.ConversionException;
  18. import org.apache.commons.beanutils.Converter;
  19. /**
  20. * <p>Standard {@link Converter} implementation that converts an incoming
  21. * String into a <code>java.lang.Boolean</code> object, optionally using a
  22. * default value or throwing a {@link ConversionException} if a conversion
  23. * error occurs.</p>
  24. *
  25. * @author Craig R. McClanahan
  26. * @version $Revision: 1.9 $ $Date: 2004/02/28 13:18:34 $
  27. * @since 1.3
  28. */
  29. public final class BooleanConverter implements Converter {
  30. // ----------------------------------------------------------- Constructors
  31. /**
  32. * Create a {@link Converter} that will throw a {@link ConversionException}
  33. * if a conversion error occurs.
  34. */
  35. public BooleanConverter() {
  36. this.defaultValue = null;
  37. this.useDefault = false;
  38. }
  39. /**
  40. * Create a {@link Converter} that will return the specified default value
  41. * if a conversion error occurs.
  42. *
  43. * @param defaultValue The default value to be returned
  44. */
  45. public BooleanConverter(Object defaultValue) {
  46. this.defaultValue = defaultValue;
  47. this.useDefault = true;
  48. }
  49. // ----------------------------------------------------- Instance Variables
  50. /**
  51. * The default value specified to our Constructor, if any.
  52. */
  53. private Object defaultValue = null;
  54. /**
  55. * Should we return the default value on conversion errors?
  56. */
  57. private boolean useDefault = true;
  58. // --------------------------------------------------------- Public Methods
  59. /**
  60. * Convert the specified input object into an output object of the
  61. * specified type.
  62. *
  63. * @param type Data type to which this value should be converted
  64. * @param value The input value to be converted
  65. *
  66. * @exception ConversionException if conversion cannot be performed
  67. * successfully
  68. */
  69. public Object convert(Class type, Object value) {
  70. if (value == null) {
  71. if (useDefault) {
  72. return (defaultValue);
  73. } else {
  74. throw new ConversionException("No value specified");
  75. }
  76. }
  77. if (value instanceof Boolean) {
  78. return (value);
  79. }
  80. try {
  81. String stringValue = value.toString();
  82. if (stringValue.equalsIgnoreCase("yes") ||
  83. stringValue.equalsIgnoreCase("y") ||
  84. stringValue.equalsIgnoreCase("true") ||
  85. stringValue.equalsIgnoreCase("on") ||
  86. stringValue.equalsIgnoreCase("1")) {
  87. return (Boolean.TRUE);
  88. } else if (stringValue.equalsIgnoreCase("no") ||
  89. stringValue.equalsIgnoreCase("n") ||
  90. stringValue.equalsIgnoreCase("false") ||
  91. stringValue.equalsIgnoreCase("off") ||
  92. stringValue.equalsIgnoreCase("0")) {
  93. return (Boolean.FALSE);
  94. } else if (useDefault) {
  95. return (defaultValue);
  96. } else {
  97. throw new ConversionException(stringValue);
  98. }
  99. } catch (ClassCastException e) {
  100. if (useDefault) {
  101. return (defaultValue);
  102. } else {
  103. throw new ConversionException(e);
  104. }
  105. }
  106. }
  107. }