1 17 18 19 20 package org.apache.fop.fonts.apps; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 import javax.xml.transform.Transformer ; 29 import javax.xml.transform.TransformerException ; 30 import javax.xml.transform.TransformerFactory ; 31 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 import org.apache.fop.util.CommandLineLogger; 35 36 39 public abstract class AbstractFontReader { 40 41 42 protected static Log log; 43 44 47 protected AbstractFontReader() { 48 if (log == null) { 52 log = LogFactory.getLog(AbstractFontReader.class); 53 } 54 } 55 56 66 protected static String [] parseArguments(Map options, String [] args) { 67 List arguments = new java.util.ArrayList (); 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 [])arguments.toArray(new String [0]); 83 } 84 85 89 protected static void setLogLevel(String level) { 90 LogFactory.getFactory().setAttribute("level", level); 92 if (log instanceof CommandLineLogger) { 93 ((CommandLineLogger) log).setLogLevel(level); 95 } 96 } 97 98 102 protected static void determineLogLevel(Map options) { 103 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 120 public void writeFontXML(org.w3c.dom.Document doc, String target) throws TransformerException { 121 writeFontXML(doc, new File (target)); 122 } 123 124 131 public void writeFontXML(org.w3c.dom.Document doc, File target) throws TransformerException { 132 log.info("Writing xml font file " + target + "..."); 133 134 try { 135 OutputStream out = new java.io.FileOutputStream (target); 136 out = new java.io.BufferedOutputStream (out); 137 try { 138 TransformerFactory factory = TransformerFactory.newInstance(); 139 Transformer transformer = factory.newTransformer(); 140 transformer.transform( 141 new javax.xml.transform.dom.DOMSource (doc), 142 new javax.xml.transform.stream.StreamResult (out)); 143 } finally { 144 out.close(); 145 } 146 } catch (IOException ioe) { 147 throw new TransformerException ("Error writing the output file", ioe); 148 } 149 } 150 151 } 152 | Popular Tags |