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.

Main.java 7.8KB

Fop.java gets two new constructors: Fop(String) and Fop(String, FOUserAgent) where the String is the MIME type for the desired output format. MimeConstants provides a comprehensive list of MIME types used in Fop.java. Non-standard, FOP-specific MIME types changed to a uniform pattern: application/X-fop-awt-preview, application/X-fop-print and application/X-fop-areatree. RendererFactory now supports manual registration and dynamic discovery of Renderers and FOEventHandlers by their MIME types. Instantitation is done using MIME types everywhere. The RENDER_* constants are mapped to MIME types in Fop.java. RendererFactory is now an instantiable class whose reference is held by FOUserAgent just like it is done for the XLMHandlers. Renderers and FOEventHandlers now each have a *Maker class which is a kind of factory class which is used to register a Renderer/FOEventHandler and additionally serves to provide additional information about the thing, such as the MIME types it supports and if the implementation requires an OutputStream. The command-line gets a new option: -out application/pdf myfile.pdf is the generic way to create an output file. If someone created a WordML output handler and provided the right service resource file he could specify "-out text/xml+msword out.xml". ".out list" lists all MIME types that are available for output. Renderers can now potionally expose a Graphics2DAdapter which in concert with Graphics2DImagePainter can be used by FOP extensions to paint their content directly using a Graphics2D instance. That makes it possible to avoid a detour via SVG/Batik in certain cases. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@332549 13f79535-47bb-0310-9956-ffa450edef68
18 years ago
Fop.java gets two new constructors: Fop(String) and Fop(String, FOUserAgent) where the String is the MIME type for the desired output format. MimeConstants provides a comprehensive list of MIME types used in Fop.java. Non-standard, FOP-specific MIME types changed to a uniform pattern: application/X-fop-awt-preview, application/X-fop-print and application/X-fop-areatree. RendererFactory now supports manual registration and dynamic discovery of Renderers and FOEventHandlers by their MIME types. Instantitation is done using MIME types everywhere. The RENDER_* constants are mapped to MIME types in Fop.java. RendererFactory is now an instantiable class whose reference is held by FOUserAgent just like it is done for the XLMHandlers. Renderers and FOEventHandlers now each have a *Maker class which is a kind of factory class which is used to register a Renderer/FOEventHandler and additionally serves to provide additional information about the thing, such as the MIME types it supports and if the implementation requires an OutputStream. The command-line gets a new option: -out application/pdf myfile.pdf is the generic way to create an output file. If someone created a WordML output handler and provided the right service resource file he could specify "-out text/xml+msword out.xml". ".out list" lists all MIME types that are available for output. Renderers can now potionally expose a Graphics2DAdapter which in concert with Graphics2DImagePainter can be used by FOP extensions to paint their content directly using a Graphics2D instance. That makes it possible to avoid a detour via SVG/Batik in certain cases. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@332549 13f79535-47bb-0310-9956-ffa450edef68
18 years ago
Fop.java gets two new constructors: Fop(String) and Fop(String, FOUserAgent) where the String is the MIME type for the desired output format. MimeConstants provides a comprehensive list of MIME types used in Fop.java. Non-standard, FOP-specific MIME types changed to a uniform pattern: application/X-fop-awt-preview, application/X-fop-print and application/X-fop-areatree. RendererFactory now supports manual registration and dynamic discovery of Renderers and FOEventHandlers by their MIME types. Instantitation is done using MIME types everywhere. The RENDER_* constants are mapped to MIME types in Fop.java. RendererFactory is now an instantiable class whose reference is held by FOUserAgent just like it is done for the XLMHandlers. Renderers and FOEventHandlers now each have a *Maker class which is a kind of factory class which is used to register a Renderer/FOEventHandler and additionally serves to provide additional information about the thing, such as the MIME types it supports and if the implementation requires an OutputStream. The command-line gets a new option: -out application/pdf myfile.pdf is the generic way to create an output file. If someone created a WordML output handler and provided the right service resource file he could specify "-out text/xml+msword out.xml". ".out list" lists all MIME types that are available for output. Renderers can now potionally expose a Graphics2DAdapter which in concert with Graphics2DImagePainter can be used by FOP extensions to paint their content directly using a Graphics2D instance. That makes it possible to avoid a detour via SVG/Batik in certain cases. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@332549 13f79535-47bb-0310-9956-ffa450edef68
18 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.cli;
  19. import java.io.File;
  20. import java.io.FileFilter;
  21. import java.io.OutputStream;
  22. import java.lang.reflect.Method;
  23. import java.net.MalformedURLException;
  24. import java.net.URL;
  25. import java.security.AccessController;
  26. import java.security.PrivilegedAction;
  27. import java.util.List;
  28. import org.apache.commons.io.IOUtils;
  29. import org.apache.fop.apps.FOUserAgent;
  30. import org.apache.fop.apps.MimeConstants;
  31. /**
  32. * Main command-line class for Apache FOP.
  33. */
  34. public final class Main {
  35. private Main() {
  36. }
  37. /**
  38. * @return the list of URLs to all libraries.
  39. * @throws MalformedURLException In case there is a problem converting java.io.File
  40. * instances to URLs.
  41. */
  42. public static URL[] getJARList() throws MalformedURLException {
  43. String fopHome = System.getProperty("fop.home");
  44. File baseDir;
  45. if (fopHome != null) {
  46. baseDir = new File(fopHome).getAbsoluteFile();
  47. } else {
  48. baseDir = new File(".").getAbsoluteFile().getParentFile();
  49. }
  50. File buildDir;
  51. if ("build".equals(baseDir.getName())) {
  52. buildDir = baseDir;
  53. baseDir = baseDir.getParentFile();
  54. } else {
  55. buildDir = new File(baseDir, "build");
  56. }
  57. File fopJar = new File(buildDir, "fop.jar");
  58. if (!fopJar.exists()) {
  59. fopJar = new File(baseDir, "fop.jar");
  60. }
  61. if (!fopJar.exists()) {
  62. throw new RuntimeException("fop.jar not found in directory: "
  63. + baseDir.getAbsolutePath() + " (or below)");
  64. }
  65. List jars = new java.util.ArrayList();
  66. jars.add(fopJar.toURI().toURL());
  67. File[] files;
  68. FileFilter filter = new FileFilter() {
  69. public boolean accept(File pathname) {
  70. return pathname.getName().endsWith(".jar");
  71. }
  72. };
  73. File libDir = new File(baseDir, "lib");
  74. if (!libDir.exists()) {
  75. libDir = baseDir;
  76. }
  77. files = libDir.listFiles(filter);
  78. if (files != null) {
  79. for (File file : files) {
  80. jars.add(file.toURI().toURL());
  81. }
  82. }
  83. String optionalLib = System.getProperty("fop.optional.lib");
  84. if (optionalLib != null) {
  85. files = new File(optionalLib).listFiles(filter);
  86. if (files != null) {
  87. for (File file : files) {
  88. jars.add(file.toURI().toURL());
  89. }
  90. }
  91. }
  92. URL[] urls = (URL[])jars.toArray(new URL[jars.size()]);
  93. /*
  94. for (int i = 0, c = urls.length; i < c; i++) {
  95. System.out.println(urls[i]);
  96. }*/
  97. return urls;
  98. }
  99. /**
  100. * @return true if FOP's dependecies are available in the current ClassLoader setup.
  101. */
  102. public static boolean checkDependencies() {
  103. try {
  104. //System.out.println(Thread.currentThread().getContextClassLoader());
  105. Class clazz = Class.forName("org.apache.commons.io.IOUtils");
  106. return (clazz != null);
  107. } catch (Exception e) {
  108. return false;
  109. }
  110. }
  111. /**
  112. * Dynamically builds a ClassLoader and executes FOP.
  113. * @param args command-line arguments
  114. */
  115. public static void startFOPWithDynamicClasspath(String[] args) {
  116. try {
  117. final URL[] urls = getJARList();
  118. //System.out.println("CCL: "
  119. // + Thread.currentThread().getContextClassLoader().toString());
  120. ClassLoader loader = (ClassLoader)
  121. AccessController.doPrivileged(new PrivilegedAction() {
  122. public Object run() {
  123. return new java.net.URLClassLoader(urls, null);
  124. }
  125. });
  126. Thread.currentThread().setContextClassLoader(loader);
  127. Class clazz = Class.forName("org.apache.fop.cli.Main", true, loader);
  128. //System.out.println("CL: " + clazz.getClassLoader().toString());
  129. Method mainMethod = clazz.getMethod("startFOP", new Class[] {String[].class});
  130. mainMethod.invoke(null, new Object[] {args});
  131. } catch (Exception e) {
  132. System.err.println("Unable to start FOP:");
  133. e.printStackTrace();
  134. // @SuppressFBWarnings("DM_EXIT")
  135. System.exit(-1);
  136. }
  137. }
  138. /**
  139. * Executes FOP with the given arguments. If no argument is provided, returns its
  140. * version number as well as a short usage statement; if '-v' is provided, returns its
  141. * version number alone; if '-h' is provided, returns its short help message.
  142. *
  143. * @param args command-line arguments
  144. */
  145. public static void startFOP(String[] args) {
  146. //System.out.println("static CCL: "
  147. // + Thread.currentThread().getContextClassLoader().toString());
  148. //System.out.println("static CL: " + Fop.class.getClassLoader().toString());
  149. CommandLineOptions options = null;
  150. FOUserAgent foUserAgent = null;
  151. OutputStream out = null;
  152. try {
  153. options = new CommandLineOptions();
  154. if (!options.parse(args)) {
  155. // @SuppressFBWarnings("DM_EXIT")
  156. System.exit(0);
  157. }
  158. foUserAgent = options.getFOUserAgent();
  159. String outputFormat = options.getOutputFormat();
  160. try {
  161. if (options.getOutputFile() != null) {
  162. out = new java.io.BufferedOutputStream(
  163. new java.io.FileOutputStream(options.getOutputFile()));
  164. foUserAgent.setOutputFile(options.getOutputFile());
  165. } else if (options.isOutputToStdOut()) {
  166. out = new java.io.BufferedOutputStream(System.out);
  167. }
  168. if (!MimeConstants.MIME_XSL_FO.equals(outputFormat)) {
  169. options.getInputHandler().renderTo(foUserAgent, outputFormat, out);
  170. } else {
  171. options.getInputHandler().transformTo(out);
  172. }
  173. } finally {
  174. IOUtils.closeQuietly(out);
  175. }
  176. // System.exit(0) called to close AWT/SVG-created threads, if any.
  177. // AWTRenderer closes with window shutdown, so exit() should not
  178. // be called here
  179. if (!MimeConstants.MIME_FOP_AWT_PREVIEW.equals(outputFormat)) {
  180. System.exit(0);
  181. }
  182. } catch (Exception e) {
  183. if (options != null) {
  184. options.getLogger().error("Exception", e);
  185. if (options.getOutputFile() != null) {
  186. options.getOutputFile().delete();
  187. }
  188. }
  189. System.exit(1);
  190. }
  191. }
  192. /**
  193. * The main routine for the command line interface
  194. * @param args the command line parameters
  195. */
  196. public static void main(String[] args) {
  197. if (checkDependencies()) {
  198. startFOP(args);
  199. } else {
  200. startFOPWithDynamicClasspath(args);
  201. }
  202. }
  203. }