1 16 package org.apache.cocoon.components.serializers.encoding; 17 18 import java.io.BufferedOutputStream ; 19 import java.io.File ; 20 import java.io.FileOutputStream ; 21 import java.io.IOException ; 22 import java.io.OutputStream ; 23 import java.io.PrintStream ; 24 import java.nio.charset.Charset ; 25 import java.nio.charset.CharsetEncoder ; 26 import java.util.Iterator ; 27 import java.util.Collection ; 28 29 35 public class Compiler extends CompiledCharset { 36 37 38 private String clazz = null; 39 40 41 private CharsetEncoder encoder = null; 42 43 44 private Compiler(String name, String aliases[], CharsetEncoder encoder) { 45 super(name, aliases); 46 this.clazz = "cs_" + name.replace('-', '_').toUpperCase(); 47 this.encoder = encoder; 48 this.compile(); 49 } 50 51 59 protected boolean compile(char c) { 60 return(this.encoder.canEncode((char)c)); 61 } 62 63 66 public void save() 67 throws IOException { 68 this.save(new File (System.getProperty("user.dir"))); 69 } 70 73 public void save(File directory) 74 throws IOException { 75 File file = new File (directory, this.clazz + ".java"); 76 OutputStream out = new FileOutputStream (file); 77 this.save(out); 78 out.flush(); 79 out.close(); 80 } 81 82 86 public void save(OutputStream stream) 87 throws IOException { 88 PrintStream out = new PrintStream (new BufferedOutputStream (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 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 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 162 public static Compiler process(Charset charset) 163 throws IOException { 164 CharsetEncoder encoder = charset.newEncoder(); 165 String name = charset.displayName(); 166 167 String aliases[] = new String [charset.aliases().size()]; 168 Iterator 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 (name, aliases, encoder)); 174 } 175 176 180 public static void main(String args[]) 181 throws IOException { 182 File directory = new File (System.getProperty("user.dir")); 183 if (args.length > 0) directory=new File (args[0]); 184 if (!directory.isDirectory()) { 185 throw new IOException ("Invalid output directory \"" 186 + directory.getName() + "\""); 187 } 188 Collection charsets = Charset.availableCharsets().values(); 189 Iterator 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 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 |