KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > fonts > apps > AbstractFontReader


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id$ */
19  
20 package org.apache.fop.fonts.apps;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import javax.xml.transform.Transformer JavaDoc;
29 import javax.xml.transform.TransformerException JavaDoc;
30 import javax.xml.transform.TransformerFactory JavaDoc;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.fop.util.CommandLineLogger;
35
36 /**
37  * Abstract base class for the PFM and TTF Reader command-line applications.
38  */

39 public abstract class AbstractFontReader {
40
41     /** Logger instance */
42     protected static Log log;
43
44     /**
45      * Main constructor.
46      */

47     protected AbstractFontReader() {
48         // Create logger if necessary here to allow embedding of TTFReader in
49
// other applications. There is a possible but harmless synchronization
50
// issue.
51
if (log == null) {
52             log = LogFactory.getLog(AbstractFontReader.class);
53         }
54     }
55
56     /**
57      * Parse commandline arguments. put options in the HashMap and return
58      * arguments in the String array
59      * the arguments: -fn Perpetua,Bold -cn PerpetuaBold per.ttf Perpetua.xml
60      * returns a String[] with the per.ttf and Perpetua.xml. The hash
61      * will have the (key, value) pairs: (-fn, Perpetua) and (-cn, PerpetuaBold)
62      * @param options Map that will receive options
63      * @param args the command-line arguments
64      * @return the arguments
65      */

66     protected static String JavaDoc[] parseArguments(Map JavaDoc options, String JavaDoc[] args) {
67         List JavaDoc arguments = new java.util.ArrayList JavaDoc();
68         for (int i = 0; i < args.length; i++) {
69             if (args[i].startsWith("-")) {
70                 if ("-d".equals(args[i]) || "-q".equals(args[i])) {
71                     options.put(args[i], "");
72                 } else if ((i + 1) < args.length && !args[i + 1].startsWith("-")) {
73                     options.put(args[i], args[i + 1]);
74                     i++;
75                 } else {
76                     options.put(args[i], "");
77                 }
78             } else {
79                 arguments.add(args[i]);
80             }
81         }
82         return (String JavaDoc[])arguments.toArray(new String JavaDoc[0]);
83     }
84     
85     /**
86      * Sets the logging level.
87      * @param level the logging level ("debug", "info", "error" etc., see Jakarta Commons Logging)
88      */

89     protected static void setLogLevel(String JavaDoc level) {
90         // Set the evel for future loggers.
91
LogFactory.getFactory().setAttribute("level", level);
92         if (log instanceof CommandLineLogger) {
93             // Set the level for the logger creates already.
94
((CommandLineLogger) log).setLogLevel(level);
95         }
96     }
97     
98     /**
99      * Determines the log level based of the options from the command-line.
100      * @param options the command-line options
101      */

102     protected static void determineLogLevel(Map JavaDoc options) {
103         //Determine log level
104
if (options.get("-d") != null) {
105             setLogLevel("debug");
106         } else if (options.get("-q") != null) {
107             setLogLevel("error");
108         } else {
109             setLogLevel("info");
110         }
111     }
112
113     /**
114      * Writes the generated DOM Document to a file.
115      *
116      * @param doc The DOM Document to save.
117      * @param target The target filename for the XML file.
118      * @throws TransformerException if an error occurs during serialization
119      */

120     public void writeFontXML(org.w3c.dom.Document JavaDoc doc, String JavaDoc target) throws TransformerException JavaDoc {
121         writeFontXML(doc, new File JavaDoc(target));
122     }
123     
124     /**
125      * Writes the generated DOM Document to a file.
126      *
127      * @param doc The DOM Document to save.
128      * @param target The target file for the XML file.
129      * @throws TransformerException if an error occurs during serialization
130      */

131     public void writeFontXML(org.w3c.dom.Document JavaDoc doc, File JavaDoc target) throws TransformerException JavaDoc {
132         log.info("Writing xml font file " + target + "...");
133     
134         try {
135             OutputStream JavaDoc out = new java.io.FileOutputStream JavaDoc(target);
136             out = new java.io.BufferedOutputStream JavaDoc(out);
137             try {
138                 TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
139                 Transformer JavaDoc transformer = factory.newTransformer();
140                 transformer.transform(
141                         new javax.xml.transform.dom.DOMSource JavaDoc(doc),
142                         new javax.xml.transform.stream.StreamResult JavaDoc(out));
143             } finally {
144                 out.close();
145             }
146         } catch (IOException JavaDoc ioe) {
147             throw new TransformerException JavaDoc("Error writing the output file", ioe);
148         }
149     }
150     
151 }
152
Popular Tags