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.Class</code> object, optionally using a
  22. * default value or throwing a {@link ConversionException} if a conversion
  23. * error occurs. The class will be loaded from the thread context class
  24. * loader (if it exists); otherwise the class loader that loaded this class
  25. * will be used.</p>
  26. *
  27. * @author Tomas Viberg
  28. * @version $Revision: 1.6 $ $Date: 2004/02/28 13:18:34 $
  29. * @since 1.4
  30. */
  31. public final class ClassConverter implements Converter {
  32. // ----------------------------------------------------------- Constructors
  33. /**
  34. * Create a {@link Converter} that will throw a {@link ConversionException}
  35. * if a conversion error occurs.
  36. */
  37. public ClassConverter() {
  38. this.defaultValue = null;
  39. this.useDefault = false;
  40. }
  41. /**
  42. * Create a {@link Converter} that will return the specified default value
  43. * if a conversion error occurs.
  44. *
  45. * @param defaultValue The default value to be returned
  46. */
  47. public ClassConverter(Object defaultValue) {
  48. this.defaultValue = defaultValue;
  49. this.useDefault = true;
  50. }
  51. // ----------------------------------------------------- Instance Variables
  52. /**
  53. * The default value specified to our Constructor, if any.
  54. */
  55. private Object defaultValue = null;
  56. /**
  57. * Should we return the default value on conversion errors?
  58. */
  59. private boolean useDefault = true;
  60. // --------------------------------------------------------- Public Methods
  61. /**
  62. * Convert the specified input object into an output object of the
  63. * specified type.
  64. *
  65. * @param type Data type to which this value should be converted
  66. * @param value The input value to be converted
  67. *
  68. * @exception ConversionException if conversion cannot be performed
  69. * successfully
  70. */
  71. public Object convert(Class type, Object value) {
  72. if (value == null) {
  73. if (useDefault) {
  74. return (defaultValue);
  75. } else {
  76. throw new ConversionException("No value specified");
  77. }
  78. }
  79. if (value instanceof Class) {
  80. return (value);
  81. }
  82. try {
  83. ClassLoader classLoader =
  84. Thread.currentThread().getContextClassLoader();
  85. if (classLoader == null) {
  86. classLoader = ClassConverter.class.getClassLoader();
  87. }
  88. return (classLoader.loadClass(value.toString()));
  89. } catch (Exception e) {
  90. if (useDefault) {
  91. return (defaultValue);
  92. } else {
  93. throw new ConversionException(e);
  94. }
  95. }
  96. }
  97. }