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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright 2005 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.cli;
  18. import java.io.BufferedOutputStream;
  19. import java.io.File;
  20. import java.io.FileFilter;
  21. import java.io.FileOutputStream;
  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.fop.apps.FOUserAgent;
  27. import org.apache.fop.apps.Fop;
  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. File baseDir = new File(".").getAbsoluteFile().getParentFile();
  40. File buildDir;
  41. if ("build".equals(baseDir.getName())) {
  42. buildDir = baseDir;
  43. baseDir = baseDir.getParentFile();
  44. } else {
  45. buildDir = new File(baseDir, "build");
  46. }
  47. File fopJar = new File(buildDir, "fop.jar");
  48. if (!fopJar.exists()) {
  49. fopJar = new File(baseDir, "fop.jar");
  50. }
  51. if (!fopJar.exists()) {
  52. throw new RuntimeException("fop.jar not found in directory: "
  53. + baseDir.getAbsolutePath() + " (or below)");
  54. }
  55. List jars = new java.util.ArrayList();
  56. jars.add(fopJar.toURL());
  57. File[] files;
  58. FileFilter filter = new FileFilter() {
  59. public boolean accept(File pathname) {
  60. return pathname.getName().endsWith(".jar");
  61. }
  62. };
  63. File libDir = new File(baseDir, "lib");
  64. if (!libDir.exists()) {
  65. libDir = baseDir;
  66. }
  67. files = libDir.listFiles(filter);
  68. if (files != null) {
  69. for (int i = 0, size = files.length; i < size; i++) {
  70. jars.add(files[i].toURL());
  71. }
  72. }
  73. String optionalLib = System.getProperty("fop.optional.lib");
  74. if (optionalLib != null) {
  75. files = new File(optionalLib).listFiles(filter);
  76. if (files != null) {
  77. for (int i = 0, size = files.length; i < size; i++) {
  78. jars.add(files[i].toURL());
  79. }
  80. }
  81. }
  82. URL[] urls = (URL[])jars.toArray(new URL[jars.size()]);
  83. /*
  84. for (int i = 0, c = urls.length; i < c; i++) {
  85. System.out.println(urls[i]);
  86. }*/
  87. return urls;
  88. }
  89. /**
  90. * @return true if FOP's dependecies are available in the current ClassLoader setup.
  91. */
  92. public static boolean checkDependencies() {
  93. try {
  94. //System.out.println(Thread.currentThread().getContextClassLoader());
  95. Class clazz = Class.forName("org.apache.batik.Version");
  96. if (clazz != null) {
  97. clazz = Class.forName("org.apache.avalon.framework.configuration.Configuration");
  98. }
  99. return (clazz != null);
  100. } catch (Exception e) {
  101. return false;
  102. }
  103. }
  104. /**
  105. * Dynamically builds a ClassLoader and executes FOP.
  106. * @param args command-line arguments
  107. */
  108. public static void startFOPWithDynamicClasspath(String[] args) {
  109. try {
  110. URL[] urls = getJARList();
  111. //System.out.println("CCL: "
  112. // + Thread.currentThread().getContextClassLoader().toString());
  113. ClassLoader loader = new java.net.URLClassLoader(urls, null);
  114. Thread.currentThread().setContextClassLoader(loader);
  115. Class clazz = Class.forName("org.apache.fop.cli.Main", true, loader);
  116. //System.out.println("CL: " + clazz.getClassLoader().toString());
  117. Method mainMethod = clazz.getMethod("startFOP", new Class[] {String[].class});
  118. mainMethod.invoke(null, new Object[] {args});
  119. } catch (Exception e) {
  120. System.err.println("Unable to start FOP:");
  121. e.printStackTrace();
  122. System.exit(-1);
  123. }
  124. }
  125. /**
  126. * Executes FOP with the given ClassLoader setup.
  127. * @param args command-line arguments
  128. */
  129. public static void startFOP(String[] args) {
  130. //System.out.println("static CCL: "
  131. // + Thread.currentThread().getContextClassLoader().toString());
  132. //System.out.println("static CL: " + Fop.class.getClassLoader().toString());
  133. CommandLineOptions options = null;
  134. FOUserAgent foUserAgent = null;
  135. BufferedOutputStream bos = null;
  136. try {
  137. options = new CommandLineOptions();
  138. options.parse(args);
  139. foUserAgent = options.getFOUserAgent();
  140. Fop fop = null;
  141. String outputFormat = options.getOutputFormat();
  142. if (!MimeConstants.MIME_XSL_FO.equals(outputFormat)) {
  143. fop = new Fop(outputFormat, foUserAgent);
  144. }
  145. try {
  146. if (options.getOutputFile() != null) {
  147. bos = new BufferedOutputStream(new FileOutputStream(
  148. options.getOutputFile()));
  149. if (fop != null) {
  150. fop.setOutputStream(bos);
  151. foUserAgent.setOutputFile(options.getOutputFile());
  152. }
  153. }
  154. if (fop != null) {
  155. options.getInputHandler().render(fop);
  156. } else {
  157. options.getInputHandler().transformTo(bos);
  158. }
  159. } finally {
  160. if (bos != null) {
  161. bos.close();
  162. }
  163. }
  164. // System.exit(0) called to close AWT/SVG-created threads, if any.
  165. // AWTRenderer closes with window shutdown, so exit() should not
  166. // be called here
  167. if (!MimeConstants.MIME_FOP_AWT_PREVIEW.equals(outputFormat)) {
  168. System.exit(0);
  169. }
  170. } catch (Exception e) {
  171. if (options != null) {
  172. options.getLogger().error("Exception", e);
  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. }