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

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