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