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.sql.Timestamp;
  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.sql.Timestamp</code> object, optionally using a
  23. * default value or throwing a {@link ConversionException} if a conversion
  24. * error occurs.</p>
  25. *
  26. * @author Craig R. McClanahan
  27. * @version $Revision: 1.7 $ $Date: 2004/02/28 13:18:34 $
  28. * @since 1.3
  29. */
  30. public final class SqlTimestampConverter implements Converter {
  31. // ----------------------------------------------------------- Constructors
  32. /**
  33. * Create a {@link Converter} that will throw a {@link ConversionException}
  34. * if a conversion error occurs.
  35. */
  36. public SqlTimestampConverter() {
  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 SqlTimestampConverter(Object defaultValue) {
  47. this.defaultValue = defaultValue;
  48. this.useDefault = true;
  49. }
  50. // ----------------------------------------------------- Instance Variables
  51. /**
  52. * The default value specified to our Constructor, if any.
  53. */
  54. private Object defaultValue = null;
  55. /**
  56. * Should we return the default value on conversion errors?
  57. */
  58. private boolean useDefault = true;
  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 Timestamp) {
  79. return (value);
  80. }
  81. try {
  82. return (Timestamp.valueOf(value.toString()));
  83. } catch (Exception e) {
  84. if (useDefault) {
  85. return (defaultValue);
  86. } else {
  87. throw new ConversionException(e);
  88. }
  89. }
  90. }
  91. }