1 19 package org.enhydra.zeus.util; 20 21 import java.util.HashMap ; 22 import java.util.Map ; 23 24 import org.enhydra.zeus.ZeusDefaults; 26 27 40 public class NamingUtils { 41 42 43 public static final String XML_PREFIX = "xml"; 44 45 46 public static final String DEFAULT_MAPPING = ""; 47 48 51 private static Map illegalCharMap = new HashMap (); 52 53 54 private static HashMap reservedWords = new HashMap (); 55 56 57 private static String reservedWordsArray[] = { "abstract", 58 "boolean", "break", "byte", "case", "catch", "char", "class", 59 "char", "const", "continue", "default", "do", "double", "else", 60 "extends", "false", "final", "finally", "float", "for", 61 "goto", "if", "implements", "import", "instanceof", "int", 62 "interface", "long", "native", "new", "null", "package", 63 "private", "protected", "public", "return", "short", "static", 64 "super", "switch", "synchronized", "this", "throw", "throws", 65 "transient", "true", "try", "void", "volatile", "while" }; 66 67 static { 69 for (int i = 0; i < reservedWordsArray.length; i++) { 70 reservedWords.put(reservedWordsArray[i], new Integer (i)); 71 } 72 reservedWordsArray = null; 73 74 illegalCharMap.put(new Character ('.'), DEFAULT_MAPPING); 77 illegalCharMap.put(new Character ('-'), DEFAULT_MAPPING); 78 illegalCharMap.put(new Character (':'), DEFAULT_MAPPING); 79 } 80 81 86 private NamingUtils() {} 87 88 98 public static boolean isLegalJavaClassName(String className) { 99 if (className == null) { 100 throw new IllegalArgumentException ("A non-null String must be " + 101 "supplied to NamingUtils methods."); 102 } 103 104 for (int i=0, len=className.length(); i<len; i++) { 105 if ((i == 0) && 106 (!Character.isJavaIdentifierStart(className.charAt(i)))) { 107 return false; 108 } else if (!Character.isJavaIdentifierPart(className.charAt(i))) { 109 return false; 110 } 111 } 112 113 return true; 115 } 116 117 127 public static boolean isLegalJavaPackageName(String packageName) { 128 if (packageName == null) { 129 throw new IllegalArgumentException ("A non-null String must be " + 130 "supplied to NamingUtils methods."); 131 } 132 133 for (int i=0, len=packageName.length(); i<len; i++) { 134 if ((i == 0) && 135 (!Character.isJavaIdentifierStart(packageName.charAt(i)))) { 136 return false; 137 } else if ( 138 (!Character.isJavaIdentifierPart(packageName.charAt(i))) && 139 (!(packageName.charAt(i) == '.'))) { 140 141 return false; 142 } 143 } 144 145 return true; 147 } 148 149 159 public static String getCharMapping(Character key) { 160 if (key == null) { 161 throw new IllegalArgumentException ("A non-null Character must be " + 162 "supplied to NamingUtils methods."); 163 } 164 165 return (String )illegalCharMap.get(key); 166 } 167 168 178 public static void setCharMapping(Character key, String value) { 179 if (key == null) { 180 throw new IllegalArgumentException ("A non-null char key must be " + 181 "supplied to NamingUtils methods."); 182 } 183 if (value == null) { 184 throw new IllegalArgumentException ("A non-null value must be " + 185 "supplied to NamingUtils methods."); 186 } 187 188 illegalCharMap.put(key, value); 189 } 190 191 204 public static String getJavaName(String xmlName) { 205 if (xmlName == null) { 206 throw new IllegalArgumentException ("A non-null XML name must be " + 207 "supplied to NamingUtils methods."); 208 } 209 210 if (xmlName.equals(ZeusDefaults.PCDATA_XML_NAME)) { 212 return convertIllegalChars(ZeusDefaults.PCDATA_JAVA_NAME); 213 } 214 215 return convertReservedWord(convertIllegalChars(xmlName)); 216 } 217 218 232 public static String getJavaVariableName(String xmlName) { 233 if (xmlName == null) { 234 throw new IllegalArgumentException ("A non-null XML name must " + 235 "be supplied to NamingUtils methods."); 236 } 237 238 if (xmlName.equals(ZeusDefaults.PCDATA_XML_NAME)) { 240 return convertReservedWord( 241 CapitalizationUtils.initialLower( 242 ZeusDefaults.PCDATA_JAVA_NAME)); 243 } 244 245 return convertReservedWord( 246 convertIllegalChars(CapitalizationUtils.initialLower(xmlName))); 247 } 248 249 268 public static String getJavaCollectionVariableName(String className) { 269 if (className == null) { 270 throw new IllegalArgumentException ("A non-null class name must " + 271 "be supplied to NamingUtils methods."); 272 } 273 274 return convertReservedWord( 275 convertIllegalChars(CapitalizationUtils.initialLower(className))); 276 } 277 278 292 public static String getJavaClassName(String xmlName) { 293 if (xmlName == null) { 294 throw new IllegalArgumentException ("A non-null XML name must be " + 295 "supplied to NamingUtils methods."); 296 } 297 298 return convertReservedWord( 299 convertIllegalChars(CapitalizationUtils.initialUpper(xmlName))); 300 } 301 302 314 public static String getJavaType(String xmlType) { 315 String javaType; 316 try { 317 javaType = SchemaUtils.getJavaType(xmlType); 318 return javaType; 319 } catch (UnsupportedSchemaTypeException e) { 320 } 322 323 return getJavaClassName(xmlType); 325 } 326 327 344 public static String getXMLElementNameFromAccessor(String accessor) { 345 if (accessor == null) { 346 throw new IllegalArgumentException ("A non-null accessor must " + 347 "be supplied to NamingUtils methods."); 348 } 349 350 return CapitalizationUtils.initialLower(accessor.substring(3)); 351 } 352 353 364 public static String removePackage(String className) { 365 String returnValue = className; 366 367 if (returnValue.indexOf(".") > -1) { 368 returnValue = returnValue.substring( 369 returnValue.lastIndexOf(".") + 1); 370 } 371 372 return returnValue; 373 } 374 375 386 private static String convertReservedWord(String name) { 387 if (name == null) { 388 throw new IllegalArgumentException ("A non-null name must " + 389 "be supplied to NamingUtils methods."); 390 } 391 392 if (reservedWords.get(name) == null) { 393 return name; 394 } else { 395 return new StringBuffer ().append(XML_PREFIX) 396 .append(name).toString(); 397 } 398 } 399 400 417 private static String convertIllegalChars(String name) { 418 if (name == null) { 419 throw new IllegalArgumentException ("A non-null name must " + 420 "be supplied to NamingUtils methods."); 421 } 422 423 StringBuffer verifiedName = new StringBuffer (); 424 char[] characters = name.toCharArray(); 425 426 boolean upperNext = false; 429 430 for (int i = 0; i < characters.length; i++) { 431 char c = characters[i]; 432 433 if (i == 0) { 434 if (Character.isJavaIdentifierStart(c) == false) { 435 String legalValue = 436 (String ) illegalCharMap.get(new Character (c)); 437 438 if (legalValue == null) { 439 440 throw new IllegalArgumentException ("No mapping for " + 442 "illegal character: " + c); 443 } else { 444 verifiedName.append(legalValue); 445 446 upperNext = true; 450 } 451 } else { 452 verifiedName.append(c); 453 } 454 } else { 455 if (Character.isJavaIdentifierPart(c) == false) { 456 String legalValue = 457 (String ) illegalCharMap.get(new Character (c)); 458 459 if (legalValue == null) { 460 461 throw new IllegalArgumentException ("No mapping for " + 463 "illegal character: " + c); 464 } else { 465 verifiedName.append(legalValue); 466 upperNext = true; 467 } 468 } else { 469 if (upperNext) { 470 verifiedName.append(Character.toUpperCase(c)); 471 upperNext = false; 472 } else { 473 verifiedName.append(c); 474 } 475 } 476 } 477 } 478 return verifiedName.toString(); 479 } 480 } 481 | Popular Tags |