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.betwixt.strategy;
  17. import org.apache.commons.beanutils.ConvertUtils;
  18. import org.apache.commons.betwixt.expression.Context;
  19. /**
  20. * String <-> object conversion strategy that delegates to ConvertUtils.
  21. *
  22. * @author Robert Burrell Donkin
  23. * @since 0.5
  24. */
  25. public class ConvertUtilsObjectStringConverter extends ObjectStringConverter {
  26. /**
  27. * Converts an object to a string representation using ConvertUtils.
  28. *
  29. * @param object the object to be converted, possibly null
  30. * @param type the property class of the object, not null
  31. * @param flavour a string allow symantic differences in formatting
  32. * to be communicated (ignored)
  33. * @param context not null
  34. * @return a String representation, not null
  35. */
  36. public String objectToString(Object object, Class type, String flavour, Context context) {
  37. if ( object != null ) {
  38. String text = ConvertUtils.convert( object );
  39. if ( text != null ) {
  40. return text;
  41. }
  42. }
  43. return "";
  44. }
  45. /**
  46. * Converts an object to a string representation using ConvertUtils.
  47. * This implementation ignores null and empty string values (rather than converting them).
  48. *
  49. * @param value the String to be converted, not null
  50. * @param type the property class to be returned (if possible), not null
  51. * @param flavour a string allow symantic differences in formatting
  52. * to be communicated (ignored)
  53. * @param context not null
  54. * @return an Object converted from the String, not null
  55. */
  56. public Object stringToObject(String value, Class type, String flavour, Context context) {
  57. if (value == null || "".equals(value))
  58. {
  59. return null;
  60. }
  61. return ConvertUtils.convert( value, type );
  62. }
  63. }