From d5911c9e9a5f3a19f1ae1ecc3b51676a4f9f2afa Mon Sep 17 00:00:00 2001 From: Simon Pepping Date: Thu, 3 Sep 2009 11:36:02 +0000 Subject: [PATCH] Moved the class UnicodeClasses to src/codegen/unicode, so that it is only built when the classes.xml file is regenerated. Modified the build file accordingly. Restored Java 5 features in UnicodeClasses. Created a License utility class. Added a license statement and a generated file statement to classes.xml. Made GenerateLineBreakUtils.java use the License class, and made it avoid trailing spaces in the generated file. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@810896 13f79535-47bb-0310-9956-ffa450edef68 --- build.xml | 7 +- .../fop/hyphenation/UnicodeClasses.java | 178 +++++++++++------- .../linebreak/GenerateLineBreakUtils.java | 44 ++--- .../java/org/apache/fop/util/License.java | 119 ++++++++++++ .../org/apache/fop/hyphenation/classes.xml | 24 +++ 5 files changed, 277 insertions(+), 95 deletions(-) rename src/{ => codegen/unicode}/java/org/apache/fop/hyphenation/UnicodeClasses.java (71%) create mode 100644 src/codegen/unicode/java/org/apache/fop/util/License.java diff --git a/build.xml b/build.xml index cf1900461..467e29192 100644 --- a/build.xml +++ b/build.xml @@ -436,8 +436,11 @@ list of possible build targets. - - + + + + + diff --git a/src/java/org/apache/fop/hyphenation/UnicodeClasses.java b/src/codegen/unicode/java/org/apache/fop/hyphenation/UnicodeClasses.java similarity index 71% rename from src/java/org/apache/fop/hyphenation/UnicodeClasses.java rename to src/codegen/unicode/java/org/apache/fop/hyphenation/UnicodeClasses.java index 328ba8e7a..4b3b9f734 100644 --- a/src/java/org/apache/fop/hyphenation/UnicodeClasses.java +++ b/src/codegen/unicode/java/org/apache/fop/hyphenation/UnicodeClasses.java @@ -22,13 +22,20 @@ package org.apache.fop.hyphenation; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; +import java.io.Writer; +import java.net.URI; +import java.net.URISyntaxException; import java.util.HashMap; import java.util.Map; +import org.apache.fop.util.License; + /** * Create the default classes file classes.xml, * for use in building hyphenation patterns @@ -44,23 +51,35 @@ import java.util.Map; */ public final class UnicodeClasses { - /** - * default path relative to the FOP base directory - */ - public static final String CLASSES_XML = "src/java/org/apache/fop/hyphenation/classes.xml"; - + public static String UNICODE_DIR = "http://www.unicode.org/Public/UNIDATA/"; + /** * Disallow constructor for this utility class */ private UnicodeClasses() { } + /** + * Write a comment that this is a generated file, + * and instructions on how to generate it + * @param w the writer which writes the comment + * @throws IOException if the write operation fails + */ + public static void writeGenerated(Writer w) throws IOException { + w.write("\n"); + w.write("\n"); + w.write("\n"); + w.write("\n"); + w.write("\n"); + w.write("\n"); + } + /** * Generate classes.xml from Java's compiled-in Unicode Character Database * @param hexcode whether to prefix each class with the hexcode (only for debugging purposes) * @param outfilePath output file * @throws IOException */ -/* public static void fromJava(boolean hexcode, String outfilePath) throws IOException { + public static void fromJava(boolean hexcode, String outfilePath) throws IOException { File f = new File(outfilePath); if (f.exists()) { f.delete(); @@ -71,8 +90,12 @@ public final class UnicodeClasses { int maxChar; maxChar = Character.MAX_VALUE; - ow.write("\n" + - "\n"); + ow.write("\n"); + License.writeXMLLicenseId(ow); + ow.write("\n"); + writeGenerated(ow); + ow.write("\n"); + ow.write("\n"); // loop over the first Unicode plane for (int code = Character.MIN_VALUE; code <= maxChar; ++code) { @@ -122,7 +145,7 @@ public final class UnicodeClasses { ow.flush(); ow.close(); } -*/ + /** * The column numbers in the UCD file @@ -136,10 +159,22 @@ public final class UnicodeClasses { * @param unidataPath path to the directory with UCD files * @param outfilePath output file * @throws IOException if the input files are not found + * @throws URISyntaxException + * @throws FOPException */ public static void fromUCD(boolean hexcode, String unidataPath, String outfilePath) - throws IOException { - File unidata = new File(unidataPath); + throws IOException, URISyntaxException { + URI unidata; + if (unidataPath.endsWith("/")) { + unidata = new URI(unidataPath); + } else { + unidata = new URI(unidataPath + "/"); + } + String scheme = unidata.getScheme(); + if (scheme == null || !(scheme.equals("file") || scheme.equals("http"))) { + throw new FileNotFoundException + ("URI with file or http scheme required for UNIDATA input directory"); + } File f = new File(outfilePath); if (f.exists()) { @@ -149,8 +184,14 @@ public final class UnicodeClasses { FileOutputStream fw = new FileOutputStream(f); OutputStreamWriter ow = new OutputStreamWriter(fw, "utf-8"); - File in = new File(unidata, "Blocks.txt"); - FileInputStream inis = new FileInputStream(in); + URI inuri = unidata.resolve("Blocks.txt"); + InputStream inis = null; + if (scheme.equals("file")) { + File in = new File(inuri); + inis = new FileInputStream(in); + } else if (scheme.equals("http")) { + inis = inuri.toURL().openStream(); + } InputStreamReader insr = new InputStreamReader(inis, "utf-8"); BufferedReader inbr = new BufferedReader(insr); Map blocks = new HashMap(); @@ -166,15 +207,24 @@ public final class UnicodeClasses { } inbr.close(); - in = new File(unidata, "UnicodeData.txt"); - inis = new FileInputStream(in); + inuri = unidata.resolve("UnicodeData.txt"); + if (scheme.equals("file")) { + File in = new File(inuri); + inis = new FileInputStream(in); + } else if (scheme.equals("http")) { + inis = inuri.toURL().openStream(); + } insr = new InputStreamReader(inis, "utf-8"); inbr = new BufferedReader(insr); int maxChar; maxChar = Character.MAX_VALUE; - ow.write("\n" - + "\n"); + ow.write("\n"); + License.writeXMLLicenseId(ow); + ow.write("\n"); + writeGenerated(ow); + ow.write("\n"); + ow.write("\n"); for (String line = inbr.readLine(); line != null; line = inbr.readLine()) { String[] fields = line.split(";", NUM_FIELDS); int code = Integer.parseInt(fields[UNICODE], 16); @@ -211,20 +261,16 @@ public final class UnicodeClasses { if (!"".equals(fields[SIMPLE_TITLECASE_MAPPING])) { titlecode = Integer.parseInt(fields[SIMPLE_TITLECASE_MAPPING], 16); } - StringBuffer s = new StringBuffer(); + StringBuilder s = new StringBuilder(); if (hexcode) { s.append("0x" + fields[UNICODE].replaceFirst("^0+", "").toLowerCase() + " "); } - // s.append(Character.toChars(code)); - /* This cast only works correctly when we do not exceed Character.MAX_VALUE */ - s.append((char) code); + s.append(Character.toChars(code)); if (uppercode != -1 && uppercode != code) { - // s.append(Character.toChars(uppercode)); - s.append((char) uppercode); + s.append(Character.toChars(uppercode)); } if (titlecode != -1 && titlecode != code && titlecode != uppercode) { - // s.append(Character.toChars(titlecode)); - s.append((char) titlecode); + s.append(Character.toChars(titlecode)); } ow.write(s.toString() + "\n"); } @@ -242,7 +288,7 @@ public final class UnicodeClasses { * @param outfilePath output file * @throws IOException */ -/* public static void fromTeX(boolean hexcode, String lettersPath, String outfilePath) + public static void fromTeX(boolean hexcode, String lettersPath, String outfilePath) throws IOException { File in = new File(lettersPath); @@ -258,8 +304,12 @@ public final class UnicodeClasses { InputStreamReader insr = new InputStreamReader(inis, "utf-8"); BufferedReader inbr = new BufferedReader(insr); - ow.write("\n" + - "\n"); + ow.write("\n"); + License.writeXMLLicenseId(ow); + ow.write("\n"); + writeGenerated(ow); + ow.write("\n"); + ow.write("\n"); for (String line = inbr.readLine(); line != null; line = inbr.readLine()) { String[] codes = line.split("\\s+"); if (!(codes[0].equals("\\L") || codes[0].equals("\\l"))) { @@ -282,9 +332,9 @@ public final class UnicodeClasses { if (hexcode) { s.append("0x" + codes[1].replaceFirst("^0+", "").toLowerCase() + " "); } - s.append((char) Integer.parseInt(codes[1], 16)); + s.append(Character.toChars(Integer.parseInt(codes[1], 16))); if (codes[0].equals("\\L")) { - s.append((char) Integer.parseInt(codes[2], 16)); + s.append(Character.toChars(Integer.parseInt(codes[2], 16))); } ow.write(s.toString() + "\n"); } @@ -294,58 +344,54 @@ public final class UnicodeClasses { ow.close(); inbr.close(); } -*/ + /** - * @param args [--hexcode] [--java|--ucd|--tex] [--out outfile] infile + * @param args [--hexcode] [--java|--ucd|--tex] outfile [infile] * @throws IOException if the input file cannot be found + * @throws URISyntaxException if the input URI is incorrect */ - public static void main(String[] args) throws IOException { - String type = "ucd", prefix = "--", infile = null, outfile = CLASSES_XML; + public static void main(String[] args) throws IOException, URISyntaxException { + String type = "ucd", prefix = "--", infile = null, outfile = null; boolean hexcode = false; - for (int i = 0; i < args.length; ++i) { - if (args[i].startsWith(prefix)) { - String option = args[i].substring(prefix.length()); - if (option.equals("java") || option.equals("ucd") || option.equals("tex")) { - type = option; - } else if (option.equals("hexcode")) { - hexcode = true; - } else if (option.equals("out")) { - outfile = args[++i]; - } else { - System.err.println("Unknown option: " + option); - System.exit(1); - } + int i; + for (i = 0; i < args.length && args[i].startsWith(prefix); ++i) { + String option = args[i].substring(prefix.length()); + if (option.equals("java") || option.equals("ucd") || option.equals("tex")) { + type = option; + } else if (option.equals("hexcode")) { + hexcode = true; } else { - if (infile != null) { - System.err.println("Only one non-option argument can be given, for infile"); - System.exit(1); - } - infile = args[i]; + System.err.println("Unknown option: " + option); + System.exit(1); } } + if (i < args.length) { + outfile = args[i]; + } else { + System.err.println("Output file is required; aborting"); + System.exit(1); + } + if (++i < args.length) { + infile = args[i]; + } - if (type.equals("java")) { - if (infile != null) { + if (type.equals("java") && infile != null) { System.err.println("Type java does not allow an infile"); System.exit(1); - } - } else { - if (infile == null) { - System.err.println("Types ucd and tex require an infile"); + } else if (type.equals("ucd") && infile == null) { + infile = UNICODE_DIR; + } else if (type.equals("tex") && infile == null) { + System.err.println("Type tex requires an input file"); System.exit(1); - } } -/* if (type.equals("java")) { + if (type.equals("java")) { fromJava(hexcode, outfile); - } else - */ - if (type.equals("ucd")) { + } else if (type.equals("ucd")) { fromUCD(hexcode, infile, outfile); -/* } else if (type.equals("tex")) { + } else if (type.equals("tex")) { fromTeX(hexcode, infile, outfile); -*/ - } else { + } else { System.err.println("Unknown type: " + type + ", nothing done"); System.exit(1); } diff --git a/src/codegen/unicode/java/org/apache/fop/text/linebreak/GenerateLineBreakUtils.java b/src/codegen/unicode/java/org/apache/fop/text/linebreak/GenerateLineBreakUtils.java index cb0288334..f96b7484f 100644 --- a/src/codegen/unicode/java/org/apache/fop/text/linebreak/GenerateLineBreakUtils.java +++ b/src/codegen/unicode/java/org/apache/fop/text/linebreak/GenerateLineBreakUtils.java @@ -31,6 +31,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.apache.fop.util.License; + /** *

Utility for generating a Java class representing line break properties * from the Unicode property files.

@@ -226,31 +228,14 @@ public class GenerateLineBreakUtils { int idx = 0; StringBuffer doStaticLinkCode = new StringBuffer(); PrintWriter out = new PrintWriter(new FileWriter(outFileName)); - out.println("/*"); - out.println(" * Licensed to the Apache Software Foundation (ASF) under one or more"); - out.println(" * contributor license agreements. See the NOTICE file distributed with"); - out.println(" * this work for additional information regarding copyright ownership."); - out.println(" * The ASF licenses this file to You under the Apache License, Version 2.0"); - out.println(" * (the \"License\"); you may not use this file except in compliance with"); - out.println(" * the License. You may obtain a copy of the License at"); - out.println(" * "); - out.println(" * http://www.apache.org/licenses/LICENSE-2.0"); - out.println(" * "); - out.println(" * Unless required by applicable law or agreed to in writing, software"); - out.println(" * distributed under the License is distributed on an \"AS IS\" BASIS,"); - out.println(" * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied."); - out.println(" * See the License for the specific language governing permissions and"); - out.println(" * limitations under the License."); - out.println(" */"); - out.println(); - out.println("/* $Id$ */"); + License.writeJavaLicenseId(out); out.println(); out.println("package org.apache.fop.text.linebreak;"); out.println(); - out.println("/* "); - out.println(" * !!! THIS IS A GENERATED FILE !!! "); + out.println("/*"); + out.println(" * !!! THIS IS A GENERATED FILE !!!"); out.println(" * If updates to the source are needed, then:"); - out.println(" * - apply the necessary modifications to "); + out.println(" * - apply the necessary modifications to"); out.println(" * 'src/codegen/unicode/java/org/apache/fop/text/linebreak/GenerateLineBreakUtils.java'"); out.println(" * - run 'ant codegen-unicode', which will generate a new LineBreakUtils.java"); out.println(" * in 'src/java/org/apache/fop/text/linebreak'"); @@ -276,7 +261,7 @@ public class GenerateLineBreakUtils { boolean printComma = false; for (int i = 1; i <= lineBreakPropertyValueCount; i++) { if (printComma) { - out.println(", "); + out.println(","); } else { printComma = true; } @@ -376,8 +361,13 @@ public class GenerateLineBreakUtils { for (int i = 0; i < lineBreakPropertyShortNames.size(); i++) { name = (String)lineBreakPropertyShortNames.get(i); if (printComma) { - out.print(", "); - lineLength++; + if (lineLength <= MAX_LINE_LENGTH - 2) { + out.print(", "); + } else { + out.print(","); + } + // count the space anyway to force a linebreak if the comma causes lineLength == MAX_LINE_LENGTH + lineLength += 2; } else { printComma = true; } @@ -418,7 +408,7 @@ public class GenerateLineBreakUtils { out.println("};"); out.println(); out.println(" /**"); - out.println(" * Return the short name for the linebreak property corresponding "); + out.println(" * Return the short name for the linebreak property corresponding"); out.println(" * to the given symbolic constant."); out.println(" *"); out.println(" * @param i the numeric value of the linebreak property"); @@ -433,7 +423,7 @@ public class GenerateLineBreakUtils { out.println(" }"); out.println(); out.println(" /**"); - out.println(" * Return the long name for the linebreak property corresponding "); + out.println(" * Return the long name for the linebreak property corresponding"); out.println(" * to the given symbolic constant."); out.println(" *"); out.println(" * @param i the numeric value of the linebreak property"); @@ -458,7 +448,7 @@ public class GenerateLineBreakUtils { out.println(" }"); out.println(); out.println(" /**"); - out.println(" * Return the break class constant for the given pair of linebreak "); + out.println(" * Return the break class constant for the given pair of linebreak"); out.println(" * property constants."); out.println(" *"); out.println(" * @param lineBreakPropertyBefore the linebreak property for the first character"); diff --git a/src/codegen/unicode/java/org/apache/fop/util/License.java b/src/codegen/unicode/java/org/apache/fop/util/License.java new file mode 100644 index 000000000..b6e3db8a4 --- /dev/null +++ b/src/codegen/unicode/java/org/apache/fop/util/License.java @@ -0,0 +1,119 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.util; + +import java.io.IOException; +import java.io.StringWriter; +import java.io.Writer; + + +/** + * Write the Apache license text in various forms + */ +public final class License { + + /** + * The Apache license text as a string array + */ + public static final String[] license + = {"Licensed to the Apache Software Foundation (ASF) under one or more", + "contributor license agreements. See the NOTICE file distributed with", + "this work for additional information regarding copyright ownership.", + "The ASF licenses this file to You under the Apache License, Version 2.0", + "(the \"License\"); you may not use this file except in compliance with", + "the License. You may obtain a copy of the License at", + "", + " http://www.apache.org/licenses/LICENSE-2.0", + "", + "Unless required by applicable law or agreed to in writing, software", + "distributed under the License is distributed on an \"AS IS\" BASIS,", + "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.", + "See the License for the specific language governing permissions and", + "limitations under the License." + }; + + /** + * The subversion Id keyword line + */ + public static final String id = "$Id$"; + + /** + * Calculate the maximum line length in the Apache license text + * for use in formatting + */ + private static int MAX_LENGTH; + static { + int j = 0; + for (int i = 0; i < license.length; ++i) { + if (j < license[i].length()) { + j = license[i].length(); + } + } + MAX_LENGTH = j; + } + + /** + * Write the Apache license text as commented lines for a Java file + * @param w the writer which writes the comment + * @throws IOException if the write operation fails + */ + public static void writeJavaLicenseId(Writer w) throws IOException { + w.write("/*\n"); + for (int i = 0; i < license.length; ++i) { + if (license[i].equals("")) { + w.write(" *\n"); + } else { + w.write(" * " + license[i] + "\n"); + } + } + w.write(" */\n"); + w.write("\n"); + w.write("/* " + id + " */\n"); + } + + /** + * Write the Apache license text as commented lines for an XML file + * @param w the writer which writes the comment + * @throws IOException if the write operation fails + */ + public static void writeXMLLicenseId(Writer w) throws IOException { + for (int i = 0; i < license.length; ++i) { + w.write(String.format("\n", new Object[] {license[i]})); + } + w.write("\n"); + w.write("\n"); + } + + /** + * For testing purposes + * @param args optional, --java or --xml + * @throws IOException if the write operation fails + */ + public static void main(String[] args) throws IOException { + StringWriter w = new StringWriter(); + if (args.length == 0 || args[0].equals("--java")) { + writeJavaLicenseId(w); + } else if (args[0].equals("--xml")) { + writeXMLLicenseId(w); + } + System.out.println(w.toString()); + } + +} diff --git a/src/java/org/apache/fop/hyphenation/classes.xml b/src/java/org/apache/fop/hyphenation/classes.xml index bf5c29758..056a533a8 100644 --- a/src/java/org/apache/fop/hyphenation/classes.xml +++ b/src/java/org/apache/fop/hyphenation/classes.xml @@ -1,4 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + + aA bB -- 2.39.5