1 package com.thaiopensource.datatype.xsd.regex.jdk1_4.gen; 2 3 import com.thaiopensource.xml.util.Naming; 4 5 import java.io.BufferedWriter ; 6 import java.io.File ; 7 import java.io.FileOutputStream ; 8 import java.io.IOException ; 9 import java.io.OutputStream ; 10 import java.io.OutputStreamWriter ; 11 import java.io.Writer ; 12 import java.util.List ; 13 import java.util.Vector ; 14 15 public class NamingExceptionsGen { 16 static public void main(String [] args) throws IOException { 17 if (args.length != 2) { 18 System.err.println("Usage: " + NamingExceptionsGen.class.toString() + " className srcDir"); 19 System.exit(1); 20 } 21 String className = args[0]; 22 String srcDir = args[1]; 23 int lastDot = className.lastIndexOf('.'); 24 String pkg; 25 if (lastDot < 0) 26 pkg = null; 27 else { 28 pkg = className.substring(0, lastDot); 29 className = className.substring(lastDot + 1); 30 srcDir = srcDir + File.separator + pkg.replace('.', File.separatorChar); 31 } 32 String srcFile = srcDir + File.separator + className + ".java"; 33 OutputStream stm = new FileOutputStream (srcFile); 34 Writer w = new BufferedWriter (new OutputStreamWriter (stm)); 35 String lineSep = System.getProperty("line.separator"); 36 w.write("// This file was automatically generated by "); 37 w.write(NamingExceptionsGen.class.getName()); 38 w.write(lineSep); 39 if (pkg != null) 40 w.write("package " + pkg + ";" + lineSep + lineSep); 41 w.write("class " + className + " {" + lineSep); 42 gen(true, w, lineSep); 43 gen(false, w, lineSep); 44 w.write("}" + lineSep); 45 w.close(); 46 } 47 48 static private void gen(boolean isStart, Writer w, String lineSep) throws IOException { 49 char[] buf = new char[1]; 50 boolean excluding = false; 51 char excludeMin = 0; 52 char excludeMax = 0; 53 List includeList = new Vector (); 54 List excludeRangeList = new Vector (); 55 for (int i = 0; i < 65536; i++) { 56 char ch = (char)i; 57 buf[0] = ch; 58 String s = new String (buf); 59 boolean isName = isStart ? Naming.isName(s) : Naming.isNmtoken(s); 60 if (isName && excluding) { 61 excludeRangeList.add(new Character (excludeMin)); 62 excludeRangeList.add(new Character (excludeMax)); 63 excluding = false; 64 } 65 if (isName != isApproxName(ch, isStart)) { 66 if (isName) 67 includeList.add(new Character (ch)); 68 else { 69 if (!excluding) { 70 excluding = true; 71 excludeMin = ch; 72 } 73 excludeMax = ch; 74 } 75 } 76 } 77 if (excluding) { 78 excludeRangeList.add(new Character (excludeMin)); 79 excludeRangeList.add(new Character (excludeMax)); 80 } 81 String prefix = isStart ? "NMSTRT" : "NMCHAR"; 82 genList(prefix + "_INCLUDES", includeList, w, lineSep); 83 genList(prefix + "_EXCLUDE_RANGES", excludeRangeList, w, lineSep); 84 w.write(INDENT); 85 w.write("static final String "); 86 w.write(prefix); 87 w.write("_CATEGORIES = \""); 88 w.write(isStart ? NMSTRT_CATEGORIES : NMCHAR_CATEGORIES); 89 w.write("\";"); 90 w.write(lineSep); 91 } 92 93 static private final int CHARS_PER_LINE = 10; 94 static private final String INDENT = " "; 95 96 static private void genList(String varName, List includeList, Writer w, String lineSep) throws IOException { 97 w.write(INDENT); 98 w.write("static final String "); 99 w.write(varName); 100 w.write(" ="); 101 w.write(lineSep); 102 w.write(INDENT); 103 w.write(INDENT); 104 w.write('"'); 105 for (int i = 0, len = includeList.size(); i < len; i++) { 106 w.write("\\u"); 107 w.write(hex(((Character )includeList.get(i)).charValue())); 108 if (i % CHARS_PER_LINE == CHARS_PER_LINE - 1 && i + 1 != len) { 109 w.write("\" +"); 110 w.write(lineSep); 111 w.write(INDENT); 112 w.write(INDENT); 113 w.write('"'); 114 } 115 } 116 w.write("\";"); 117 w.write(lineSep); 118 } 119 120 static private final String HEX_DIGITS = "0123456789ABCDEF"; 121 122 static String hex(char c) { 123 char[] buf = new char[4]; 124 buf[0] = HEX_DIGITS.charAt((c >> 12) & 0xF); 125 buf[1] = HEX_DIGITS.charAt((c >> 8) & 0xF); 126 buf[2] = HEX_DIGITS.charAt((c >> 4) & 0xF); 127 buf[3] = HEX_DIGITS.charAt(c & 0xF); 128 return new String (buf); 129 } 130 131 static private final String NMSTRT_CATEGORIES = "LlLuLoLtNl"; 132 static private final String NMCHAR_CATEGORIES = NMSTRT_CATEGORIES + "McMeMnLmNd"; 133 134 static private boolean isApproxName(char c, boolean isStart) { 135 switch (Character.getType(c)) { 136 case Character.LOWERCASE_LETTER: case Character.UPPERCASE_LETTER: case Character.OTHER_LETTER: case Character.TITLECASE_LETTER: case Character.LETTER_NUMBER: return true; 142 case Character.COMBINING_SPACING_MARK: case Character.ENCLOSING_MARK: case Character.NON_SPACING_MARK: case Character.MODIFIER_LETTER: case Character.DECIMAL_DIGIT_NUMBER: return !isStart; 148 } 149 return false; 150 } 151 } 152 153 | Popular Tags |