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:
018: /* $Id$ */
019:
020: package org.apache.fop.fonts.apps;
021:
022: import java.io.File;
023: import java.io.IOException;
024: import java.io.OutputStream;
025: import java.util.List;
026: import java.util.Map;
027:
028: import javax.xml.transform.Transformer;
029: import javax.xml.transform.TransformerException;
030: import javax.xml.transform.TransformerFactory;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.apache.fop.util.CommandLineLogger;
035:
036: /**
037: * Abstract base class for the PFM and TTF Reader command-line applications.
038: */
039: public abstract class AbstractFontReader {
040:
041: /** Logger instance */
042: protected static Log log;
043:
044: /**
045: * Main constructor.
046: */
047: protected AbstractFontReader() {
048: // Create logger if necessary here to allow embedding of TTFReader in
049: // other applications. There is a possible but harmless synchronization
050: // issue.
051: if (log == null) {
052: log = LogFactory.getLog(AbstractFontReader.class);
053: }
054: }
055:
056: /**
057: * Parse commandline arguments. put options in the HashMap and return
058: * arguments in the String array
059: * the arguments: -fn Perpetua,Bold -cn PerpetuaBold per.ttf Perpetua.xml
060: * returns a String[] with the per.ttf and Perpetua.xml. The hash
061: * will have the (key, value) pairs: (-fn, Perpetua) and (-cn, PerpetuaBold)
062: * @param options Map that will receive options
063: * @param args the command-line arguments
064: * @return the arguments
065: */
066: protected static String[] parseArguments(Map options, String[] args) {
067: List arguments = new java.util.ArrayList();
068: for (int i = 0; i < args.length; i++) {
069: if (args[i].startsWith("-")) {
070: if ("-d".equals(args[i]) || "-q".equals(args[i])) {
071: options.put(args[i], "");
072: } else if ((i + 1) < args.length
073: && !args[i + 1].startsWith("-")) {
074: options.put(args[i], args[i + 1]);
075: i++;
076: } else {
077: options.put(args[i], "");
078: }
079: } else {
080: arguments.add(args[i]);
081: }
082: }
083: return (String[]) arguments.toArray(new String[0]);
084: }
085:
086: /**
087: * Sets the logging level.
088: * @param level the logging level ("debug", "info", "error" etc., see Jakarta Commons Logging)
089: */
090: protected static void setLogLevel(String level) {
091: // Set the evel for future loggers.
092: LogFactory.getFactory().setAttribute("level", level);
093: if (log instanceof CommandLineLogger) {
094: // Set the level for the logger creates already.
095: ((CommandLineLogger) log).setLogLevel(level);
096: }
097: }
098:
099: /**
100: * Determines the log level based of the options from the command-line.
101: * @param options the command-line options
102: */
103: protected static void determineLogLevel(Map options) {
104: //Determine log level
105: if (options.get("-d") != null) {
106: setLogLevel("debug");
107: } else if (options.get("-q") != null) {
108: setLogLevel("error");
109: } else {
110: setLogLevel("info");
111: }
112: }
113:
114: /**
115: * Writes the generated DOM Document to a file.
116: *
117: * @param doc The DOM Document to save.
118: * @param target The target filename for the XML file.
119: * @throws TransformerException if an error occurs during serialization
120: */
121: public void writeFontXML(org.w3c.dom.Document doc, String target)
122: throws TransformerException {
123: writeFontXML(doc, new File(target));
124: }
125:
126: /**
127: * Writes the generated DOM Document to a file.
128: *
129: * @param doc The DOM Document to save.
130: * @param target The target file for the XML file.
131: * @throws TransformerException if an error occurs during serialization
132: */
133: public void writeFontXML(org.w3c.dom.Document doc, File target)
134: throws TransformerException {
135: log.info("Writing xml font file " + target + "...");
136:
137: try {
138: OutputStream out = new java.io.FileOutputStream(target);
139: out = new java.io.BufferedOutputStream(out);
140: try {
141: TransformerFactory factory = TransformerFactory
142: .newInstance();
143: Transformer transformer = factory.newTransformer();
144: transformer
145: .transform(
146: new javax.xml.transform.dom.DOMSource(
147: doc),
148: new javax.xml.transform.stream.StreamResult(
149: out));
150: } finally {
151: out.close();
152: }
153: } catch (IOException ioe) {
154: throw new TransformerException(
155: "Error writing the output file", ioe);
156: }
157: }
158:
159: }
|