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.

Fop.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Copyright 1999-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.apps;
  18. // Java
  19. import java.io.BufferedOutputStream;
  20. import java.io.File;
  21. import java.io.FileFilter;
  22. import java.io.FileOutputStream;
  23. import java.io.OutputStream;
  24. import java.lang.reflect.Method;
  25. import java.net.MalformedURLException;
  26. import java.net.URL;
  27. import java.util.List;
  28. // XML
  29. import org.xml.sax.helpers.DefaultHandler;
  30. // FOP
  31. import org.apache.fop.fo.Constants;
  32. import org.apache.fop.fo.FOTreeBuilder;
  33. /**
  34. * Primary class that activates the FOP process for both command line
  35. * and embedded usage.
  36. * <P>
  37. * JAXP is the standard method of embedding FOP in Java programs.
  38. * Please check our embedding page (http://xml.apache.org/fop/embedding.html)
  39. * for samples (these are also available within the distribution in
  40. * FOP_DIR\examples\embedding)
  41. * <P>
  42. * Methods within FOUserAgent are available to customize portions of the
  43. * process. For example, a specific Renderer object can be specified,
  44. * also ElementMappings which determine elements in the FO that can be
  45. * processed) can be added.
  46. */
  47. public class Fop implements Constants {
  48. // desired output type: RENDER_PDF, RENDER_PS, etc.
  49. private int renderType = NOT_SET;
  50. // output stream to send results to
  51. private OutputStream stream = null;
  52. // FOUserAgent object to set processing options
  53. private FOUserAgent foUserAgent = null;
  54. /**
  55. * Constructor for use with already-created FOUserAgents
  56. * @param renderType the type of renderer to use. Must be one of
  57. * <ul>
  58. * <li>Fop.RENDER_PDF</li>
  59. * <li>Fop.RENDER_AWT</li>
  60. * <li>Fop.RENDER_PRINT</li>
  61. * <li>Fop.RENDER_MIF</li>
  62. * <li>Fop.RENDER_XML</li>
  63. * <li>Fop.RENDER_PCL</li>
  64. * <li>Fop.RENDER_PS</li>
  65. * <li>Fop.RENDER_TXT</li>
  66. * <li>Fop.RENDER_SVG</li>
  67. * <li>Fop.RENDER_RTF</li>
  68. * <li>Fop.RENDER_TIFF</li>
  69. * <li>Fop.RENDER_PNG</li>
  70. * </ul>
  71. * @param ua FOUserAgent object
  72. * @throws IllegalArgumentException if an unsupported renderer type was requested.
  73. */
  74. public Fop(int renderType, FOUserAgent ua) {
  75. if (renderType < Constants.RENDER_MIN_CONST
  76. || renderType > Constants.RENDER_MAX_CONST) {
  77. throw new IllegalArgumentException(
  78. "Invalid render type #" + renderType);
  79. }
  80. this.renderType = renderType;
  81. foUserAgent = ua;
  82. if (foUserAgent == null) {
  83. foUserAgent = new FOUserAgent();
  84. }
  85. }
  86. /**
  87. * Constructor that creates a default FOUserAgent
  88. * @see org.apache.fop.apps.Fop#(int, FOUserAgent)
  89. */
  90. public Fop(int renderType) {
  91. this(renderType, new FOUserAgent());
  92. }
  93. /**
  94. * Get the FOUserAgent instance for this process
  95. * @return the user agent
  96. */
  97. public FOUserAgent getUserAgent() {
  98. return foUserAgent;
  99. }
  100. /**
  101. * Set the OutputStream to use to output the result of the Render
  102. * (if applicable)
  103. * @param stream the stream to output the result of rendering to
  104. */
  105. public void setOutputStream(OutputStream stream) {
  106. this.stream = stream;
  107. }
  108. /**
  109. * Returns a DefaultHandler object used to generate the document.
  110. * Note this object implements the ContentHandler interface.
  111. * For processing with a Transformer object, this DefaultHandler object
  112. * can be used in the SAXResult constructor.
  113. * Alternatively, for processing with a SAXParser, this object can be
  114. * used as the DefaultHandler argument to its parse() methods.
  115. *
  116. * @return a SAX DefaultHandler for handling the SAX events.
  117. * @throws FOPException if setting up the DefaultHandler fails
  118. */
  119. public DefaultHandler getDefaultHandler() throws FOPException {
  120. return new FOTreeBuilder(renderType, foUserAgent, stream);
  121. }
  122. /**
  123. * @return the list of URLs to all libraries.
  124. * @throws MalformedURLException In case there is a problem converting java.io.File
  125. * instances to URLs.
  126. */
  127. public static URL[] getJARList() throws MalformedURLException {
  128. File baseDir = new File(".").getAbsoluteFile().getParentFile();
  129. File buildDir;
  130. if ("build".equals(baseDir.getName())) {
  131. buildDir = baseDir;
  132. baseDir = baseDir.getParentFile();
  133. } else {
  134. buildDir = new File(baseDir, "build");
  135. }
  136. File fopJar = new File(buildDir, "fop.jar");
  137. if (!fopJar.exists()) {
  138. fopJar = new File(baseDir, "fop.jar");
  139. }
  140. if (!fopJar.exists()) {
  141. throw new RuntimeException("fop.jar not found in directory: "
  142. + baseDir.getAbsolutePath() + " (or below)");
  143. }
  144. List jars = new java.util.ArrayList();
  145. jars.add(fopJar.toURL());
  146. File[] files;
  147. FileFilter filter = new FileFilter() {
  148. public boolean accept(File pathname) {
  149. return pathname.getName().endsWith(".jar");
  150. }
  151. };
  152. File libDir = new File(baseDir, "lib");
  153. if (!libDir.exists()) {
  154. libDir = baseDir;
  155. }
  156. files = libDir.listFiles(filter);
  157. if (files != null) {
  158. for (int i = 0, size = files.length; i < size; i++) {
  159. jars.add(files[i].toURL());
  160. }
  161. }
  162. String optionalLib = System.getProperty("fop.optional.lib");
  163. if (optionalLib != null) {
  164. files = new File(optionalLib).listFiles(filter);
  165. if (files != null) {
  166. for (int i = 0, size = files.length; i < size; i++) {
  167. jars.add(files[i].toURL());
  168. }
  169. }
  170. }
  171. URL[] urls = (URL[])jars.toArray(new URL[jars.size()]);
  172. /*
  173. for (int i = 0, c = urls.length; i < c; i++) {
  174. System.out.println(urls[i]);
  175. }*/
  176. return urls;
  177. }
  178. /**
  179. * @return true if FOP's dependecies are available in the current ClassLoader setup.
  180. */
  181. public static boolean checkDependencies() {
  182. try {
  183. //System.out.println(Thread.currentThread().getContextClassLoader());
  184. Class clazz = Class.forName("org.apache.batik.Version");
  185. if (clazz != null) {
  186. clazz = Class.forName("org.apache.avalon.framework.configuration.Configuration");
  187. }
  188. return (clazz != null);
  189. } catch (Exception e) {
  190. return false;
  191. }
  192. }
  193. /**
  194. * Dynamically builds a ClassLoader and executes FOP.
  195. * @param args command-line arguments
  196. */
  197. public static void startFOPWithDynamicClasspath(String[] args) {
  198. try {
  199. URL[] urls = getJARList();
  200. //System.out.println("CCL: "
  201. // + Thread.currentThread().getContextClassLoader().toString());
  202. ClassLoader loader = new java.net.URLClassLoader(urls, null);
  203. Thread.currentThread().setContextClassLoader(loader);
  204. Class clazz = Class.forName("org.apache.fop.apps.Fop", true, loader);
  205. //System.out.println("CL: " + clazz.getClassLoader().toString());
  206. Method mainMethod = clazz.getMethod("startFOP", new Class[] {String[].class});
  207. mainMethod.invoke(null, new Object[] {args});
  208. } catch (Exception e) {
  209. System.err.println("Unable to start FOP:");
  210. e.printStackTrace();
  211. System.exit(-1);
  212. }
  213. }
  214. /**
  215. * Executes FOP with the given ClassLoader setup.
  216. * @param args command-line arguments
  217. */
  218. public static void startFOP(String[] args) {
  219. //System.out.println("static CCL: "
  220. // + Thread.currentThread().getContextClassLoader().toString());
  221. //System.out.println("static CL: " + Fop.class.getClassLoader().toString());
  222. CommandLineOptions options = null;
  223. FOUserAgent foUserAgent = null;
  224. BufferedOutputStream bos = null;
  225. try {
  226. options = new CommandLineOptions();
  227. options.parse(args);
  228. foUserAgent = options.getFOUserAgent();
  229. Fop fop = new Fop(options.getRenderer(), foUserAgent);
  230. try {
  231. if (options.getOutputFile() != null) {
  232. bos = new BufferedOutputStream(new FileOutputStream(
  233. options.getOutputFile()));
  234. fop.setOutputStream(bos);
  235. foUserAgent.setOutputFile(options.getOutputFile());
  236. }
  237. foUserAgent.getInputHandler().render(fop);
  238. } finally {
  239. if (bos != null) {
  240. bos.close();
  241. }
  242. }
  243. // System.exit(0) called to close AWT/SVG-created threads, if any.
  244. // AWTRenderer closes with window shutdown, so exit() should not
  245. // be called here
  246. if (options.getOutputMode() != CommandLineOptions.RENDER_AWT) {
  247. System.exit(0);
  248. }
  249. } catch (Exception e) {
  250. if (options != null) {
  251. options.getLogger().error("Exception", e);
  252. }
  253. System.exit(1);
  254. }
  255. }
  256. /**
  257. * The main routine for the command line interface
  258. * @param args the command line parameters
  259. */
  260. public static void main(String[] args) {
  261. if (checkDependencies()) {
  262. startFOP(args);
  263. } else {
  264. startFOPWithDynamicClasspath(args);
  265. }
  266. }
  267. /**
  268. * Get the version of FOP
  269. * @return the version string
  270. */
  271. public static String getVersion() {
  272. return "1.0dev";
  273. }
  274. }