1 56 package org.objectstyle.cayenne.util; 57 58 import java.util.StringTokenizer ; 59 60 66 public class NameConverter { 67 68 74 public static String javaToUnderscored(String name) { 75 if (name == null) { 76 return null; 77 } 78 79 char charArray[] = name.toCharArray(); 80 StringBuffer buffer = new StringBuffer (); 81 82 for (int i = 0; i < charArray.length; i++) { 83 if ((Character.isUpperCase(charArray[i])) && (i != 0)) { 84 85 char prevChar = charArray[i - 1]; 86 if ((Character.isLowerCase(prevChar))) { 87 buffer.append("_"); 88 } 89 } 90 91 buffer.append(Character.toUpperCase(charArray[i])); 92 } 93 94 return buffer.toString(); 95 } 96 97 102 public static String undescoredToJava(String name, boolean capitalize) { 103 StringTokenizer st = new StringTokenizer (name, "_"); 104 StringBuffer buf = new StringBuffer (); 105 106 boolean first = true; 107 while (st.hasMoreTokens()) { 108 String token = st.nextToken(); 109 110 int len = token.length(); 111 if (len == 0) { 112 continue; 113 } 114 115 boolean hasLowerCase = false; 117 boolean hasUpperCase = false; 118 for (int i = 0; i < len && !(hasUpperCase && hasLowerCase); i++) { 119 if (Character.isUpperCase(token.charAt(i))) { 120 hasUpperCase = true; 121 } else if (Character.isLowerCase(token.charAt(i))) { 122 hasLowerCase = true; 123 } 124 } 125 126 if (hasUpperCase && !hasLowerCase) { 128 token = token.toLowerCase(); 129 } 130 131 if (first) { 132 first = false; 134 if (capitalize) { 135 buf.append(Character.toUpperCase(token.charAt(0))); 136 } else { 137 buf.append(Character.toLowerCase(token.charAt(0))); 138 } 139 } else { 140 buf.append(Character.toUpperCase(token.charAt(0))); 141 } 142 143 if (len > 1) { 144 buf.append(token.substring(1, len)); 145 } 146 } 147 return buf.toString(); 148 } 149 } | Popular Tags |