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. /**
  18. * A name mapper which converts types to a hypenated String. So
  19. * a bean type of FooBar will be converted to the element name "foo-bar".
  20. * The name mapper can be configured to convert to upper case and to
  21. * use a different separator via the <code>separator</code> and
  22. * <code>upperCase</code> properties, so that FooBar can be converted
  23. * to FOO_BAR if needed, by calling the constructor
  24. * <code>new HyphenatedNameMapper(true, "_")</code>.
  25. *
  26. * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
  27. * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
  28. * @version $Revision: 1.11 $
  29. */
  30. public class HyphenatedNameMapper implements NameMapper {
  31. /** the separator used to seperate words, which defaults to '-' */
  32. private String separator = "-";
  33. /** whether upper or lower case conversions should be performed */
  34. private boolean upperCase = false;
  35. /**
  36. * Construct a hyphenated name mapper that converts the name to lower case
  37. * and uses the default separator.
  38. */
  39. public HyphenatedNameMapper() {
  40. }
  41. /**
  42. * Construct a hyphenated name mapper with default separator.
  43. *
  44. * @param upperCase should the type name be converted (entirely) to upper case
  45. */
  46. public HyphenatedNameMapper(boolean upperCase) {
  47. this.upperCase = upperCase;
  48. }
  49. /**
  50. * Construct a hyphenated name mapper.
  51. *
  52. * @param upperCase should the type name be converted (entirely) to upper case
  53. * @param separator use this string to separate the words in the name returned.
  54. * The words in the bean name are deduced by relying on the standard camel's hump
  55. * property naming convention.
  56. */
  57. public HyphenatedNameMapper(boolean upperCase, String separator) {
  58. this.upperCase = upperCase;
  59. this.separator = separator;
  60. }
  61. /**
  62. * <p>The words within the bean name are deduced assuming the
  63. * first-letter-capital (e.g. camel's hump) naming convention. For
  64. * example, the words in <code>FooBar</code> are <code>foo</code>
  65. * and <code>bar</code>.</p>
  66. *
  67. * <p>Next convert all letter in the bean name to either upper case or lower case
  68. * based on the {@link #isUpperCase} property value.</p>
  69. *
  70. * <p>Then the {@link #getSeparator} property value is inserted so that it separates
  71. * each word.</p>
  72. *
  73. * @param typeName The name string to convert. If a JavaBean
  74. * class name, should included only the last part of the name
  75. * rather than the fully qualified name (e.g. FooBar rather than
  76. * org.example.FooBar).
  77. * @return the bean name converted to either upper or lower case with words separated
  78. * by the separator.
  79. */
  80. public String mapTypeToElementName(String typeName) {
  81. int length = typeName.length();
  82. if (length == 0) {
  83. return "";
  84. }
  85. StringBuffer sb = new StringBuffer();
  86. sb.append(convertChar(typeName.charAt(0)));
  87. for (int i = 1; i < length; i++) {
  88. if (Character.isUpperCase(typeName.charAt(i))) {
  89. sb.append(separator);
  90. sb.append(convertChar(typeName.charAt(i)));
  91. } else {
  92. if ( upperCase ) {
  93. sb.append(convertChar(typeName.charAt(i)));
  94. } else {
  95. sb.append(typeName.charAt(i));
  96. }
  97. }
  98. }
  99. return sb.toString();
  100. }
  101. // Properties
  102. //-------------------------------------------------------------------------
  103. /**
  104. * This separator will be inserted between the words in the bean name.
  105. *
  106. * @return the separator used to seperate words, which defaults to '-'
  107. */
  108. public String getSeparator() {
  109. return separator;
  110. }
  111. /**
  112. * Sets the separator used to seperate words, which defaults to '-'
  113. *
  114. * @param separator the string inserted to separate words
  115. */
  116. public void setSeparator(String separator) {
  117. this.separator = separator;
  118. }
  119. /**
  120. * Should the bean name be converted to upper case?
  121. * Otherwise, it will be converted to lower case.
  122. *
  123. * @return whether upper or lower case conversions should be performed,
  124. * which defaults to false for lower case
  125. */
  126. public boolean isUpperCase() {
  127. return upperCase;
  128. }
  129. /**
  130. * Sets whether upper or lower case conversions should be performed,
  131. * which defaults to false for lower case.
  132. *
  133. * @param upperCase whether the name is to be converted to upper case
  134. */
  135. public void setUpperCase(boolean upperCase) {
  136. this.upperCase = upperCase;
  137. }
  138. // Implementation methods
  139. //-------------------------------------------------------------------------
  140. /**
  141. * Performs type conversion on the given character based on whether
  142. * upper or lower case conversions are being used
  143. *
  144. * @param ch the character to be converted
  145. * @return converted to upper case if {@link #isUpperCase} otherwise to lower case
  146. */
  147. protected char convertChar(char ch) {
  148. if ( upperCase ) {
  149. return Character.toUpperCase(ch);
  150. } else {
  151. return Character.toLowerCase(ch);
  152. }
  153. }
  154. }