1 7 package com.bull.eclipse.jonas.utils; 8 9 15 public class DBToJavaNameTransformUtil { 16 public static String dbNameToVariableName(String s) { 17 if ("".equals(s)) { 18 return s; 19 } 20 StringBuffer result = new StringBuffer (); 21 boolean capitalize = true; 22 boolean lastCapital = false; 23 boolean lastDecapitalized = false; 24 for (int i = 0; i < s.length(); i++) { 25 String c = s.substring(i, i + 1); 26 if ("_".equals(c) || " ".equals(c)) { 27 capitalize = true; 28 continue; 29 } 30 31 if (c.toUpperCase().equals(c)) { 32 if (lastDecapitalized && !lastCapital) { 33 capitalize = true; 34 } 35 lastCapital = true; 36 } 37 else { 38 lastCapital = false; 39 } 40 41 if (capitalize) { 42 result.append(c.toUpperCase()); 43 capitalize = false; 44 } 45 else { 46 result.append(c.toLowerCase()); 47 lastDecapitalized = true; 48 } 49 50 } 51 String r = result.toString(); 52 return r; 53 } 54 55 56 public static String singularise(String name) { 57 String result = name; 58 if (seemsPluralised(name)) { 59 String lower = name.toLowerCase(); 60 if (lower.endsWith("ies")) { 61 result = name.substring(0, name.length() - 3) + "y"; 63 } else if (lower.endsWith("ches") || lower.endsWith("ses")) { 64 result = name.substring(0, name.length() - 2); 66 } else if (lower.endsWith("s")) { 67 result = name.substring(0, name.length() - 1); 69 } 70 } 71 return result; 72 } 73 74 private static boolean seemsPluralised(String name) { 75 name = name.toLowerCase(); 76 boolean pluralised = false; 77 pluralised |= name.endsWith("es"); 78 pluralised |= name.endsWith("s"); 79 pluralised &= !(name.endsWith("ss") || name.endsWith("us")); 80 return pluralised; 81 } 82 83 public static String capitalise(String s) { 84 if (s.equals("")) { 85 return ""; 86 } 87 if (s.length() == 1) { 88 return s.toUpperCase(); 89 } 90 else { 91 String caps = s.substring(0, 1).toUpperCase(); 92 String rest = s.substring(1); 93 return caps + rest; 94 } 95 } 96 97 98 } 99 | Popular Tags |