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