You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractFontReader.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. /* $Id$ */
  18. package org.apache.fop.fonts.apps;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.io.OutputStream;
  22. import java.util.List;
  23. import java.util.Map;
  24. import javax.xml.transform.Transformer;
  25. import javax.xml.transform.TransformerException;
  26. import javax.xml.transform.TransformerFactory;
  27. import org.apache.commons.logging.Log;
  28. import org.apache.commons.logging.LogFactory;
  29. /**
  30. * Abstract base class for the PFM and TTF Reader command-line applications.
  31. */
  32. public abstract class AbstractFontReader {
  33. /** Logger instance */
  34. protected static Log log;
  35. /**
  36. * Main constructor.
  37. */
  38. protected AbstractFontReader() {
  39. // Create logger if necessary here to allow embedding of TTFReader in
  40. // other applications. There is a possible but harmless synchronization
  41. // issue.
  42. if (log == null) {
  43. log = LogFactory.getLog(AbstractFontReader.class);
  44. }
  45. }
  46. /**
  47. * Parse commandline arguments. put options in the HashMap and return
  48. * arguments in the String array
  49. * the arguments: -fn Perpetua,Bold -cn PerpetuaBold per.ttf Perpetua.xml
  50. * returns a String[] with the per.ttf and Perpetua.xml. The hash
  51. * will have the (key, value) pairs: (-fn, Perpetua) and (-cn, PerpetuaBold)
  52. * @param options Map that will receive options
  53. * @param args the command-line arguments
  54. * @return the arguments
  55. */
  56. protected static String[] parseArguments(Map options, String[] args) {
  57. List arguments = new java.util.ArrayList();
  58. for (int i = 0; i < args.length; i++) {
  59. if (args[i].startsWith("-")) {
  60. if ("-t".equals(args[i]) || "-d".equals(args[i]) || "-q".equals(args[i])) {
  61. options.put(args[i], "");
  62. } else if ((i + 1) < args.length && !args[i + 1].startsWith("-")) {
  63. options.put(args[i], args[i + 1]);
  64. i++;
  65. } else {
  66. options.put(args[i], "");
  67. }
  68. } else {
  69. arguments.add(args[i]);
  70. }
  71. }
  72. return (String[])arguments.toArray(new String[0]);
  73. }
  74. /**
  75. * Sets the logging level.
  76. * @param level the logging level ("debug", "info", "error" etc., see Jakarta Commons Logging)
  77. */
  78. protected static void setLogLevel(String level) {
  79. // Set the evel for future loggers.
  80. LogFactory.getFactory().setAttribute("level", level);
  81. }
  82. /**
  83. * Determines the log level based of the options from the command-line.
  84. * @param options the command-line options
  85. */
  86. protected static void determineLogLevel(Map options) {
  87. //Determine log level
  88. if (options.get("-t") != null) {
  89. setLogLevel("trace");
  90. } else if (options.get("-d") != null) {
  91. setLogLevel("debug");
  92. } else if (options.get("-q") != null) {
  93. setLogLevel("error");
  94. } else {
  95. setLogLevel("info");
  96. }
  97. }
  98. /**
  99. * Writes the generated DOM Document to a file.
  100. *
  101. * @param doc The DOM Document to save.
  102. * @param target The target filename for the XML file.
  103. * @throws TransformerException if an error occurs during serialization
  104. */
  105. public void writeFontXML(org.w3c.dom.Document doc, String target) throws TransformerException {
  106. writeFontXML(doc, new File(target));
  107. }
  108. /**
  109. * Writes the generated DOM Document to a file.
  110. *
  111. * @param doc The DOM Document to save.
  112. * @param target The target file for the XML file.
  113. * @throws TransformerException if an error occurs during serialization
  114. */
  115. public void writeFontXML(org.w3c.dom.Document doc, File target) throws TransformerException {
  116. log.info("Writing xml font file " + target + "...");
  117. try {
  118. OutputStream out = new java.io.FileOutputStream(target);
  119. out = new java.io.BufferedOutputStream(out);
  120. try {
  121. TransformerFactory factory = TransformerFactory.newInstance();
  122. Transformer transformer = factory.newTransformer();
  123. transformer.transform(
  124. new javax.xml.transform.dom.DOMSource(doc),
  125. new javax.xml.transform.stream.StreamResult(out));
  126. } finally {
  127. out.close();
  128. }
  129. } catch (IOException ioe) {
  130. throw new TransformerException("Error writing the output file", ioe);
  131. }
  132. }
  133. }