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. import java.net.URL;
  20. import java.net.MalformedURLException;
  21. /**
  22. * <p>Standard {@link Converter} implementation that converts an incoming
  23. * String into a <code>java.net.URL</code> object, optionally using a
  24. * default value or throwing a {@link ConversionException} if a conversion
  25. * error occurs.</p>
  26. *
  27. * @author Henri Yandell
  28. * @version $Revision: 1.4 $ $Date: 2004/02/28 13:18:34 $
  29. * @since 1.3
  30. */
  31. public final class URLConverter 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 URLConverter() {
  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 URLConverter(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 URL) {
  80. return (value);
  81. }
  82. try {
  83. return new URL(value.toString());
  84. } catch(MalformedURLException murle) {
  85. if (useDefault) {
  86. return (defaultValue);
  87. } else {
  88. throw new ConversionException(murle);
  89. }
  90. }
  91. }
  92. }