001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.betwixt.strategy;
018:
019: /**
020: * A name mapper which converts types to a hypenated String. So
021: * a bean type of FooBar will be converted to the element name "foo-bar".
022: * The name mapper can be configured to convert to upper case and to
023: * use a different separator via the <code>separator</code> and
024: * <code>upperCase</code> properties, so that FooBar can be converted
025: * to FOO_BAR if needed, by calling the constructor
026: * <code>new HyphenatedNameMapper(true, "_")</code>.
027: *
028: * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
029: * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
030: * @version $Revision: 471234 $
031: */
032: public class HyphenatedNameMapper implements NameMapper {
033:
034: /** the separator used to seperate words, which defaults to '-' */
035: private String separator = "-";
036:
037: /** whether upper or lower case conversions should be performed */
038: private boolean upperCase = false;
039:
040: /**
041: * Construct a hyphenated name mapper that converts the name to lower case
042: * and uses the default separator.
043: */
044: public HyphenatedNameMapper() {
045: }
046:
047: /**
048: * Construct a hyphenated name mapper with default separator.
049: *
050: * @param upperCase should the type name be converted (entirely) to upper case
051: */
052: public HyphenatedNameMapper(boolean upperCase) {
053: this .upperCase = upperCase;
054: }
055:
056: /**
057: * Construct a hyphenated name mapper.
058: *
059: * @param upperCase should the type name be converted (entirely) to upper case
060: * @param separator use this string to separate the words in the name returned.
061: * The words in the bean name are deduced by relying on the standard camel's hump
062: * property naming convention.
063: */
064: public HyphenatedNameMapper(boolean upperCase, String separator) {
065: this .upperCase = upperCase;
066: this .separator = separator;
067: }
068:
069: /**
070: * <p>The words within the bean name are deduced assuming the
071: * first-letter-capital (for example camel's hump) naming convention. For
072: * example, the words in <code>FooBar</code> are <code>foo</code>
073: * and <code>bar</code>.</p>
074: *
075: * <p>Next convert all letter in the bean name to either upper case or lower case
076: * based on the {@link #isUpperCase} property value.</p>
077: *
078: * <p>Then the {@link #getSeparator} property value is inserted so that it separates
079: * each word.</p>
080: *
081: * @param typeName The name string to convert. If a JavaBean
082: * class name, should included only the last part of the name
083: * rather than the fully qualified name (e.g. FooBar rather than
084: * org.example.FooBar).
085: * @return the bean name converted to either upper or lower case with words separated
086: * by the separator.
087: */
088: public String mapTypeToElementName(String typeName) {
089:
090: int length = typeName.length();
091: if (length == 0) {
092: return "";
093: }
094:
095: StringBuffer sb = new StringBuffer();
096:
097: sb.append(convertChar(typeName.charAt(0)));
098:
099: for (int i = 1; i < length; i++) {
100: if (Character.isUpperCase(typeName.charAt(i))) {
101: sb.append(separator);
102: sb.append(convertChar(typeName.charAt(i)));
103: } else {
104: if (upperCase) {
105: sb.append(convertChar(typeName.charAt(i)));
106: } else {
107: sb.append(typeName.charAt(i));
108: }
109: }
110: }
111:
112: return sb.toString();
113: }
114:
115: // Properties
116: //-------------------------------------------------------------------------
117: /**
118: * This separator will be inserted between the words in the bean name.
119: *
120: * @return the separator used to seperate words, which defaults to '-'
121: */
122: public String getSeparator() {
123: return separator;
124: }
125:
126: /**
127: * Sets the separator used to seperate words, which defaults to '-'
128: *
129: * @param separator the string inserted to separate words
130: */
131: public void setSeparator(String separator) {
132: this .separator = separator;
133: }
134:
135: /**
136: * <p>Should the bean name be converted to upper case?
137: * </p>
138: * <p>
139: * Otherwise, it will be converted to lower case.
140: * </p>
141: * @return whether upper or lower case conversions should be performed,
142: * which defaults to false for lower case
143: */
144: public boolean isUpperCase() {
145: return upperCase;
146: }
147:
148: /**
149: * Sets whether upper or lower case conversions should be performed,
150: * which defaults to false for lower case.
151: *
152: * @param upperCase whether the name is to be converted to upper case
153: */
154: public void setUpperCase(boolean upperCase) {
155: this .upperCase = upperCase;
156: }
157:
158: // Implementation methods
159: //-------------------------------------------------------------------------
160:
161: /**
162: * Performs type conversion on the given character based on whether
163: * upper or lower case conversions are being used
164: *
165: * @param ch the character to be converted
166: * @return converted to upper case if {@link #isUpperCase} otherwise to lower case
167: */
168: protected char convertChar(char ch) {
169: if (upperCase) {
170: return Character.toUpperCase(ch);
171:
172: } else {
173: return Character.toLowerCase(ch);
174: }
175: }
176:
177: /**
178: * Outputs brief description.
179: * @since 0.8
180: */
181: public String toString() {
182: return "Hyphenated Name Mapper";
183: }
184: }
|