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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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.util.List;
  26. import org.apache.commons.io.IOUtils;
  27. import org.apache.fop.apps.FOUserAgent;
  28. import org.apache.fop.apps.MimeConstants;
  29. /**
  30. * Main command-line class for Apache FOP.
  31. */
  32. public final class Main {
  33. private Main() {
  34. }
  35. /**
  36. * @return the list of URLs to all libraries.
  37. * @throws MalformedURLException In case there is a problem converting java.io.File
  38. * instances to URLs.
  39. */
  40. public static URL[] getJARList() throws MalformedURLException {
  41. String fopHome = System.getProperty("fop.home");
  42. File baseDir;
  43. if (fopHome != null) {
  44. baseDir = new File(fopHome).getAbsoluteFile();
  45. } else {
  46. baseDir = new File(".").getAbsoluteFile().getParentFile();
  47. }
  48. File buildDir;
  49. if ("build".equals(baseDir.getName())) {
  50. buildDir = baseDir;
  51. baseDir = baseDir.getParentFile();
  52. } else {
  53. buildDir = new File(baseDir, "build");
  54. }
  55. File fopJar = new File(buildDir, "fop.jar");
  56. if (!fopJar.exists()) {
  57. fopJar = new File(baseDir, "fop.jar");
  58. }
  59. if (!fopJar.exists()) {
  60. throw new RuntimeException("fop.jar not found in directory: "
  61. + baseDir.getAbsolutePath() + " (or below)");
  62. }
  63. List jars = new java.util.ArrayList();
  64. jars.add(fopJar.toURI().toURL());
  65. File[] files;
  66. FileFilter filter = new FileFilter() {
  67. public boolean accept(File pathname) {
  68. return pathname.getName().endsWith(".jar");
  69. }
  70. };
  71. File libDir = new File(baseDir, "lib");
  72. if (!libDir.exists()) {
  73. libDir = baseDir;
  74. }
  75. files = libDir.listFiles(filter);
  76. if (files != null) {
  77. for (int i = 0, size = files.length; i < size; i++) {
  78. jars.add(files[i].toURI().toURL());
  79. }
  80. }
  81. String optionalLib = System.getProperty("fop.optional.lib");
  82. if (optionalLib != null) {
  83. files = new File(optionalLib).listFiles(filter);
  84. if (files != null) {
  85. for (int i = 0, size = files.length; i < size; i++) {
  86. jars.add(files[i].toURI().toURL());
  87. }
  88. }
  89. }
  90. URL[] urls = (URL[])jars.toArray(new URL[jars.size()]);
  91. /*
  92. for (int i = 0, c = urls.length; i < c; i++) {
  93. System.out.println(urls[i]);
  94. }*/
  95. return urls;
  96. }
  97. /**
  98. * @return true if FOP's dependecies are available in the current ClassLoader setup.
  99. */
  100. public static boolean checkDependencies() {
  101. try {
  102. //System.out.println(Thread.currentThread().getContextClassLoader());
  103. Class clazz = Class.forName("org.apache.commons.io.IOUtils");
  104. if (clazz != null) {
  105. clazz = Class.forName("org.apache.avalon.framework.configuration.Configuration");
  106. }
  107. return (clazz != null);
  108. } catch (Exception e) {
  109. return false;
  110. }
  111. }
  112. /**
  113. * Dynamically builds a ClassLoader and executes FOP.
  114. * @param args command-line arguments
  115. */
  116. public static void startFOPWithDynamicClasspath(String[] args) {
  117. try {
  118. URL[] urls = getJARList();
  119. //System.out.println("CCL: "
  120. // + Thread.currentThread().getContextClassLoader().toString());
  121. ClassLoader loader = new java.net.URLClassLoader(urls, null);
  122. Thread.currentThread().setContextClassLoader(loader);
  123. Class clazz = Class.forName("org.apache.fop.cli.Main", true, loader);
  124. //System.out.println("CL: " + clazz.getClassLoader().toString());
  125. Method mainMethod = clazz.getMethod("startFOP", new Class[] {String[].class});
  126. mainMethod.invoke(null, new Object[] {args});
  127. } catch (Exception e) {
  128. System.err.println("Unable to start FOP:");
  129. e.printStackTrace();
  130. System.exit(-1);
  131. }
  132. }
  133. /**
  134. * Executes FOP with the given arguments. If no argument is provided, returns its
  135. * version number as well as a short usage statement; if '-v' is provided, returns its
  136. * version number alone; if '-h' is provided, returns its short help message.
  137. *
  138. * @param args command-line arguments
  139. */
  140. public static void startFOP(String[] args) {
  141. //System.out.println("static CCL: "
  142. // + Thread.currentThread().getContextClassLoader().toString());
  143. //System.out.println("static CL: " + Fop.class.getClassLoader().toString());
  144. CommandLineOptions options = null;
  145. FOUserAgent foUserAgent = null;
  146. OutputStream out = null;
  147. try {
  148. options = new CommandLineOptions();
  149. if (!options.parse(args)) {
  150. System.exit(0);
  151. }
  152. foUserAgent = options.getFOUserAgent();
  153. String outputFormat = options.getOutputFormat();
  154. try {
  155. if (options.getOutputFile() != null) {
  156. out = new java.io.BufferedOutputStream(
  157. new java.io.FileOutputStream(options.getOutputFile()));
  158. foUserAgent.setOutputFile(options.getOutputFile());
  159. } else if (options.isOutputToStdOut()) {
  160. out = new java.io.BufferedOutputStream(System.out);
  161. }
  162. if (!MimeConstants.MIME_XSL_FO.equals(outputFormat)) {
  163. options.getInputHandler().renderTo(foUserAgent, outputFormat, out);
  164. } else {
  165. options.getInputHandler().transformTo(out);
  166. }
  167. } finally {
  168. IOUtils.closeQuietly(out);
  169. }
  170. // System.exit(0) called to close AWT/SVG-created threads, if any.
  171. // AWTRenderer closes with window shutdown, so exit() should not
  172. // be called here
  173. if (!MimeConstants.MIME_FOP_AWT_PREVIEW.equals(outputFormat)) {
  174. System.exit(0);
  175. }
  176. } catch (Exception e) {
  177. if (options != null) {
  178. options.getLogger().error("Exception", e);
  179. if (options.getOutputFile() != null) {
  180. options.getOutputFile().delete();
  181. }
  182. }
  183. System.exit(1);
  184. }
  185. }
  186. /**
  187. * The main routine for the command line interface
  188. * @param args the command line parameters
  189. */
  190. public static void main(String[] args) {
  191. if (checkDependencies()) {
  192. startFOP(args);
  193. } else {
  194. startFOPWithDynamicClasspath(args);
  195. }
  196. }
  197. }