選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CommandLine.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*-- $Id$ --
  2. ============================================================================
  3. The Apache Software License, Version 1.1
  4. ============================================================================
  5. Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modifica-
  7. tion, are permitted provided that the following conditions are met:
  8. 1. Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright notice,
  11. this list of conditions and the following disclaimer in the documentation
  12. and/or other materials provided with the distribution.
  13. 3. The end-user documentation included with the redistribution, if any, must
  14. include the following acknowledgment: "This product includes software
  15. developed by the Apache Software Foundation (http://www.apache.org/)."
  16. Alternately, this acknowledgment may appear in the software itself, if
  17. and wherever such third-party acknowledgments normally appear.
  18. 4. The names "Fop" and "Apache Software Foundation" must not be used to
  19. endorse or promote products derived from this software without prior
  20. written permission. For written permission, please contact
  21. apache@apache.org.
  22. 5. Products derived from this software may not be called "Apache", nor may
  23. "Apache" appear in their name, without prior written permission of the
  24. Apache Software Foundation.
  25. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  26. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  27. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  29. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  30. DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  31. OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  32. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  34. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. This software consists of voluntary contributions made by many individuals
  36. on behalf of the Apache Software Foundation and was originally created by
  37. James Tauber <jtauber@jtauber.com>. For more information on the Apache
  38. Software Foundation, please see <http://www.apache.org/>.
  39. */
  40. package org.apache.xml.fop.apps;
  41. // SAX
  42. import org.xml.sax.Parser;
  43. import org.xml.sax.InputSource;
  44. import org.xml.sax.SAXException;
  45. import org.xml.sax.SAXParseException;
  46. // Java
  47. import java.io.FileReader;
  48. import java.io.File;
  49. import java.io.FileWriter;
  50. import java.io.PrintWriter;
  51. import java.io.IOException;
  52. import java.io.FileNotFoundException;
  53. import java.net.URL;
  54. /**
  55. * mainline class.
  56. *
  57. * Gets input and output filenames from the command line.
  58. * Creates a SAX Parser (defaulting to XP).
  59. *
  60. */
  61. public class CommandLine {
  62. /**
  63. * creates a SAX parser, using the value of org.xml.sax.parser
  64. * defaulting to com.jclark.xml.sax.Driver
  65. *
  66. * @return the created SAX parser
  67. */
  68. static Parser createParser() {
  69. String parserClassName =
  70. System.getProperty("org.xml.sax.parser");
  71. if (parserClassName == null) {
  72. parserClassName = "com.jclark.xml.sax.Driver";
  73. }
  74. System.err.println("using SAX parser " + parserClassName);
  75. try {
  76. return (Parser)
  77. Class.forName(parserClassName).newInstance();
  78. } catch (ClassNotFoundException e) {
  79. System.err.println("Could not find " + parserClassName);
  80. } catch (InstantiationException e) {
  81. System.err.println("Could not instantiate "
  82. + parserClassName);
  83. } catch (IllegalAccessException e) {
  84. System.err.println("Could not access " + parserClassName);
  85. } catch (ClassCastException e) {
  86. System.err.println(parserClassName + " is not a SAX driver");
  87. }
  88. return null;
  89. }
  90. /**
  91. * create an InputSource from a file name
  92. *
  93. * @param filename the name of the file
  94. * @return the InputSource created
  95. */
  96. protected static InputSource fileInputSource(String filename) {
  97. /* this code adapted from James Clark's in XT */
  98. File file = new File(filename);
  99. String path = file.getAbsolutePath();
  100. String fSep = System.getProperty("file.separator");
  101. if (fSep != null && fSep.length() == 1)
  102. path = path.replace(fSep.charAt(0), '/');
  103. if (path.length() > 0 && path.charAt(0) != '/')
  104. path = '/' + path;
  105. try {
  106. return new InputSource(new URL("file", null,
  107. path).toString());
  108. }
  109. catch (java.net.MalformedURLException e) {
  110. throw new Error("unexpected MalformedURLException");
  111. }
  112. }
  113. /**
  114. * mainline method
  115. *
  116. * first command line argument is input file
  117. * second command line argument is output file
  118. *
  119. * @param command line arguments
  120. */
  121. public static void main(String[] args) {
  122. String version = Version.getVersion();
  123. System.err.println(version);
  124. if (args.length != 2) {
  125. System.err.println("usage: java "
  126. + "org.apache.xml.fop.apps.CommandLine "
  127. + "formatting-object-file pdf-file");
  128. System.exit(1);
  129. }
  130. Parser parser = createParser();
  131. if (parser == null) {
  132. System.err.println("ERROR: Unable to create SAX parser");
  133. System.exit(1);
  134. }
  135. try {
  136. Driver driver = new Driver();
  137. driver.setRenderer("org.apache.xml.fop.render.pdf.PDFRenderer", version);
  138. driver.addElementMapping("org.apache.xml.fop.fo.StandardElementMapping");
  139. driver.addElementMapping("org.apache.xml.fop.svg.SVGElementMapping");
  140. driver.setWriter(new PrintWriter(new FileWriter(args[1])));
  141. driver.buildFOTree(parser, fileInputSource(args[0]));
  142. driver.format();
  143. driver.render();
  144. } catch (Exception e) {
  145. System.err.println("FATAL ERROR: " + e.getMessage());
  146. System.exit(1);
  147. }
  148. }
  149. }