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.io.File;
  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 <code>java.io.FileL</code> object, optionally using a
  23. * default value or throwing a {@link ConversionException} if a conversion
  24. * error occurs.</p>
  25. *
  26. * @author James Strachan
  27. * @version $Revision: 1.4 $ $Date: 2004/02/28 13:18:34 $
  28. * @since 1.6
  29. */
  30. public final class FileConverter implements Converter {
  31. // ----------------------------------------------------- Instance Variables
  32. /**
  33. * The default value specified to our Constructor, if any.
  34. */
  35. private Object defaultValue = null;
  36. /**
  37. * Should we return the default value on conversion errors?
  38. */
  39. private boolean useDefault = true;
  40. // ----------------------------------------------------------- Constructors
  41. /**
  42. * Create a {@link Converter} that will throw a {@link ConversionException}
  43. * if a conversion error occurs.
  44. */
  45. public FileConverter() {
  46. this.defaultValue = null;
  47. this.useDefault = false;
  48. }
  49. /**
  50. * Create a {@link Converter} that will return the specified default value
  51. * if a conversion error occurs.
  52. *
  53. * @param defaultValue The default value to be returned
  54. */
  55. public FileConverter(Object defaultValue) {
  56. this.defaultValue = defaultValue;
  57. this.useDefault = true;
  58. }
  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. if (value == null) {
  72. if (useDefault) {
  73. return (defaultValue);
  74. } else {
  75. throw new ConversionException("No value specified");
  76. }
  77. }
  78. if (value instanceof File) {
  79. return (value);
  80. }
  81. return new File(value.toString());
  82. }
  83. }