KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > serializers > encoding > Compiler


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.serializers.encoding;
17
18 import java.io.BufferedOutputStream JavaDoc;
19 import java.io.File JavaDoc;
20 import java.io.FileOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.io.PrintStream JavaDoc;
24 import java.nio.charset.Charset JavaDoc;
25 import java.nio.charset.CharsetEncoder JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Collection JavaDoc;
28
29 /**
30  *
31  *
32  * @author <a HREF="mailto:pier@apache.org">Pier Fumagalli</a>, February 2003
33  * @version CVS $Id: Compiler.java 30932 2004-07-29 17:35:38Z vgritsenko $
34  */

35 public class Compiler extends CompiledCharset {
36
37     /** The class name to be generated. */
38     private String JavaDoc clazz = null;
39
40     /** The <code>CharsetEncoder</code> instance. */
41     private CharsetEncoder JavaDoc encoder = null;
42
43     /** Create a new instance of this <code>Compiler</code>. */
44     private Compiler(String JavaDoc name, String JavaDoc aliases[], CharsetEncoder JavaDoc encoder) {
45         super(name, aliases);
46         this.clazz = "cs_" + name.replace('-', '_').toUpperCase();
47         this.encoder = encoder;
48         this.compile();
49     }
50
51     /**
52      * Return true or false wether this encoding can encode the specified
53      * character or not.
54      * <p>
55      * This method is equivalent to the <code>allows(...)</code> method, but
56      * it will be called upon construction of the encoding table.
57      * </p>
58      */

59     protected boolean compile(char c) {
60         return(this.encoder.canEncode((char)c));
61     }
62
63     /**
64      * Save this <code>Charset</code> into a Java source file.
65      */

66     public void save()
67     throws IOException JavaDoc {
68         this.save(new File JavaDoc(System.getProperty("user.dir")));
69     }
70    /**
71      * Save this <code>Charset</code> into a Java source file.
72      */

73     public void save(File JavaDoc directory)
74     throws IOException JavaDoc {
75         File JavaDoc file = new File JavaDoc(directory, this.clazz + ".java");
76         OutputStream JavaDoc out = new FileOutputStream JavaDoc(file);
77         this.save(out);
78         out.flush();
79         out.close();
80     }
81
82     /**
83      * Save this <code>Charset</code> as a Java source file to the specified
84      * <code>OutputStream</code>.
85      */

86     public void save(OutputStream JavaDoc stream)
87     throws IOException JavaDoc {
88         PrintStream JavaDoc out = new PrintStream JavaDoc(new BufferedOutputStream JavaDoc(stream));
89
90         out.println("/*");
91         out.println(" * Copyright 1999-2004 The Apache Software Foundation.");
92         out.println(" *");
93         out.println(" * Licensed under the Apache License, Version 2.0 (the \"License\");");
94         out.println(" * you may not use this file except in compliance with the License.");
95         out.println(" * You may obtain a copy of the License at");
96         out.println(" *");
97         out.println(" * http://www.apache.org/licenses/LICENSE-2.0");
98         out.println(" *");
99         out.println(" * Unless required by applicable law or agreed to in writing, software");
100         out.println(" * distributed under the License is distributed on an \"AS IS\" BASIS,");
101         out.println(" * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.");
102         out.println(" * See the License for the specific language governing permissions and");
103         out.println(" * limitations under the License.");
104         out.println(" */");
105         out.println("/* Generated by " + this.getClass().getName() + " */");
106         out.println();
107         out.println("package org.apache.cocoon.components.serializers.encoding;");
108         out.println();
109         out.println("/**");
110         out.println(" * The <b>" + this.getName() + "</b> character set encoding representation.");
111         out.println(" *");
112         out.println(" * @author Generated by <code>" + this.getClass().getName() + "</code>");
113         out.println(" */");
114         out.println("class " + this.clazz + " extends CompiledCharset {");
115         out.println();
116         out.println(" /** The name of this charset (<b>" + this.getName() + "</b>). */");
117         out.println(" public static final String CS_NAME = \"" + this.getName() + "\";");
118         out.println();
119         out.println(" /** The array of alias names of this charset. */");
120         out.println(" public static final String CS_ALIASES[] = {");
121         String JavaDoc aliases[] = this.getAliases();
122         for (int x = 0; x < aliases.length; x++) out.println(" \"" + aliases[x] + "\",");
123         out.println(" };");
124         out.println();
125         out.println(" /** The array all characters encoded by this encoding. */");
126         out.print(" public static final byte CS_ENCODING[] = {");
127         for (int x = 0; x < this.encoding.length; x++) {
128             if ((x & 0x0F) == 0) {
129                 out.println();
130                 out.print(" ");
131             }
132             String JavaDoc value = Integer.toString(this.encoding[x]);
133             value = " ".substring(value.length()) + value;
134             out.print(value);
135             if ((x + 1) != this.encoding.length) out.print(",");
136         }
137         out.println();
138         out.println(" };");
139         out.println();
140         out.println(" /**");
141         out.println(" * Create a new instance of the <b>" + this.getName() + "</b> caracter");
142         out.println(" * encoding as a <code>Charset</code>.");
143         out.println(" */");
144         out.println(" public " + this.clazz + "() {");
145         out.println(" super(CS_NAME, CS_ALIASES, CS_ENCODING);");
146         out.println(" }");
147         out.println();
148         out.println(" /**");
149         out.println(" * Operation not supported.");
150         out.println(" */");
151         out.println(" public boolean compile(char c) {");
152         out.println(" throw new UnsupportedOperationException();");
153         out.println(" }");
154         out.println();
155         out.println("}");
156         out.flush();
157     }
158
159     /**
160      * Process a NIO <code>Charset</code> producing a java source file.
161      */

162     public static Compiler JavaDoc process(Charset charset)
163     throws IOException JavaDoc {
164         CharsetEncoder JavaDoc encoder = charset.newEncoder();
165         String JavaDoc name = charset.displayName();
166
167         String JavaDoc aliases[] = new String JavaDoc[charset.aliases().size()];
168         Iterator JavaDoc iterator = charset.aliases().iterator();
169         for (int k = 0; k < aliases.length; k++) {
170             aliases[k] = iterator.next().toString();
171         }
172
173         return(new Compiler JavaDoc(name, aliases, encoder));
174     }
175
176     /**
177      * Compile all <code>java.nio.charset.Charset</code> classes and generate
178      * the main holding encodings table.
179      */

180     public static void main(String JavaDoc args[])
181     throws IOException JavaDoc {
182         File JavaDoc directory = new File JavaDoc(System.getProperty("user.dir"));
183         if (args.length > 0) directory=new File JavaDoc(args[0]);
184         if (!directory.isDirectory()) {
185             throw new IOException JavaDoc("Invalid output directory \""
186                                   + directory.getName() + "\"");
187         }
188         Collection JavaDoc charsets = Charset.availableCharsets().values();
189         Iterator JavaDoc iterator = charsets.iterator();
190         int pos = 0;
191         int len = charsets.size();
192
193         while (iterator.hasNext()) {
194             Charset charset = (Charset)iterator.next();
195             Compiler JavaDoc compiler = process(charset);
196             compiler.save(directory);
197             System.out.println("Generating \"" + compiler.clazz + ".java\" "
198                                + "for \"" + compiler.getName() + "\" charset ("
199                                + (++pos) + " of " + len + ")");
200         }
201     }
202 }
203
Popular Tags