1. /*
  2. * Copyright 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.betwixt.IntrospectionConfiguration;
  18. /**
  19. * Strategy for binding simple types.
  20. * Simple types (in xml) have no attributes or child elements.
  21. * For Betwixt, these are converted to and from strings
  22. * and these strings used to populate either attributes or element body's.
  23. * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
  24. * @version $Revision: 1.2 $
  25. */
  26. public abstract class SimpleTypeMapper {
  27. /**
  28. * Enumerates binding options for simple types.
  29. * Simple types (in xml) have no attributes or child elements.
  30. * For Betwixt, these are converted to and from strings
  31. * and these strings used to populate either attributes or element body's.
  32. * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
  33. * @version $Revision: 1.2 $
  34. */
  35. public static class Binding {
  36. public static final Binding ELEMENT = new Binding(1);
  37. public static final Binding ATTRIBUTE = new Binding(2);
  38. private static final int ELEMENT_CODE = 1;
  39. private static final int ATTRIBUTE_CODE = 2;
  40. private int code;
  41. private Binding(int code) {
  42. this.code = code;
  43. }
  44. /**
  45. * Equals compatible with the enumeration.
  46. */
  47. public boolean equals( Object obj ) {
  48. boolean result = false;
  49. if ( obj == this ) {
  50. result = true;
  51. }
  52. return result;
  53. }
  54. /**
  55. * Implementation compatible with equals
  56. */
  57. public int hashCode() {
  58. return code;
  59. }
  60. /**
  61. * Generate something appropriate for logging.
  62. */
  63. public String toString() {
  64. String result = "[Binding]";
  65. switch (code) {
  66. case ELEMENT_CODE:
  67. result = "[Binding: ELEMENT]";
  68. break;
  69. case ATTRIBUTE_CODE:
  70. result = "[Binding: ATTRIBUTE]";
  71. break;
  72. }
  73. return result;
  74. }
  75. }
  76. /**
  77. * <p>Specifies the binding of a simple type.
  78. * </p><p>
  79. * <strong>Note:</strong> the xml name to which this property will be bound
  80. * cannot be known at this stage (since it depends
  81. * </p>
  82. * @param propertyName the name of the property (to be bound)
  83. * @param propertyType the type of the property (to be bound)
  84. * @param configuration the current IntrospectionConfiguration
  85. */
  86. public abstract SimpleTypeMapper.Binding bind(
  87. String propertyName,
  88. Class propertyType,
  89. IntrospectionConfiguration configuration);
  90. }