001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.serializers.encoding;
018:
019: import java.io.BufferedOutputStream;
020: import java.io.File;
021: import java.io.FileOutputStream;
022: import java.io.IOException;
023: import java.io.OutputStream;
024: import java.io.PrintStream;
025: import java.nio.charset.Charset;
026: import java.nio.charset.CharsetEncoder;
027: import java.util.Iterator;
028: import java.util.Collection;
029:
030: /**
031: *
032: *
033: * @author <a href="mailto:pier@apache.org">Pier Fumagalli</a>, February 2003
034: * @version CVS $Id: Compiler.java 433543 2006-08-22 06:22:54Z crossley $
035: */
036: public class Compiler extends CompiledCharset {
037:
038: /** The class name to be generated. */
039: private String clazz = null;
040:
041: /** The <code>CharsetEncoder</code> instance. */
042: private CharsetEncoder encoder = null;
043:
044: /** Create a new instance of this <code>Compiler</code>. */
045: private Compiler(String name, String aliases[],
046: CharsetEncoder encoder) {
047: super (name, aliases);
048: this .clazz = "cs_" + name.replace('-', '_').toUpperCase();
049: this .encoder = encoder;
050: this .compile();
051: }
052:
053: /**
054: * Return true or false wether this encoding can encode the specified
055: * character or not.
056: * <p>
057: * This method is equivalent to the <code>allows(...)</code> method, but
058: * it will be called upon construction of the encoding table.
059: * </p>
060: */
061: protected boolean compile(char c) {
062: return (this .encoder.canEncode((char) c));
063: }
064:
065: /**
066: * Save this <code>Charset</code> into a Java source file.
067: */
068: public void save() throws IOException {
069: this .save(new File(System.getProperty("user.dir")));
070: }
071:
072: /**
073: * Save this <code>Charset</code> into a Java source file.
074: */
075: public void save(File directory) throws IOException {
076: File file = new File(directory, this .clazz + ".java");
077: OutputStream out = new FileOutputStream(file);
078: this .save(out);
079: out.flush();
080: out.close();
081: }
082:
083: /**
084: * Save this <code>Charset</code> as a Java source file to the specified
085: * <code>OutputStream</code>.
086: */
087: public void save(OutputStream stream) throws IOException {
088: PrintStream out = new PrintStream(new BufferedOutputStream(
089: stream));
090:
091: out.println("/*");
092: out
093: .println(" * Copyright 1999-2004 The Apache Software Foundation.");
094: out.println(" *");
095: out
096: .println(" * Licensed under the Apache License, Version 2.0 (the \"License\");");
097: out
098: .println(" * you may not use this file except in compliance with the License.");
099: out.println(" * You may obtain a copy of the License at");
100: out.println(" *");
101: out
102: .println(" * http://www.apache.org/licenses/LICENSE-2.0");
103: out.println(" *");
104: out
105: .println(" * Unless required by applicable law or agreed to in writing, software");
106: out
107: .println(" * distributed under the License is distributed on an \"AS IS\" BASIS,");
108: out
109: .println(" * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.");
110: out
111: .println(" * See the License for the specific language governing permissions and");
112: out.println(" * limitations under the License.");
113: out.println(" */");
114: out.println("/* Generated by " + this .getClass().getName()
115: + " */");
116: out.println();
117: out
118: .println("package org.apache.cocoon.components.serializers.encoding;");
119: out.println();
120: out.println("/**");
121: out.println(" * The <b>" + this .getName()
122: + "</b> character set encoding representation.");
123: out.println(" *");
124: out.println(" * @author Generated by <code>"
125: + this .getClass().getName() + "</code>");
126: out.println(" */");
127: out.println("class " + this .clazz
128: + " extends CompiledCharset {");
129: out.println();
130: out.println(" /** The name of this charset (<b>"
131: + this .getName() + "</b>). */");
132: out.println(" public static final String CS_NAME = \""
133: + this .getName() + "\";");
134: out.println();
135: out
136: .println(" /** The array of alias names of this charset. */");
137: out.println(" public static final String CS_ALIASES[] = {");
138: String aliases[] = this .getAliases();
139: for (int x = 0; x < aliases.length; x++)
140: out.println(" \"" + aliases[x] + "\",");
141: out.println(" };");
142: out.println();
143: out
144: .println(" /** The array all characters encoded by this encoding. */");
145: out.print(" public static final byte CS_ENCODING[] = {");
146: for (int x = 0; x < this .encoding.length; x++) {
147: if ((x & 0x0F) == 0) {
148: out.println();
149: out.print(" ");
150: }
151: String value = Integer.toString(this .encoding[x]);
152: value = " ".substring(value.length()) + value;
153: out.print(value);
154: if ((x + 1) != this .encoding.length)
155: out.print(",");
156: }
157: out.println();
158: out.println(" };");
159: out.println();
160: out.println(" /**");
161: out.println(" * Create a new instance of the <b>"
162: + this .getName() + "</b> caracter");
163: out.println(" * encoding as a <code>Charset</code>.");
164: out.println(" */");
165: out.println(" public " + this .clazz + "() {");
166: out.println(" super(CS_NAME, CS_ALIASES, CS_ENCODING);");
167: out.println(" }");
168: out.println();
169: out.println(" /**");
170: out.println(" * Operation not supported.");
171: out.println(" */");
172: out.println(" public boolean compile(char c) {");
173: out
174: .println(" throw new UnsupportedOperationException();");
175: out.println(" }");
176: out.println();
177: out.println("}");
178: out.flush();
179: }
180:
181: /**
182: * Process a NIO <code>Charset</code> producing a java source file.
183: */
184: public static Compiler process(Charset charset) throws IOException {
185: CharsetEncoder encoder = charset.newEncoder();
186: String name = charset.displayName();
187:
188: String aliases[] = new String[charset.aliases().size()];
189: Iterator iterator = charset.aliases().iterator();
190: for (int k = 0; k < aliases.length; k++) {
191: aliases[k] = iterator.next().toString();
192: }
193:
194: return (new Compiler(name, aliases, encoder));
195: }
196:
197: /**
198: * Compile all <code>java.nio.charset.Charset</code> classes and generate
199: * the main holding encodings table.
200: */
201: public static void main(String args[]) throws IOException {
202: File directory = new File(System.getProperty("user.dir"));
203: if (args.length > 0)
204: directory = new File(args[0]);
205: if (!directory.isDirectory()) {
206: throw new IOException("Invalid output directory \""
207: + directory.getName() + "\"");
208: }
209: Collection charsets = Charset.availableCharsets().values();
210: Iterator iterator = charsets.iterator();
211: int pos = 0;
212: int len = charsets.size();
213:
214: while (iterator.hasNext()) {
215: Charset charset = (Charset) iterator.next();
216: try {
217: Compiler compiler = process(charset);
218: compiler.save(directory);
219: System.out
220: .println("Generating \"" + compiler.clazz
221: + ".java\" " + "for \""
222: + compiler.getName() + "\" charset ("
223: + (++pos) + " of " + len + ")");
224: } catch (Exception exception) {
225: System.err.println("Error generating charset \""
226: + charset + "\"");
227: System.err.print(exception.getClass().getName() + ": ");
228: System.err.println(exception.getMessage());
229: }
230: }
231: }
232: }
|