1 23 24 package org.enhydra.xml.xmlc.codegen; 25 26 import java.io.File ; 27 import java.util.StringTokenizer ; 28 29 32 public final class JavaLang { 33 36 public static final String INDENT_STR = " "; 37 38 41 public static final char lineSeparator; 42 43 46 static { 47 String lineSepStr = System.getProperty("line.separator"); 48 lineSeparator = ((lineSepStr != null) && (lineSepStr.length() > 0)) 49 ? lineSepStr.charAt(0) : '\n'; 50 } 51 52 55 private JavaLang() { 56 } 57 58 61 public static String [] parseClassName(String name) { 62 StringTokenizer tokens = new StringTokenizer (name, "."); 63 String [] names = new String [tokens.countTokens()]; 64 65 for (int i = 0; i < names.length; i++) { 66 names[i] = tokens.nextToken(); 67 } 68 return names; 69 } 70 71 75 public static String simpleClassName(Object obj) { 76 if (obj instanceof Class ) { 77 return simpleClassName(((Class )obj).getName()); 78 } else { 79 return simpleClassName(obj.getClass().getName()); 80 } 81 } 82 83 86 public static String simpleClassName(String className) { 87 int lastDot = className.lastIndexOf('.'); 88 if (lastDot < 0) { 89 return className; 90 } else { 91 return className.substring(lastDot+1); 92 } 93 } 94 95 99 public static String getPackageName(String className) { 100 int lastDot = className.lastIndexOf('.'); 101 if (lastDot < 0) { 102 return ""; 103 } else { 104 return className.substring(0, lastDot); 105 } 106 } 107 108 111 public static String packageNameToFileName(String packageName) { 112 return packageName.replace('.', File.separatorChar); 113 } 114 115 118 public static String packageNameToUnixFileName(String packageName) { 119 return packageName.replace('.', '/'); 120 } 121 122 126 private static void toUnicodeEncoding(char ch, StringBuffer buf) { 127 String numStr = Integer.toHexString(ch); 128 buf.append("\\u"); 129 for (int cnt = 4-numStr.length(); cnt > 0; cnt--) { 130 buf.append('0'); 131 } 132 buf.append(numStr); 133 } 134 135 142 public static String createStringConst(String text) { 143 if (text == null) { 144 return "null"; 145 } 146 147 StringBuffer str = new StringBuffer (); 148 int len = text.length (); 149 150 str.append ("\""); 151 for (int idx = 0; idx < len; idx++) { 152 char textChar = text.charAt (idx); 153 switch (textChar) { 154 case '\\': 155 case '"': 156 str.append ('\\'); 157 str.append (textChar); 158 break; 159 case '\t': 160 str.append ("\\t"); 161 break; 162 case '\n': 163 str.append ("\\n"); 164 break; 165 case '\r': 166 str.append ("\\r"); 167 break; 168 default: 169 if ((textChar > 127) || (Character.isISOControl(textChar))) { 170 toUnicodeEncoding(textChar, str); 171 } else { 172 str.append (textChar); 173 } 174 break; 175 } 176 } 177 str.append ("\""); 178 return str.toString(); 179 } 180 181 184 public static boolean legalJavaIdentifier(String str) { 185 if (str.length() == 0) { 186 return false; 187 } 188 if (!Character.isJavaIdentifierStart(str.charAt(0))) { 189 return false; 190 } 191 for (int j = 1; j < str.length(); j++) { 192 if (!Character.isJavaIdentifierPart(str.charAt(j))) { 193 return false; 194 } 195 } 196 return true; 197 } 198 199 203 public static String makeIndent(int len) { 204 StringBuffer prefix = new StringBuffer (); 205 for (int idx = 0; idx < len; idx++) { 206 prefix.append(' '); 207 } 208 return prefix.toString(); 209 } 210 211 } 212 | Popular Tags |