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.

Driver.java 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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.fo.ElementMapping;
  54. import org.apache.fop.fo.FOTreeBuilder;
  55. import org.apache.fop.fo.FOUserAgent;
  56. import org.apache.fop.render.Renderer;
  57. import org.apache.fop.tools.DocumentInputSource;
  58. import org.apache.fop.tools.DocumentReader;
  59. // Avalon
  60. import org.apache.avalon.framework.logger.ConsoleLogger;
  61. import org.apache.avalon.framework.logger.LogEnabled;
  62. import org.apache.avalon.framework.logger.Logger;
  63. // DOM
  64. import org.w3c.dom.Document;
  65. // SAX
  66. import org.xml.sax.ContentHandler;
  67. import org.xml.sax.InputSource;
  68. import org.xml.sax.SAXException;
  69. import org.xml.sax.XMLReader;
  70. import javax.xml.parsers.ParserConfigurationException;
  71. import javax.xml.parsers.SAXParserFactory;
  72. // Java
  73. import java.io.BufferedReader;
  74. import java.io.IOException;
  75. import java.io.InputStream;
  76. import java.io.InputStreamReader;
  77. import java.io.OutputStream;
  78. import java.io.Reader;
  79. import java.util.Enumeration;
  80. import java.util.Iterator;
  81. import java.util.List;
  82. import java.util.Map;
  83. /**
  84. * Primary class that drives overall FOP process.
  85. * <P>
  86. * The simplest way to use this is to instantiate it with the
  87. * InputSource and OutputStream, then set the renderer desired, and
  88. * calling run();
  89. * <P>
  90. * Here is an example use of Driver which outputs PDF:
  91. *
  92. * <PRE>
  93. * Driver driver = new Driver(new InputSource (args[0]),
  94. * new FileOutputStream(args[1]));
  95. * driver.enableLogging(myLogger); //optional
  96. * driver.setRenderer(RENDER_PDF);
  97. * driver.run();
  98. * </PRE>
  99. * If neccessary, calling classes can call into the lower level
  100. * methods to setup and
  101. * render. Methods can be called to set the
  102. * Renderer to use, the (possibly multiple) ElementMapping(s) to
  103. * use and the OutputStream to use to output the results of the
  104. * rendering (where applicable). In the case of the Renderer and
  105. * ElementMapping(s), the Driver may be supplied either with the
  106. * object itself, or the name of the class, in which case Driver will
  107. * instantiate the class itself. The advantage of the latter is it
  108. * enables runtime determination of Renderer and ElementMapping(s).
  109. * <P>
  110. * Once the Driver is set up, the render method
  111. * is called. Depending on whether DOM or SAX is being used, the
  112. * invocation of the method is either render(Document) or
  113. * buildFOTree(Parser, InputSource) respectively.
  114. * <P>
  115. * A third possibility may be used to build the FO Tree, namely
  116. * calling getContentHandler() and firing the SAX events yourself.
  117. * <P>
  118. * Once the FO Tree is built, the format() and render() methods may be
  119. * called in that order.
  120. * <P>
  121. * Here is an example use of Driver which outputs to AWT:
  122. *
  123. * <PRE>
  124. * Driver driver = new Driver();
  125. * driver.enableLogging(myLogger); //optional
  126. * driver.setRenderer(new org.apache.fop.render.awt.AWTRenderer(translator));
  127. * driver.render(parser, fileInputSource(args[0]));
  128. * </PRE>
  129. *
  130. * @deprecated This class is replaced by {@link Session}. See {@link
  131. * CommandLineStarter#run for a usage example.
  132. */
  133. public class Driver implements LogEnabled {
  134. /**
  135. * Render to PDF. OutputStream must be set
  136. */
  137. public static final int RENDER_PDF = 1;
  138. /**
  139. * Render to a GUI window. No OutputStream neccessary
  140. */
  141. public static final int RENDER_AWT = 2;
  142. /**
  143. * Render to MIF. OutputStream must be set
  144. */
  145. public static final int RENDER_MIF = 3;
  146. /**
  147. * Render to XML. OutputStream must be set
  148. */
  149. public static final int RENDER_XML = 4;
  150. /**
  151. * Render to PRINT. No OutputStream neccessary
  152. */
  153. public static final int RENDER_PRINT = 5;
  154. /**
  155. * Render to PCL. OutputStream must be set
  156. */
  157. public static final int RENDER_PCL = 6;
  158. /**
  159. * Render to Postscript. OutputStream must be set
  160. */
  161. public static final int RENDER_PS = 7;
  162. /**
  163. * Render to Text. OutputStream must be set
  164. */
  165. public static final int RENDER_TXT = 8;
  166. /**
  167. * Render to SVG. OutputStream must be set
  168. */
  169. public static final int RENDER_SVG = 9;
  170. /**
  171. * Render to RTF. OutputStream must be set
  172. */
  173. public static final int RENDER_RTF = 10;
  174. /**
  175. * the FO tree builder
  176. */
  177. private FOTreeBuilder treeBuilder;
  178. /**
  179. * the renderer type code given by setRenderer
  180. */
  181. private int rendererType;
  182. /**
  183. * the renderer to use to output the area tree
  184. */
  185. private Renderer renderer;
  186. /**
  187. * the structure handler
  188. */
  189. private StructureHandler structHandler;
  190. /**
  191. * the source of the FO file
  192. */
  193. private InputSource source;
  194. /**
  195. * the stream to use to output the results of the renderer
  196. */
  197. private OutputStream stream;
  198. /**
  199. * The XML parser to use when building the FO tree
  200. */
  201. private XMLReader reader;
  202. /**
  203. * the system resources that FOP will use
  204. */
  205. private Logger log = null;
  206. private FOUserAgent userAgent = null;
  207. /**
  208. * Returns the fully qualified classname of the standard XML parser for FOP
  209. * to use.
  210. * @return the XML parser classname
  211. */
  212. public static final String getParserClassName() {
  213. try {
  214. return javax.xml.parsers.SAXParserFactory.newInstance()
  215. .newSAXParser().getXMLReader().getClass().getName();
  216. } catch (javax.xml.parsers.ParserConfigurationException e) {
  217. return null;
  218. } catch (org.xml.sax.SAXException e) {
  219. return null;
  220. }
  221. }
  222. /**
  223. * Main constructor for the Driver class.
  224. */
  225. public Driver() {
  226. stream = null;
  227. }
  228. /**
  229. * Convenience constructor for directly setting input and output.
  230. * @param source InputSource to take the XSL-FO input from
  231. * @param stream Target output stream
  232. */
  233. public Driver(InputSource source, OutputStream stream) {
  234. this();
  235. this.source = source;
  236. this.stream = stream;
  237. }
  238. private boolean isInitialized() {
  239. return (treeBuilder != null);
  240. }
  241. /**
  242. * Initializes the Driver object.
  243. */
  244. public void initialize() {
  245. if (isInitialized()) {
  246. throw new IllegalStateException("Driver already initialized");
  247. }
  248. treeBuilder = new FOTreeBuilder();
  249. treeBuilder.setUserAgent(getUserAgent());
  250. setupDefaultMappings();
  251. }
  252. /**
  253. * Optionally sets the FOUserAgent instance for FOP to use. The Driver
  254. * class sets up its own FOUserAgent if none is set through this method.
  255. * @param agent FOUserAgent to use
  256. */
  257. public void setUserAgent(FOUserAgent agent) {
  258. userAgent = agent;
  259. }
  260. private FOUserAgent getUserAgent() {
  261. if (userAgent == null) {
  262. userAgent = new FOUserAgent();
  263. userAgent.enableLogging(getLogger());
  264. userAgent.setBaseURL("");
  265. }
  266. return userAgent;
  267. }
  268. /**
  269. * Provide the Driver instance with a logger. More information on Avalon
  270. * logging can be found at the
  271. * <a href="http://avalon.apache.org">Avalon site</a>.
  272. *
  273. * @param log the logger. Must not be <code>null</code>.
  274. * @see org.apache.avalon.framework.logger.LogEnabled#enableLogging(Logger)
  275. */
  276. public void enableLogging(Logger log) {
  277. if (this.log == null) {
  278. this.log = log;
  279. } else {
  280. getLogger().warn("Logger is already set! Won't use the new logger.");
  281. }
  282. }
  283. /**
  284. * Provide the Driver instance with a logger.
  285. * @param log the logger. Must not be <code>null</code>.
  286. * @deprecated Use #enableLogging(Logger) instead.
  287. */
  288. public void setLogger(Logger log) {
  289. enableLogging(log);
  290. }
  291. /**
  292. * Returns the logger for use by FOP.
  293. * @return the logger
  294. * @see #enableLogging(Logger)
  295. */
  296. protected Logger getLogger() {
  297. if (this.log == null) {
  298. this.log = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
  299. this.log.error("Logger not set. Using ConsoleLogger as default.");
  300. }
  301. return this.log;
  302. }
  303. /**
  304. * Resets the Driver so it can be reused. Property and element
  305. * mappings are reset to defaults.
  306. * The output stream is cleared. The renderer is cleared.
  307. */
  308. public synchronized void reset() {
  309. source = null;
  310. stream = null;
  311. reader = null;
  312. treeBuilder.reset();
  313. }
  314. /**
  315. * Indicates whether FOP has already received input data.
  316. * @return true, if input data was received
  317. */
  318. public boolean hasData() {
  319. return (treeBuilder.hasData());
  320. }
  321. /**
  322. * Set the OutputStream to use to output the result of the Renderer
  323. * (if applicable)
  324. * @param stream the stream to output the result of rendering to
  325. */
  326. public void setOutputStream(OutputStream stream) {
  327. this.stream = stream;
  328. }
  329. private void validateOutputStream() {
  330. if (this.stream == null) {
  331. throw new IllegalStateException("OutputStream has not been set");
  332. }
  333. }
  334. /**
  335. * Set the source for the FO document. This can be a normal SAX
  336. * InputSource, or an DocumentInputSource containing a DOM document.
  337. * @see DocumentInputSource
  338. */
  339. public void setInputSource(InputSource source) {
  340. this.source = source;
  341. }
  342. /**
  343. * Sets the reader used when reading in the source. If not set,
  344. * this defaults to a basic SAX parser.
  345. * @param reader the reader to use.
  346. */
  347. public void setXMLReader(XMLReader reader) {
  348. this.reader = reader;
  349. }
  350. /**
  351. * Sets all the element and property list mappings to their default values.
  352. *
  353. */
  354. public void setupDefaultMappings() {
  355. addElementMapping("org.apache.fop.fo.FOElementMapping");
  356. addElementMapping("org.apache.fop.svg.SVGElementMapping");
  357. addElementMapping("org.apache.fop.extensions.ExtensionElementMapping");
  358. // add mappings from available services
  359. Iterator providers =
  360. Session.providers(org.apache.fop.fo.ElementMapping.class);
  361. if (providers != null) {
  362. while (providers.hasNext()) {
  363. String str = (String)providers.next();
  364. try {
  365. addElementMapping(str);
  366. } catch (IllegalArgumentException e) {
  367. getLogger().warn("Error while adding element mapping", e);
  368. }
  369. }
  370. }
  371. }
  372. /**
  373. * Shortcut to set the rendering type to use. Must be one of
  374. * <ul>
  375. * <li>RENDER_PDF</li>
  376. * <li>RENDER_AWT</li>
  377. * <li>RENDER_MIF</li>
  378. * <li>RENDER_XML</li>
  379. * <li>RENDER_PCL</li>
  380. * <li>RENDER_PS</li>
  381. * <li>RENDER_TXT</li>
  382. * <li>RENDER_SVG</li>
  383. * <li>RENDER_RTF</li>
  384. * </ul>
  385. * @param renderer the type of renderer to use
  386. * @throws IllegalArgumentException if an unsupported renderer type was required.
  387. */
  388. public void setRenderer(int renderer) throws IllegalArgumentException {
  389. rendererType = renderer;
  390. switch (renderer) {
  391. case RENDER_PDF:
  392. setRenderer("org.apache.fop.render.pdf.PDFRenderer");
  393. break;
  394. case RENDER_AWT:
  395. throw new IllegalArgumentException("Use renderer form of setRenderer() for AWT");
  396. case RENDER_PRINT:
  397. throw new IllegalArgumentException("Use renderer form of setRenderer() for PRINT");
  398. case RENDER_PCL:
  399. setRenderer("org.apache.fop.render.pcl.PCLRenderer");
  400. break;
  401. case RENDER_PS:
  402. setRenderer("org.apache.fop.render.ps.PSRenderer");
  403. break;
  404. case RENDER_TXT:
  405. setRenderer("org.apache.fop.render.txt.TXTRenderer()");
  406. break;
  407. case RENDER_MIF:
  408. //structHandler will be set later
  409. break;
  410. case RENDER_XML:
  411. setRenderer("org.apache.fop.render.xml.XMLRenderer");
  412. break;
  413. case RENDER_SVG:
  414. setRenderer("org.apache.fop.render.svg.SVGRenderer");
  415. break;
  416. case RENDER_RTF:
  417. //structHandler will be set later
  418. break;
  419. default:
  420. throw new IllegalArgumentException("Unknown renderer type");
  421. }
  422. }
  423. /**
  424. * Set the Renderer to use.
  425. * @param renderer the renderer instance to use (Note: Logger must be set at this point)
  426. */
  427. public void setRenderer(Renderer renderer) {
  428. renderer.setUserAgent(getUserAgent());
  429. this.renderer = renderer;
  430. }
  431. /**
  432. * Returns the currently active renderer.
  433. * @return the renderer
  434. */
  435. public Renderer getRenderer() {
  436. return renderer;
  437. }
  438. /**
  439. * Sets the renderer.
  440. * @param rendererClassName the fully qualified classname of the renderer
  441. * class to use.
  442. * @param version version number
  443. * @deprecated use renderer.setProducer(version) + setRenderer(renderer) or
  444. * just setRenderer(rendererType) which will use the default producer string.
  445. * @see #setRenderer(int)
  446. * @see #setRenderer(Renderer)
  447. */
  448. public void setRenderer(String rendererClassName, String version) {
  449. setRenderer(rendererClassName);
  450. }
  451. /**
  452. * Set the class name of the Renderer to use as well as the
  453. * producer string for those renderers that can make use of it.
  454. * @param rendererClassName classname of the renderer to use such as
  455. * "org.apache.fop.render.pdf.PDFRenderer"
  456. * @exception IllegalArgumentException if the classname was invalid.
  457. * @see #setRenderer(int)
  458. */
  459. public void setRenderer(String rendererClassName)
  460. throws IllegalArgumentException {
  461. try {
  462. renderer =
  463. (Renderer)Class.forName(rendererClassName).newInstance();
  464. if (renderer instanceof LogEnabled) {
  465. ((LogEnabled)renderer).enableLogging(getLogger());
  466. }
  467. renderer.setProducer(Version.getVersion());
  468. renderer.setUserAgent(getUserAgent());
  469. } catch (ClassNotFoundException e) {
  470. throw new IllegalArgumentException("Could not find "
  471. + rendererClassName);
  472. } catch (InstantiationException e) {
  473. throw new IllegalArgumentException("Could not instantiate "
  474. + rendererClassName);
  475. } catch (IllegalAccessException e) {
  476. throw new IllegalArgumentException("Could not access "
  477. + rendererClassName);
  478. } catch (ClassCastException e) {
  479. throw new IllegalArgumentException(rendererClassName
  480. + " is not a renderer");
  481. }
  482. }
  483. /**
  484. * Add the given element mapping.
  485. * An element mapping maps element names to Java classes.
  486. *
  487. * @param mapping the element mappingto add
  488. */
  489. public void addElementMapping(ElementMapping mapping) {
  490. mapping.addToBuilder(treeBuilder);
  491. }
  492. /**
  493. * Add the element mapping with the given class name.
  494. * @param mappingClassName the class name representing the element mapping.
  495. * @throws IllegalArgumentException if there was not such element mapping.
  496. */
  497. public void addElementMapping(String mappingClassName)
  498. throws IllegalArgumentException {
  499. try {
  500. ElementMapping mapping =
  501. (ElementMapping)Class.forName(mappingClassName).newInstance();
  502. addElementMapping(mapping);
  503. } catch (ClassNotFoundException e) {
  504. throw new IllegalArgumentException("Could not find "
  505. + mappingClassName);
  506. } catch (InstantiationException e) {
  507. throw new IllegalArgumentException("Could not instantiate "
  508. + mappingClassName);
  509. } catch (IllegalAccessException e) {
  510. throw new IllegalArgumentException("Could not access "
  511. + mappingClassName);
  512. } catch (ClassCastException e) {
  513. throw new IllegalArgumentException(mappingClassName
  514. + " is not an ElementMapping");
  515. }
  516. }
  517. /**
  518. * Returns the tree builder (a SAX ContentHandler).
  519. *
  520. * Used in situations where SAX is used but not via a FOP-invoked
  521. * SAX parser. A good example is an XSLT engine that fires SAX
  522. * events but isn't a SAX Parser itself.
  523. * @return a content handler for handling the SAX events.
  524. */
  525. public ContentHandler getContentHandler() {
  526. if (!isInitialized()) {
  527. initialize();
  528. }
  529. validateOutputStream();
  530. // TODO: - do this stuff in a better way
  531. // PIJ: I guess the structure handler should be created by the renderer.
  532. if (rendererType == RENDER_MIF) {
  533. structHandler = new org.apache.fop.mif.MIFHandler(stream);
  534. } else if (rendererType == RENDER_RTF) {
  535. structHandler = new org.apache.fop.rtf.renderer.RTFHandler(stream);
  536. } else {
  537. if (renderer == null) {
  538. throw new IllegalStateException(
  539. "Renderer not set when using standard structHandler");
  540. }
  541. structHandler = new LayoutHandler(stream, renderer, true);
  542. }
  543. structHandler.enableLogging(getLogger());
  544. treeBuilder.setUserAgent(getUserAgent());
  545. treeBuilder.setStructHandler(structHandler);
  546. return treeBuilder;
  547. }
  548. /**
  549. * Render the FO document read by a SAX Parser from an InputSource.
  550. * @param parser the SAX parser.
  551. * @param source the input source the parser reads from.
  552. * @throws FOPException if anything goes wrong.
  553. */
  554. public synchronized void render(XMLReader parser, InputSource source)
  555. throws FOPException {
  556. if (!isInitialized()) {
  557. initialize();
  558. }
  559. parser.setContentHandler(getContentHandler());
  560. try {
  561. parser.parse(source);
  562. } catch (SAXException e) {
  563. if (e.getException() instanceof FOPException) {
  564. // Undo exception tunneling.
  565. throw (FOPException)e.getException();
  566. } else {
  567. throw new FOPException(e);
  568. }
  569. } catch (IOException e) {
  570. throw new FOPException(e);
  571. }
  572. }
  573. /**
  574. * Render the FO ducument represented by a DOM Document.
  575. * @param document the DOM document to read from
  576. * @throws FOPException if anything goes wrong.
  577. */
  578. public synchronized void render(Document document)
  579. throws FOPException {
  580. if (!isInitialized()) {
  581. initialize();
  582. }
  583. try {
  584. DocumentInputSource source = new DocumentInputSource(document);
  585. DocumentReader reader = new DocumentReader();
  586. reader.setContentHandler(getContentHandler());
  587. reader.parse(source);
  588. } catch (SAXException e) {
  589. if (e.getException() instanceof FOPException) {
  590. // Undo exception tunneling.
  591. throw (FOPException)e.getException();
  592. } else {
  593. throw new FOPException(e);
  594. }
  595. } catch (IOException e) {
  596. throw new FOPException(e);
  597. }
  598. }
  599. /**
  600. * Runs the formatting and renderering process using the previously set
  601. * parser, input source, renderer and output stream.
  602. * If the renderer was not set, default to PDF.
  603. * If no parser was set, and the input source is not a dom document,
  604. * get a default SAX parser.
  605. * @throws IOException in case of IO errors.
  606. * @throws FOPException if anything else goes wrong.
  607. */
  608. public synchronized void run() throws IOException, FOPException {
  609. if (!isInitialized()) {
  610. initialize();
  611. }
  612. if (renderer == null) {
  613. setRenderer(RENDER_PDF);
  614. }
  615. if (source == null) {
  616. throw new FOPException("InputSource is not set.");
  617. }
  618. if (reader == null) {
  619. if (!(source instanceof DocumentInputSource)) {
  620. try {
  621. SAXParserFactory spf = javax.xml.parsers.SAXParserFactory.newInstance();
  622. spf.setNamespaceAware(true);
  623. reader = spf.newSAXParser().getXMLReader();
  624. } catch (SAXException e) {
  625. throw new FOPException(e);
  626. } catch (ParserConfigurationException e) {
  627. throw new FOPException(e);
  628. }
  629. }
  630. }
  631. if (source instanceof DocumentInputSource) {
  632. render(((DocumentInputSource)source).getDocument());
  633. } else {
  634. render(reader, source);
  635. }
  636. }
  637. }