From: Jeremias Maerki Date: Wed, 25 Jan 2006 21:16:26 +0000 (+0000) Subject: Bugzilla #37506: X-Git-Tag: fop-0_92-beta~172 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=5c9f9e96d0d283994ceefe4e8ca8aaa6e711a88f;p=xmlgraphics-fop.git Bugzilla #37506: Extracted a base class for both PFMReader and TTFReader to remove duplicate code. Submitted by: Kev Jackson (Patch applied with changes: escape method removed, tabs removed, javadocs added) git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@372317 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/fop/fonts/apps/AbstractFontReader.java b/src/java/org/apache/fop/fonts/apps/AbstractFontReader.java new file mode 100644 index 000000000..35e103aad --- /dev/null +++ b/src/java/org/apache/fop/fonts/apps/AbstractFontReader.java @@ -0,0 +1,105 @@ +/* + * Copyright 1999-2005 The Apache Software Foundation. + * + * Licensed 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.fonts.apps; + +import java.util.List; +import java.util.Map; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.fop.util.CommandLineLogger; + +/** + * Abstract base class for the PFM and TTF Reader command-line applications. + */ +public abstract class AbstractFontReader { + + /** Logger instance */ + protected static Log log; + + /** + * Main constructor. + */ + protected AbstractFontReader() { + // Create logger if necessary here to allow embedding of TTFReader in + // other applications. There is a possible but harmless synchronization + // issue. + if (log == null) { + log = LogFactory.getLog(AbstractFontReader.class); + } + } + + /** + * Parse commandline arguments. put options in the HashMap and return + * arguments in the String array + * the arguments: -fn Perpetua,Bold -cn PerpetuaBold per.ttf Perpetua.xml + * returns a String[] with the per.ttf and Perpetua.xml. The hash + * will have the (key, value) pairs: (-fn, Perpetua) and (-cn, PerpetuaBold) + * @param options Map that will receive options + * @param args the command-line arguments + * @return the arguments + */ + protected static String[] parseArguments(Map options, String[] args) { + List arguments = new java.util.ArrayList(); + for (int i = 0; i < args.length; i++) { + if (args[i].startsWith("-")) { + if ("-d".equals(args[i]) || "-q".equals(args[i])) { + options.put(args[i], ""); + } else if ((i + 1) < args.length && !args[i + 1].startsWith("-")) { + options.put(args[i], args[i + 1]); + i++; + } else { + options.put(args[i], ""); + } + } else { + arguments.add(args[i]); + } + } + return (String[])arguments.toArray(new String[0]); + } + + /** + * Sets the logging level. + * @param level the logging level ("debug", "info", "error" etc., see Jakarta Commons Logging) + */ + protected static void setLogLevel(String level) { + // Set the evel for future loggers. + LogFactory.getFactory().setAttribute("level", level); + if (log instanceof CommandLineLogger) { + // Set the level for the logger creates already. + ((CommandLineLogger) log).setLogLevel(level); + } + } + + /** + * Determines the log level based of the options from the command-line. + * @param options the command-line options + */ + protected static void determineLogLevel(Map options) { + //Determine log level + if (options.get("-d") != null) { + setLogLevel("debug"); + } else if (options.get("-q") != null) { + setLogLevel("error"); + } else { + setLogLevel("info"); + } + } + +} diff --git a/src/java/org/apache/fop/fonts/apps/PFMReader.java b/src/java/org/apache/fop/fonts/apps/PFMReader.java index f5ca0e192..ebf7bd2ba 100644 --- a/src/java/org/apache/fop/fonts/apps/PFMReader.java +++ b/src/java/org/apache/fop/fonts/apps/PFMReader.java @@ -19,75 +19,34 @@ package org.apache.fop.fonts.apps; import java.io.File; -import java.io.InputStream; import java.io.IOException; -import java.util.Map; -import java.util.List; +import java.io.InputStream; import java.util.Iterator; +import java.util.Map; + import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; -import org.w3c.dom.Document; -import org.w3c.dom.Element; - -import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; - -//FOP import org.apache.fop.Version; import org.apache.fop.fonts.type1.PFMFile; import org.apache.fop.util.CommandLineLogger; +import org.w3c.dom.Document; +import org.w3c.dom.Element; /** * A tool which reads PFM files from Adobe Type 1 fonts and creates * XML font metrics file for use in FOP. */ -public class PFMReader { +public class PFMReader extends AbstractFontReader { - /** - * logging instance - */ - protected static Log log; - /** * Main constructor. */ public PFMReader() { - // Create logger if necessary here to allow embedding of PFMReader in - // other applications. There is a possible but harmless synchronization - // issue. - if (log == null) { - log = LogFactory.getLog(PFMReader.class); - } - } - - /** - * Parse commandline arguments. put options in the HashMap and return - * arguments in the String array - * the arguments: -fn Perpetua,Bold -cn PerpetuaBold per.ttf Perpetua.xml - * returns a String[] with the per.ttf and Perpetua.xml. The hash - * will have the (key, value) pairs: (-fn, Perpetua) and (-cn, PerpetuaBold) - */ - private static String[] parseArguments(Map options, String[] args) { - List arguments = new java.util.ArrayList(); - for (int i = 0; i < args.length; i++) { - if (args[i].startsWith("-")) { - if ("-d".equals(args[i]) || "-q".equals(args[i])) { - options.put(args[i], ""); - } else if ((i + 1) < args.length && !args[i + 1].startsWith("-")) { - options.put(args[i], args[i + 1]); - i++; - } else { - options.put(args[i], ""); - } - } else { - arguments.add(args[i]); - } - } - - return (String[])arguments.toArray(new String[arguments.size()]); + super(); } private static void displayUsage() { @@ -142,15 +101,8 @@ public class PFMReader { CommandLineLogger.class.getName()); } - //Determine log level - if (options.get("-d") != null) { - setLogLevel("debug"); - } else if (options.get("-q") != null) { - setLogLevel("error"); - } else { - setLogLevel("info"); - } - + determineLogLevel(options); + PFMReader app = new PFMReader(); log.info("PFM Reader for Apache FOP " + Version.getVersion() + "\n"); @@ -194,15 +146,6 @@ public class PFMReader { } } - private static void setLogLevel(String level) { - // Set the evel for future loggers. - LogFactory.getFactory().setAttribute("level", level); - if (log instanceof CommandLineLogger) { - // Set the level for the logger creates already. - ((CommandLineLogger) log).setLogLevel(level); - } - } - /** * Read a PFM file and returns it as an object. * @@ -408,22 +351,6 @@ public class PFMReader { } return doc; } - - - private String escapeString(String str) { - StringBuffer esc = new StringBuffer(); - - for (int i = 0; i < str.length(); i++) { - if (str.charAt(i) == '\\') { - esc.append("\\\\"); - } else { - esc.append(str.charAt(i)); - } - } - - return esc.toString(); - } - } diff --git a/src/java/org/apache/fop/fonts/apps/TTFReader.java b/src/java/org/apache/fop/fonts/apps/TTFReader.java index e63e7cd88..9358cab66 100644 --- a/src/java/org/apache/fop/fonts/apps/TTFReader.java +++ b/src/java/org/apache/fop/fonts/apps/TTFReader.java @@ -20,78 +20,36 @@ package org.apache.fop.fonts.apps; import java.io.File; import java.io.IOException; -import java.util.Map; -import java.util.List; import java.util.Iterator; +import java.util.Map; + import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; -import org.w3c.dom.Document; -import org.w3c.dom.Element; - -import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; - -//FOP import org.apache.fop.Version; import org.apache.fop.fonts.truetype.FontFileReader; import org.apache.fop.fonts.truetype.TTFCmapEntry; import org.apache.fop.fonts.truetype.TTFFile; import org.apache.fop.util.CommandLineLogger; +import org.w3c.dom.Document; +import org.w3c.dom.Element; /** * A tool which reads TTF files and generates * XML font metrics file for use in FOP. */ -public class TTFReader { - - /** - * logging instance - */ - protected static Log log; +public class TTFReader extends AbstractFontReader { /** * Main constructor. */ public TTFReader() { - // Create logger if necessary here to allow embedding of TTFReader in - // other applications. There is a possible but harmless synchronization - // issue. - if (log == null) { - log = LogFactory.getLog(TTFReader.class); - } + super(); } - /** - * Parse commandline arguments. put options in the HashMap and return - * arguments in the String array - * the arguments: -fn Perpetua,Bold -cn PerpetuaBold per.ttf Perpetua.xml - * returns a String[] with the per.ttf and Perpetua.xml. The hash - * will have the (key, value) pairs: (-fn, Perpetua) and (-cn, PerpetuaBold) - */ - private static String[] parseArguments(Map options, String[] args) { - List arguments = new java.util.ArrayList(); - for (int i = 0; i < args.length; i++) { - if (args[i].startsWith("-")) { - if ("-d".equals(args[i]) || "-q".equals(args[i])) { - options.put(args[i], ""); - } else if ((i + 1) < args.length && !args[i + 1].startsWith("-")) { - options.put(args[i], args[i + 1]); - i++; - } else { - options.put(args[i], ""); - } - } else { - arguments.add(args[i]); - } - } - - return (String[])arguments.toArray(new String[0]); - } - - private static void displayUsage() { System.out.println( "java " + TTFReader.class.getName() + " [options] fontfile.ttf xmlfile.xml"); @@ -158,14 +116,7 @@ public class TTFReader { CommandLineLogger.class.getName()); } - //Determine log level - if (options.get("-d") != null) { - setLogLevel("debug"); - } else if (options.get("-q") != null) { - setLogLevel("error"); - } else { - setLogLevel("info"); - } + determineLogLevel(options); TTFReader app = new TTFReader(); @@ -236,15 +187,6 @@ public class TTFReader { } } - private static void setLogLevel(String level) { - // Set the evel for future loggers. - LogFactory.getFactory().setAttribute("level", level); - if (log instanceof CommandLineLogger) { - // Set the level for the logger creates already. - ((CommandLineLogger) log).setLogLevel(level); - } - } - /** * Read a TTF file and returns it as an object. * @@ -532,19 +474,5 @@ public class TTFReader { return stb.toString(); } - private String escapeString(String str) { - StringBuffer esc = new StringBuffer(); - - for (int i = 0; i < str.length(); i++) { - if (str.charAt(i) == '\\') { - esc.append("\\\\"); - } else { - esc.append(str.charAt(i)); - } - } - - return esc.toString(); - } - }