Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
HyphenatedNameMapper |
|
| 1.7;1.7 |
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 | ||
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: 238403 $ |
|
32 | */ |
|
33 | public class HyphenatedNameMapper implements NameMapper { |
|
34 | ||
35 | /** the separator used to seperate words, which defaults to '-' */ |
|
36 | 468 | private String separator = "-"; |
37 | ||
38 | /** whether upper or lower case conversions should be performed */ |
|
39 | 468 | 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 | 325 | public HyphenatedNameMapper() { |
46 | 325 | } |
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 | 13 | public HyphenatedNameMapper(boolean upperCase) { |
54 | 13 | this.upperCase = upperCase; |
55 | 13 | } |
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 | 130 | public HyphenatedNameMapper(boolean upperCase, String separator) { |
66 | 130 | this.upperCase = upperCase; |
67 | 130 | this.separator = separator; |
68 | 130 | } |
69 | ||
70 | /** |
|
71 | * <p>The words within the bean name are deduced assuming the |
|
72 | * first-letter-capital (for example 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 | 3354 | int length = typeName.length(); |
92 | 3354 | if (length == 0) { |
93 | 0 | return ""; |
94 | } |
|
95 | ||
96 | 3354 | StringBuffer sb = new StringBuffer(); |
97 | ||
98 | 3354 | sb.append(convertChar(typeName.charAt(0))); |
99 | ||
100 | 27222 | for (int i = 1; i < length; i++) { |
101 | 23868 | if (Character.isUpperCase(typeName.charAt(i))) { |
102 | 1859 | sb.append(separator); |
103 | 1859 | sb.append(convertChar(typeName.charAt(i))); |
104 | } else { |
|
105 | 22009 | if ( upperCase ) { |
106 | 3991 | sb.append(convertChar(typeName.charAt(i))); |
107 | } else { |
|
108 | 18018 | sb.append(typeName.charAt(i)); |
109 | } |
|
110 | } |
|
111 | } |
|
112 | ||
113 | 3354 | return sb.toString(); |
114 | } |
|
115 | ||
116 | // Properties |
|
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 | 0 | 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 | 13 | this.separator = separator; |
134 | 13 | } |
135 | ||
136 | /** |
|
137 | * <p>Should the bean name be converted to upper case? |
|
138 | * </p> |
|
139 | * <p> |
|
140 | * Otherwise, it will be converted to lower case. |
|
141 | * </p> |
|
142 | * @return whether upper or lower case conversions should be performed, |
|
143 | * which defaults to false for lower case |
|
144 | */ |
|
145 | public boolean isUpperCase() { |
|
146 | 0 | return upperCase; |
147 | } |
|
148 | ||
149 | /** |
|
150 | * Sets whether upper or lower case conversions should be performed, |
|
151 | * which defaults to false for lower case. |
|
152 | * |
|
153 | * @param upperCase whether the name is to be converted to upper case |
|
154 | */ |
|
155 | public void setUpperCase(boolean upperCase) { |
|
156 | 13 | this.upperCase = upperCase; |
157 | 13 | } |
158 | ||
159 | // Implementation methods |
|
160 | //------------------------------------------------------------------------- |
|
161 | ||
162 | /** |
|
163 | * Performs type conversion on the given character based on whether |
|
164 | * upper or lower case conversions are being used |
|
165 | * |
|
166 | * @param ch the character to be converted |
|
167 | * @return converted to upper case if {@link #isUpperCase} otherwise to lower case |
|
168 | */ |
|
169 | protected char convertChar(char ch) { |
|
170 | 9204 | if ( upperCase ) { |
171 | 5265 | return Character.toUpperCase(ch); |
172 | ||
173 | } else { |
|
174 | 3939 | return Character.toLowerCase(ch); |
175 | } |
|
176 | } |
|
177 | ||
178 | /** |
|
179 | * Outputs brief description. |
|
180 | */ |
|
181 | public String toString() { |
|
182 | 0 | return "Hyphenated Name Mapper"; |
183 | } |
|
184 | } |