KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thaiopensource > datatype > xsd > regex > jdk1_4 > gen > NamingExceptionsGen


1 package com.thaiopensource.datatype.xsd.regex.jdk1_4.gen;
2
3 import com.thaiopensource.xml.util.Naming;
4
5 import java.io.BufferedWriter JavaDoc;
6 import java.io.File JavaDoc;
7 import java.io.FileOutputStream JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.io.OutputStream JavaDoc;
10 import java.io.OutputStreamWriter JavaDoc;
11 import java.io.Writer JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Vector JavaDoc;
14
15 public class NamingExceptionsGen {
16   static public void main(String JavaDoc[] args) throws IOException JavaDoc {
17     if (args.length != 2) {
18       System.err.println("Usage: " + NamingExceptionsGen.class.toString() + " className srcDir");
19       System.exit(1);
20     }
21     String JavaDoc className = args[0];
22     String JavaDoc srcDir = args[1];
23     int lastDot = className.lastIndexOf('.');
24     String JavaDoc 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 JavaDoc srcFile = srcDir + File.separator + className + ".java";
33     OutputStream JavaDoc stm = new FileOutputStream JavaDoc(srcFile);
34     Writer JavaDoc w = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(stm));
35     String JavaDoc 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 JavaDoc w, String JavaDoc lineSep) throws IOException JavaDoc {
49     char[] buf = new char[1];
50     boolean excluding = false;
51     char excludeMin = 0;
52     char excludeMax = 0;
53     List JavaDoc includeList = new Vector JavaDoc();
54     List JavaDoc excludeRangeList = new Vector JavaDoc();
55     for (int i = 0; i < 65536; i++) {
56       char ch = (char)i;
57       buf[0] = ch;
58       String JavaDoc s = new String JavaDoc(buf);
59       boolean isName = isStart ? Naming.isName(s) : Naming.isNmtoken(s);
60       if (isName && excluding) {
61         excludeRangeList.add(new Character JavaDoc(excludeMin));
62         excludeRangeList.add(new Character JavaDoc(excludeMax));
63         excluding = false;
64       }
65       if (isName != isApproxName(ch, isStart)) {
66         if (isName)
67           includeList.add(new Character JavaDoc(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 JavaDoc(excludeMin));
79       excludeRangeList.add(new Character JavaDoc(excludeMax));
80     }
81     String JavaDoc 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 JavaDoc INDENT = " ";
95
96   static private void genList(String JavaDoc varName, List JavaDoc includeList, Writer JavaDoc w, String JavaDoc lineSep) throws IOException JavaDoc {
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 JavaDoc)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 JavaDoc HEX_DIGITS = "0123456789ABCDEF";
121
122   static String JavaDoc 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 JavaDoc(buf);
129   }
130
131   static private final String JavaDoc NMSTRT_CATEGORIES = "LlLuLoLtNl";
132   static private final String JavaDoc 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: // Ll
137
case Character.UPPERCASE_LETTER: // Lu
138
case Character.OTHER_LETTER: // Lo
139
case Character.TITLECASE_LETTER: // Lt
140
case Character.LETTER_NUMBER: // Nl
141
return true;
142     case Character.COMBINING_SPACING_MARK: // Mc
143
case Character.ENCLOSING_MARK: // Me
144
case Character.NON_SPACING_MARK: // Mn
145
case Character.MODIFIER_LETTER: // Lm
146
case Character.DECIMAL_DIGIT_NUMBER: // Nd
147
return !isStart;
148     }
149     return false;
150   }
151 }
152
153
Popular Tags