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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. /*
  2. * $Id$
  3. * ============================================================================
  4. * The Apache Software License, Version 1.1
  5. * ============================================================================
  6. *
  7. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modifica-
  10. * tion, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if any, must
  20. * include the following acknowledgment: "This product includes software
  21. * developed by the Apache Software Foundation (http://www.apache.org/)."
  22. * Alternately, this acknowledgment may appear in the software itself, if
  23. * and wherever such third-party acknowledgments normally appear.
  24. *
  25. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  26. * endorse or promote products derived from this software without prior
  27. * written permission. For written permission, please contact
  28. * apache@apache.org.
  29. *
  30. * 5. Products derived from this software may not be called "Apache", nor may
  31. * "Apache" appear in their name, without prior written permission of the
  32. * Apache Software Foundation.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  37. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  38. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  39. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. * ============================================================================
  45. *
  46. * This software consists of voluntary contributions made by many individuals
  47. * on behalf of the Apache Software Foundation and was originally created by
  48. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  49. * Software Foundation, please see <http://www.apache.org/>.
  50. */
  51. package org.apache.fop.apps;
  52. // FOP
  53. import org.apache.fop.area.AreaTree;
  54. import org.apache.fop.area.RenderPagesModel;
  55. import org.apache.fop.fo.ElementMapping;
  56. import org.apache.fop.fo.FOTreeBuilder;
  57. import org.apache.fop.fo.FOInputHandler;
  58. import org.apache.fop.fo.FOTreeHandler;
  59. import org.apache.fop.render.mif.MIFHandler;
  60. import org.apache.fop.render.Renderer;
  61. import org.apache.fop.render.awt.AWTRenderer;
  62. import org.apache.fop.rtf.renderer.RTFHandler;
  63. import org.apache.fop.tools.DocumentInputSource;
  64. import org.apache.fop.tools.DocumentReader;
  65. import org.apache.fop.layoutmgr.LayoutManagerLS;
  66. // Avalon
  67. import org.apache.avalon.framework.logger.ConsoleLogger;
  68. import org.apache.avalon.framework.logger.LogEnabled;
  69. import org.apache.avalon.framework.logger.Logger;
  70. // DOM
  71. /* org.w3c.dom.Document is not imported to reduce confusion with
  72. org.apache.fop.control.Document */
  73. // SAX
  74. import org.xml.sax.ContentHandler;
  75. import org.xml.sax.InputSource;
  76. import org.xml.sax.SAXException;
  77. import org.xml.sax.XMLReader;
  78. // Java
  79. import java.io.IOException;
  80. import java.io.OutputStream;
  81. /**
  82. * Primary class that drives overall FOP process.
  83. * <P>
  84. * The simplest way to use this is to instantiate it with the
  85. * InputSource and OutputStream, then set the renderer desired, and
  86. * calling run();
  87. * <P>
  88. * Here is an example use of Driver which outputs PDF:
  89. *
  90. * <PRE>
  91. * Driver driver = new Driver(new InputSource (args[0]),
  92. * new FileOutputStream(args[1]));
  93. * driver.enableLogging(myLogger); //optional
  94. * driver.setRenderer(RENDER_PDF);
  95. * driver.run();
  96. * </PRE>
  97. * If neccessary, calling classes can call into the lower level
  98. * methods to setup and
  99. * render. Methods can be called to set the
  100. * Renderer to use, the (possibly multiple) ElementMapping(s) to
  101. * use and the OutputStream to use to output the results of the
  102. * rendering (where applicable). In the case of the Renderer and
  103. * ElementMapping(s), the Driver may be supplied either with the
  104. * object itself, or the name of the class, in which case Driver will
  105. * instantiate the class itself. The advantage of the latter is it
  106. * enables runtime determination of Renderer and ElementMapping(s).
  107. * <P>
  108. * Once the Driver is set up, the render method
  109. * is called. Depending on whether DOM or SAX is being used, the
  110. * invocation of the method is either render(Document) or
  111. * buildFOTree(Parser, InputSource) respectively.
  112. * <P>
  113. * A third possibility may be used to build the FO Tree, namely
  114. * calling getContentHandler() and firing the SAX events yourself.
  115. * <P>
  116. * Once the FO Tree is built, the format() and render() methods may be
  117. * called in that order.
  118. * <P>
  119. * Here is an example use of Driver which outputs to AWT:
  120. *
  121. * <PRE>
  122. * Driver driver = new Driver();
  123. * driver.enableLogging(myLogger); //optional
  124. * driver.setRenderer(new org.apache.fop.render.awt.AWTRenderer(translator));
  125. * driver.render(parser, fileInputSource(args[0]));
  126. * </PRE>
  127. */
  128. public class Driver implements LogEnabled {
  129. /**
  130. * private constant to indicate renderer was not defined.
  131. */
  132. private static final int NOT_SET = 0;
  133. /**
  134. * Render to PDF. OutputStream must be set
  135. */
  136. public static final int RENDER_PDF = 1;
  137. /**
  138. * Render to a GUI window. No OutputStream neccessary
  139. */
  140. public static final int RENDER_AWT = 2;
  141. /**
  142. * Render to MIF. OutputStream must be set
  143. */
  144. public static final int RENDER_MIF = 3;
  145. /**
  146. * Render to XML. OutputStream must be set
  147. */
  148. public static final int RENDER_XML = 4;
  149. /**
  150. * Render to PRINT. No OutputStream neccessary
  151. */
  152. public static final int RENDER_PRINT = 5;
  153. /**
  154. * Render to PCL. OutputStream must be set
  155. */
  156. public static final int RENDER_PCL = 6;
  157. /**
  158. * Render to Postscript. OutputStream must be set
  159. */
  160. public static final int RENDER_PS = 7;
  161. /**
  162. * Render to Text. OutputStream must be set
  163. */
  164. public static final int RENDER_TXT = 8;
  165. /**
  166. * Render to SVG. OutputStream must be set
  167. */
  168. public static final int RENDER_SVG = 9;
  169. /**
  170. * Render to RTF. OutputStream must be set
  171. */
  172. public static final int RENDER_RTF = 10;
  173. /**
  174. * the FO tree builder
  175. */
  176. private FOTreeBuilder treeBuilder;
  177. /**
  178. * the renderer type code given by setRenderer
  179. */
  180. private int rendererType = NOT_SET;
  181. /**
  182. * the renderer to use to output the area tree
  183. */
  184. private Renderer renderer;
  185. /**
  186. * the SAX ContentHandler
  187. */
  188. private FOInputHandler foInputHandler;
  189. /**
  190. * the source of the FO file
  191. */
  192. private InputSource source;
  193. /**
  194. * the stream to use to output the results of the renderer
  195. */
  196. private OutputStream stream;
  197. /**
  198. * The XML parser to use when building the FO tree
  199. */
  200. private XMLReader reader;
  201. /**
  202. * the system resources that FOP will use
  203. */
  204. private Logger log = null;
  205. private FOUserAgent userAgent = null;
  206. private Document currentDocument = null;
  207. /**
  208. * Main constructor for the Driver class.
  209. */
  210. public Driver() {
  211. stream = null;
  212. }
  213. /**
  214. * Convenience constructor for directly setting input and output.
  215. * @param source InputSource to take the XSL-FO input from
  216. * @param stream Target output stream
  217. */
  218. public Driver(InputSource source, OutputStream stream) {
  219. this();
  220. this.source = source;
  221. this.stream = stream;
  222. }
  223. private boolean isInitialized() {
  224. return (treeBuilder != null);
  225. }
  226. /**
  227. * Initializes the Driver object.
  228. */
  229. public void initialize() {
  230. if (isInitialized()) {
  231. throw new IllegalStateException("Driver already initialized");
  232. }
  233. treeBuilder = new FOTreeBuilder();
  234. treeBuilder.setUserAgent(getUserAgent());
  235. }
  236. /**
  237. * Optionally sets the FOUserAgent instance for FOP to use. The Driver
  238. * class sets up its own FOUserAgent if none is set through this method.
  239. * @param agent FOUserAgent to use
  240. */
  241. public void setUserAgent(FOUserAgent agent) {
  242. userAgent = agent;
  243. }
  244. protected FOUserAgent getUserAgent() {
  245. if (userAgent == null) {
  246. userAgent = new FOUserAgent();
  247. userAgent.enableLogging(getLogger());
  248. userAgent.setBaseURL("");
  249. }
  250. return userAgent;
  251. }
  252. /**
  253. * Provide the Driver instance with a logger. More information on Avalon
  254. * logging can be found at the
  255. * <a href="http://avalon.apache.org">Avalon site</a>.
  256. *
  257. * @param log the logger. Must not be <code>null</code>.
  258. * @see org.apache.avalon.framework.logger.LogEnabled#enableLogging(Logger)
  259. */
  260. public void enableLogging(Logger log) {
  261. if (this.log == null) {
  262. this.log = log;
  263. } else {
  264. getLogger().warn("Logger is already set! Won't use the new logger.");
  265. }
  266. }
  267. /**
  268. * Provide the Driver instance with a logger.
  269. * @param log the logger. Must not be <code>null</code>.
  270. * @deprecated Use #enableLogging(Logger) instead.
  271. */
  272. public void setLogger(Logger log) {
  273. enableLogging(log);
  274. }
  275. /**
  276. * Returns the logger for use by FOP.
  277. * @return the logger
  278. * @see #enableLogging(Logger)
  279. */
  280. public Logger getLogger() {
  281. if (this.log == null) {
  282. // use ConsoleLogger as default when logger not explicitly set
  283. this.log = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
  284. }
  285. return this.log;
  286. }
  287. /**
  288. * Resets the Driver so it can be reused. Property and element
  289. * mappings are reset to defaults.
  290. * The output stream is cleared. The renderer is cleared.
  291. */
  292. public synchronized void reset() {
  293. source = null;
  294. stream = null;
  295. reader = null;
  296. if (treeBuilder != null) {
  297. treeBuilder.reset();
  298. }
  299. }
  300. /**
  301. * Indicates whether FOP has already received input data.
  302. * @return true, if input data was received
  303. */
  304. public boolean hasData() {
  305. return (treeBuilder.hasData());
  306. }
  307. /**
  308. * Set the OutputStream to use to output the result of the Renderer
  309. * (if applicable)
  310. * @param stream the stream to output the result of rendering to
  311. */
  312. public void setOutputStream(OutputStream stream) {
  313. this.stream = stream;
  314. }
  315. private void validateOutputStream() {
  316. if (this.stream == null) {
  317. throw new IllegalStateException("OutputStream has not been set");
  318. }
  319. }
  320. /**
  321. * Set the source for the FO document. This can be a normal SAX
  322. * InputSource, or an DocumentInputSource containing a DOM document.
  323. * @see DocumentInputSource
  324. */
  325. public void setInputSource(InputSource source) {
  326. this.source = source;
  327. }
  328. /**
  329. * Sets the reader used when reading in the source. If not set,
  330. * this defaults to a basic SAX parser.
  331. * @param reader the reader to use.
  332. */
  333. public void setXMLReader(XMLReader reader) {
  334. this.reader = reader;
  335. }
  336. /**
  337. * Shortcut to set the rendering type to use. Must be one of
  338. * <ul>
  339. * <li>RENDER_PDF</li>
  340. * <li>RENDER_AWT</li>
  341. * <li>RENDER_PRINT</li>
  342. * <li>RENDER_MIF</li>
  343. * <li>RENDER_XML</li>
  344. * <li>RENDER_PCL</li>
  345. * <li>RENDER_PS</li>
  346. * <li>RENDER_TXT</li>
  347. * <li>RENDER_SVG</li>
  348. * <li>RENDER_RTF</li>
  349. * </ul>
  350. * @param renderer the type of renderer to use
  351. * @throws IllegalArgumentException if an unsupported renderer type was required.
  352. */
  353. public void setRenderer(int renderer) throws IllegalArgumentException {
  354. rendererType = renderer;
  355. switch (renderer) {
  356. case RENDER_PDF:
  357. setRenderer("org.apache.fop.render.pdf.PDFRenderer");
  358. break;
  359. case RENDER_AWT:
  360. throw new IllegalArgumentException("Use renderer form of setRenderer() for AWT");
  361. case RENDER_PRINT:
  362. setRenderer("org.apache.fop.render.awt.AWTPrintRenderer");
  363. break;
  364. case RENDER_PCL:
  365. setRenderer("org.apache.fop.render.pcl.PCLRenderer");
  366. break;
  367. case RENDER_PS:
  368. setRenderer("org.apache.fop.render.ps.PSRenderer");
  369. break;
  370. case RENDER_TXT:
  371. setRenderer("org.apache.fop.render.txt.TXTRenderer()");
  372. break;
  373. case RENDER_MIF:
  374. //foInputHandler will be set later
  375. break;
  376. case RENDER_XML:
  377. setRenderer("org.apache.fop.render.xml.XMLRenderer");
  378. break;
  379. case RENDER_SVG:
  380. setRenderer("org.apache.fop.render.svg.SVGRenderer");
  381. break;
  382. case RENDER_RTF:
  383. //foInputHandler will be set later
  384. break;
  385. default:
  386. rendererType = NOT_SET;
  387. throw new IllegalArgumentException("Unknown renderer type " + renderer);
  388. }
  389. }
  390. /**
  391. * Set the Renderer to use.
  392. * @param renderer the renderer instance to use (Note: Logger must be set at this point)
  393. */
  394. public void setRenderer(Renderer renderer) {
  395. // AWTStarter calls this function directly
  396. if (renderer instanceof AWTRenderer) {
  397. rendererType = RENDER_AWT;
  398. }
  399. renderer.setProducer(Version.getVersion());
  400. renderer.setUserAgent(getUserAgent());
  401. this.renderer = renderer;
  402. }
  403. /**
  404. * Returns the currently active renderer.
  405. * @return the renderer
  406. */
  407. public Renderer getRenderer() {
  408. return renderer;
  409. }
  410. /**
  411. * Set the class name of the Renderer to use as well as the
  412. * producer string for those renderers that can make use of it.
  413. * @param rendererClassName classname of the renderer to use such as
  414. * "org.apache.fop.render.pdf.PDFRenderer"
  415. * @exception IllegalArgumentException if the classname was invalid.
  416. * @see #setRenderer(int)
  417. */
  418. public void setRenderer(String rendererClassName)
  419. throws IllegalArgumentException {
  420. try {
  421. renderer =
  422. (Renderer)Class.forName(rendererClassName).newInstance();
  423. if (renderer instanceof LogEnabled) {
  424. ((LogEnabled)renderer).enableLogging(getLogger());
  425. }
  426. renderer.setProducer(Version.getVersion());
  427. renderer.setUserAgent(getUserAgent());
  428. } catch (ClassNotFoundException e) {
  429. throw new IllegalArgumentException("Could not find "
  430. + rendererClassName);
  431. } catch (InstantiationException e) {
  432. throw new IllegalArgumentException("Could not instantiate "
  433. + rendererClassName);
  434. } catch (IllegalAccessException e) {
  435. throw new IllegalArgumentException("Could not access "
  436. + rendererClassName);
  437. } catch (ClassCastException e) {
  438. throw new IllegalArgumentException(rendererClassName
  439. + " is not a renderer");
  440. }
  441. }
  442. /**
  443. * Add the given element mapping.
  444. * An element mapping maps element names to Java classes.
  445. *
  446. * @param mapping the element mappingto add
  447. */
  448. public void addElementMapping(ElementMapping mapping) {
  449. treeBuilder.addElementMapping(mapping);
  450. }
  451. /**
  452. * Add the element mapping with the given class name.
  453. * @param mappingClassName the class name representing the element mapping.
  454. */
  455. public void addElementMapping(String mappingClassName) {
  456. treeBuilder.addElementMapping(mappingClassName);
  457. }
  458. /**
  459. * Determines which SAX ContentHandler is appropriate for the rendererType.
  460. * Structure renderers (e.g. MIF & RTF) each have a specialized
  461. * ContentHandler that directly place data into the output stream. Layout
  462. * renderers (e.g. PDF & PostScript) use a ContentHandler that builds an FO
  463. * Tree.
  464. * @return a SAX ContentHandler for handling the SAX events.
  465. */
  466. public ContentHandler getContentHandler() {
  467. if (!isInitialized()) {
  468. initialize();
  469. }
  470. if (rendererType != RENDER_PRINT && rendererType != RENDER_AWT) {
  471. validateOutputStream();
  472. }
  473. // TODO: - do this stuff in a better way
  474. // PIJ: I guess the structure handler should be created by the renderer.
  475. if (rendererType == RENDER_MIF) {
  476. foInputHandler = new MIFHandler(currentDocument, stream);
  477. } else if (rendererType == RENDER_RTF) {
  478. foInputHandler = new RTFHandler(currentDocument, stream);
  479. } else {
  480. if (renderer == null) {
  481. throw new IllegalStateException(
  482. "Renderer not set when using standard foInputHandler");
  483. }
  484. foInputHandler = new FOTreeHandler(currentDocument, true);
  485. }
  486. foInputHandler.enableLogging(getLogger());
  487. treeBuilder.setUserAgent(getUserAgent());
  488. treeBuilder.setFOInputHandler(foInputHandler);
  489. return treeBuilder;
  490. }
  491. /**
  492. * Render the FO document read by a SAX Parser from an InputHandler
  493. * @param inputHandler the input handler containing the source and
  494. * parser information.
  495. * @throws FOPException if anything goes wrong.
  496. */
  497. public synchronized void render(InputHandler inputHandler)
  498. throws FOPException {
  499. XMLReader parser = inputHandler.getParser();
  500. render(parser, inputHandler.getInputSource());
  501. }
  502. /**
  503. * This is the main render() method. The other render() methods are for
  504. * convenience, and normalize to this form, then run this.
  505. * Renders the FO document read by a SAX Parser from an InputSource.
  506. * @param parser the SAX parser.
  507. * @param source the input source the parser reads from.
  508. * @throws FOPException if anything goes wrong.
  509. */
  510. public synchronized void render(XMLReader parser, InputSource source)
  511. throws FOPException {
  512. if (!isInitialized()) {
  513. initialize();
  514. }
  515. /** Document creation is hard-wired for now, but needs to be made
  516. accessible through the API and/or configuration */
  517. if (currentDocument == null) {
  518. currentDocument = new Document(this);
  519. }
  520. parser.setContentHandler(getContentHandler());
  521. currentDocument.foInputHandler = foInputHandler;
  522. /** LayoutStrategy is hard-wired for now, but needs to be made
  523. accessible through the API and/or configuration */
  524. if (foInputHandler instanceof FOTreeHandler) {
  525. if (currentDocument.getLayoutStrategy() == null) {
  526. currentDocument.setLayoutStrategy(new LayoutManagerLS(currentDocument));
  527. }
  528. }
  529. treeBuilder.foTreeControl = currentDocument;
  530. try {
  531. if (foInputHandler instanceof FOTreeHandler) {
  532. FOTreeHandler foTreeHandler = (FOTreeHandler)foInputHandler;
  533. foTreeHandler.addFOTreeListener(currentDocument);
  534. currentDocument.areaTree = new AreaTree(currentDocument);
  535. currentDocument.atModel = new RenderPagesModel(renderer);
  536. //this.atModel = new CachedRenderPagesModel(renderer);
  537. currentDocument.areaTree.setTreeModel(currentDocument.atModel);
  538. try {
  539. renderer.setupFontInfo(currentDocument);
  540. // check that the "any,normal,400" font exists
  541. if (!currentDocument.isSetupValid()) {
  542. throw new SAXException(new FOPException(
  543. "No default font defined by OutputConverter"));
  544. }
  545. renderer.startRenderer(stream);
  546. } catch (IOException e) {
  547. throw new SAXException(e);
  548. }
  549. }
  550. /**
  551. The following statement triggers virtually all of the processing
  552. for this document. The SAX parser fires events that are handled by
  553. the appropriate InputHandler object, which means that you will need
  554. to look in those objects to see where FOP picks up control of
  555. processing again. For Structure Renderers (e.g. MIF & RTF), the SAX
  556. events are handled directly. For Layout Renderers (e.g. PDF &
  557. PostScript), an FO Tree is built by the FOTreeHandler, which in
  558. turn fires events when a PageSequence object is complete. This
  559. allows higher-level control objects (such as this class) to work
  560. directly with PageSequence objects. See foPageSequenceComplete()
  561. where this level of control is implemented.
  562. */
  563. parser.parse(source);
  564. if (foInputHandler instanceof FOTreeHandler) {
  565. FOTreeHandler foTreeHandler = (FOTreeHandler)foInputHandler;
  566. foTreeHandler.removeFOTreeListener(currentDocument);
  567. }
  568. } catch (SAXException e) {
  569. if (e.getException() instanceof FOPException) {
  570. // Undo exception tunneling.
  571. throw (FOPException)e.getException();
  572. } else {
  573. throw new FOPException(e);
  574. }
  575. } catch (IOException e) {
  576. throw new FOPException(e);
  577. }
  578. }
  579. /**
  580. * This method overloads the main render() method, adding the convenience
  581. * of using a DOM Document as input.
  582. * @see #render(XMLReader, InputSource)
  583. * @param document the DOM document to read from
  584. * @throws FOPException if anything goes wrong.
  585. */
  586. public synchronized void render(org.w3c.dom.Document document)
  587. throws FOPException {
  588. DocumentInputSource source = new DocumentInputSource(document);
  589. DocumentReader reader = new DocumentReader();
  590. render(reader, source);
  591. }
  592. /**
  593. * Runs the formatting and renderering process using the previously set
  594. * parser, input source, renderer and output stream.
  595. * If the renderer was not set, default to PDF.
  596. * If no parser was set, and the input source is not a dom document,
  597. * get a default SAX parser.
  598. * @throws IOException in case of IO errors.
  599. * @throws FOPException if anything else goes wrong.
  600. */
  601. public synchronized void run() throws IOException, FOPException {
  602. if (!isInitialized()) {
  603. initialize();
  604. }
  605. if (renderer == null && rendererType != RENDER_RTF
  606. && rendererType != RENDER_MIF) {
  607. setRenderer(RENDER_PDF);
  608. }
  609. if (source == null) {
  610. throw new FOPException("InputSource is not set.");
  611. }
  612. if (reader == null) {
  613. if (!(source instanceof DocumentInputSource)) {
  614. reader = FOFileHandler.createParser();
  615. }
  616. }
  617. if (source instanceof DocumentInputSource) {
  618. render(((DocumentInputSource)source).getDocument());
  619. } else {
  620. render(reader, source);
  621. }
  622. }
  623. /**
  624. * Public accessor for setting the currentDocument to process.
  625. * @param document the Document object that should be processed.
  626. */
  627. public void setCurrentDocument(Document document) {
  628. currentDocument = document;
  629. }
  630. /**
  631. * Public accessor for getting the currentDocument
  632. * @return the currentDocument
  633. */
  634. public Document getCurrentDocument() {
  635. return currentDocument;
  636. }
  637. }