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.0KB

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