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 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /*
  2. * Copyright 1999-2004 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.IOException;
  20. import java.io.OutputStream;
  21. // XML
  22. import org.xml.sax.ContentHandler;
  23. import org.xml.sax.InputSource;
  24. import org.xml.sax.SAXException;
  25. import org.xml.sax.XMLReader;
  26. import org.w3c.dom.Document;
  27. // FOP
  28. import org.apache.fop.fo.ElementMapping;
  29. import org.apache.fop.fo.FOTreeBuilder;
  30. import org.apache.fop.fo.FOInputHandler;
  31. import org.apache.fop.fo.FOTreeHandler;
  32. import org.apache.fop.render.Renderer;
  33. import org.apache.fop.render.awt.AWTRenderer;
  34. import org.apache.fop.render.mif.MIFHandler;
  35. import org.apache.fop.render.rtf.RTFHandler;
  36. import org.apache.fop.tools.DocumentInputSource;
  37. import org.apache.fop.tools.DocumentReader;
  38. /**
  39. * Primary class that drives overall FOP process.
  40. * <P>
  41. * The simplest way to use this is to instantiate it with the
  42. * InputSource and OutputStream, then set the renderer desired, and
  43. * calling run();
  44. * <P>
  45. * Here is an example use of Driver which outputs PDF:
  46. *
  47. * <PRE>
  48. * Driver driver = new Driver(new InputSource (args[0]),
  49. * new FileOutputStream(args[1]));
  50. * driver.setRenderer(RENDER_PDF);
  51. * driver.run();
  52. * </PRE>
  53. * If neccessary, calling classes can call into the lower level
  54. * methods to setup and
  55. * render. Methods can be called to set the
  56. * Renderer to use, the (possibly multiple) ElementMapping(s) to
  57. * use and the OutputStream to use to output the results of the
  58. * rendering (where applicable). In the case of the Renderer and
  59. * ElementMapping(s), the Driver may be supplied either with the
  60. * object itself, or the name of the class, in which case Driver will
  61. * instantiate the class itself. The advantage of the latter is it
  62. * enables runtime determination of Renderer and ElementMapping(s).
  63. * <P>
  64. * Once the Driver is set up, the render method
  65. * is called. Depending on whether DOM or SAX is being used, the
  66. * invocation of the method is either render(Document) or
  67. * buildFOTree(Parser, InputSource) respectively.
  68. * <P>
  69. * A third possibility may be used to build the FO Tree, namely
  70. * calling getContentHandler() and firing the SAX events yourself.
  71. * <P>
  72. * Once the FO Tree is built, the format() and render() methods may be
  73. * called in that order.
  74. * <P>
  75. * Here is an example use of Driver which outputs to AWT:
  76. *
  77. * <PRE>
  78. * Driver driver = new Driver();
  79. * driver.setRenderer(new org.apache.fop.render.awt.AWTRenderer(translator));
  80. * driver.render(parser, fileInputSource(args[0]));
  81. * </PRE>
  82. */
  83. public class Driver {
  84. /**
  85. * private constant to indicate renderer was not defined.
  86. */
  87. private static final int NOT_SET = 0;
  88. /**
  89. * Render to PDF. OutputStream must be set
  90. */
  91. public static final int RENDER_PDF = 1;
  92. /**
  93. * Render to a GUI window. No OutputStream neccessary
  94. */
  95. public static final int RENDER_AWT = 2;
  96. /**
  97. * Render to MIF. OutputStream must be set
  98. */
  99. public static final int RENDER_MIF = 3;
  100. /**
  101. * Render to XML. OutputStream must be set
  102. */
  103. public static final int RENDER_XML = 4;
  104. /**
  105. * Render to PRINT. No OutputStream neccessary
  106. */
  107. public static final int RENDER_PRINT = 5;
  108. /**
  109. * Render to PCL. OutputStream must be set
  110. */
  111. public static final int RENDER_PCL = 6;
  112. /**
  113. * Render to Postscript. OutputStream must be set
  114. */
  115. public static final int RENDER_PS = 7;
  116. /**
  117. * Render to Text. OutputStream must be set
  118. */
  119. public static final int RENDER_TXT = 8;
  120. /**
  121. * Render to SVG. OutputStream must be set
  122. */
  123. public static final int RENDER_SVG = 9;
  124. /**
  125. * Render to RTF. OutputStream must be set
  126. */
  127. public static final int RENDER_RTF = 10;
  128. /**
  129. * the FO tree builder
  130. */
  131. private FOTreeBuilder treeBuilder;
  132. /**
  133. * the renderer type code given by setRenderer
  134. */
  135. private int rendererType = NOT_SET;
  136. /**
  137. * the renderer to use to output the area tree
  138. */
  139. private Renderer renderer;
  140. /**
  141. * the SAX ContentHandler
  142. */
  143. private FOInputHandler foInputHandler;
  144. /**
  145. * the source of the FO file
  146. */
  147. private InputSource source;
  148. /**
  149. * the stream to use to output the results of the renderer
  150. */
  151. private OutputStream stream;
  152. /**
  153. * The XML parser to use when building the FO tree
  154. */
  155. private XMLReader reader;
  156. /**
  157. * The system resources that FOP will use
  158. */
  159. private FOUserAgent userAgent = null;
  160. /**
  161. * Main constructor for the Driver class.
  162. */
  163. public Driver() {
  164. stream = null;
  165. }
  166. /**
  167. * Convenience constructor for directly setting input and output.
  168. * @param source InputSource to take the XSL-FO input from
  169. * @param stream Target output stream
  170. */
  171. public Driver(InputSource source, OutputStream stream) {
  172. this();
  173. this.source = source;
  174. this.stream = stream;
  175. }
  176. private boolean isInitialized() {
  177. return (treeBuilder != null);
  178. }
  179. /**
  180. * Initializes the Driver object.
  181. */
  182. public void initialize() {
  183. if (isInitialized()) {
  184. throw new IllegalStateException("Driver already initialized");
  185. }
  186. treeBuilder = new FOTreeBuilder();
  187. }
  188. /**
  189. * Optionally sets the FOUserAgent instance for FOP to use. The Driver
  190. * class sets up its own FOUserAgent if none is set through this method.
  191. * @param agent FOUserAgent to use
  192. */
  193. public void setUserAgent(FOUserAgent agent) {
  194. userAgent = agent;
  195. }
  196. public FOUserAgent getUserAgent() {
  197. if (userAgent == null) {
  198. userAgent = new FOUserAgent();
  199. }
  200. return userAgent;
  201. }
  202. /**
  203. * Resets the Driver so it can be reused. Property and element
  204. * mappings are reset to defaults.
  205. * The output stream is cleared. The renderer is cleared.
  206. */
  207. public synchronized void reset() {
  208. source = null;
  209. stream = null;
  210. reader = null;
  211. if (treeBuilder != null) {
  212. treeBuilder.reset();
  213. }
  214. }
  215. /**
  216. * Indicates whether FOP has already received input data.
  217. * @return true, if input data was received
  218. */
  219. public boolean hasData() {
  220. return (treeBuilder.hasData());
  221. }
  222. /**
  223. * Set the OutputStream to use to output the result of the Renderer
  224. * (if applicable)
  225. * @param stream the stream to output the result of rendering to
  226. */
  227. public void setOutputStream(OutputStream stream) {
  228. this.stream = stream;
  229. }
  230. private void validateOutputStream() {
  231. if (this.stream == null) {
  232. throw new IllegalStateException("OutputStream has not been set");
  233. }
  234. }
  235. /**
  236. * Set the source for the FO document. This can be a normal SAX
  237. * InputSource, or an DocumentInputSource containing a DOM document.
  238. * @see DocumentInputSource
  239. */
  240. public void setInputSource(InputSource source) {
  241. this.source = source;
  242. }
  243. /**
  244. * Sets the reader used when reading in the source. If not set,
  245. * this defaults to a basic SAX parser.
  246. * @param reader the reader to use.
  247. */
  248. public void setXMLReader(XMLReader reader) {
  249. this.reader = reader;
  250. }
  251. /**
  252. * Shortcut to set the rendering type to use. Must be one of
  253. * <ul>
  254. * <li>RENDER_PDF</li>
  255. * <li>RENDER_AWT</li>
  256. * <li>RENDER_PRINT</li>
  257. * <li>RENDER_MIF</li>
  258. * <li>RENDER_XML</li>
  259. * <li>RENDER_PCL</li>
  260. * <li>RENDER_PS</li>
  261. * <li>RENDER_TXT</li>
  262. * <li>RENDER_SVG</li>
  263. * <li>RENDER_RTF</li>
  264. * </ul>
  265. * @param renderer the type of renderer to use
  266. * @throws IllegalArgumentException if an unsupported renderer type was required.
  267. */
  268. public void setRenderer(int renderer) throws IllegalArgumentException {
  269. rendererType = renderer;
  270. switch (renderer) {
  271. case RENDER_PDF:
  272. setRenderer("org.apache.fop.render.pdf.PDFRenderer");
  273. break;
  274. case RENDER_AWT:
  275. throw new IllegalArgumentException("Use renderer form of setRenderer() for AWT");
  276. case RENDER_PRINT:
  277. setRenderer("org.apache.fop.render.awt.AWTPrintRenderer");
  278. break;
  279. case RENDER_PCL:
  280. setRenderer("org.apache.fop.render.pcl.PCLRenderer");
  281. break;
  282. case RENDER_PS:
  283. setRenderer("org.apache.fop.render.ps.PSRenderer");
  284. break;
  285. case RENDER_TXT:
  286. setRenderer("org.apache.fop.render.txt.TXTRenderer()");
  287. break;
  288. case RENDER_MIF:
  289. //foInputHandler will be set later
  290. break;
  291. case RENDER_XML:
  292. setRenderer("org.apache.fop.render.xml.XMLRenderer");
  293. break;
  294. case RENDER_SVG:
  295. setRenderer("org.apache.fop.render.svg.SVGRenderer");
  296. break;
  297. case RENDER_RTF:
  298. //foInputHandler will be set later
  299. break;
  300. default:
  301. rendererType = NOT_SET;
  302. throw new IllegalArgumentException("Unknown renderer type " + renderer);
  303. }
  304. }
  305. /**
  306. * Set the Renderer to use.
  307. * @param renderer the renderer instance to use
  308. */
  309. public void setRenderer(Renderer renderer) {
  310. // AWTStarter calls this function directly
  311. if (renderer instanceof AWTRenderer) {
  312. rendererType = RENDER_AWT;
  313. }
  314. renderer.setUserAgent(getUserAgent());
  315. userAgent.setProducer("FOP Version" + Fop.getVersion());
  316. this.renderer = renderer;
  317. }
  318. /**
  319. * Returns the currently active renderer.
  320. * @return the renderer
  321. */
  322. public Renderer getRenderer() {
  323. return renderer;
  324. }
  325. /**
  326. * Set the class name of the Renderer to use as well as the
  327. * producer string for those renderers that can make use of it.
  328. * @param rendererClassName classname of the renderer to use such as
  329. * "org.apache.fop.render.pdf.PDFRenderer"
  330. * @exception IllegalArgumentException if the classname was invalid.
  331. * @see #setRenderer(int)
  332. */
  333. public void setRenderer(String rendererClassName)
  334. throws IllegalArgumentException {
  335. try {
  336. renderer = (Renderer)Class.forName(rendererClassName).newInstance();
  337. renderer.setUserAgent(getUserAgent());
  338. userAgent.setProducer("FOP Version" + Fop.getVersion());
  339. } catch (ClassNotFoundException e) {
  340. throw new IllegalArgumentException("Could not find "
  341. + rendererClassName);
  342. } catch (InstantiationException e) {
  343. throw new IllegalArgumentException("Could not instantiate "
  344. + rendererClassName);
  345. } catch (IllegalAccessException e) {
  346. throw new IllegalArgumentException("Could not access "
  347. + rendererClassName);
  348. } catch (ClassCastException e) {
  349. throw new IllegalArgumentException(rendererClassName
  350. + " is not a renderer");
  351. }
  352. }
  353. /**
  354. * Add the given element mapping.
  355. * An element mapping maps element names to Java classes.
  356. *
  357. * @param mapping the element mappingto add
  358. */
  359. public void addElementMapping(ElementMapping mapping) {
  360. treeBuilder.addElementMapping(mapping);
  361. }
  362. /**
  363. * Add the element mapping with the given class name.
  364. * @param mappingClassName the class name representing the element mapping.
  365. */
  366. public void addElementMapping(String mappingClassName) {
  367. treeBuilder.addElementMapping(mappingClassName);
  368. }
  369. /**
  370. * Determines which SAX ContentHandler is appropriate for the rendererType.
  371. * Structure renderers (e.g. MIF & RTF) each have a specialized
  372. * ContentHandler that directly place data into the output stream. Layout
  373. * renderers (e.g. PDF & PostScript) use a ContentHandler that builds an FO
  374. * Tree.
  375. * @return a SAX ContentHandler for handling the SAX events.
  376. * @throws FOPException if setting up the ContentHandler fails
  377. */
  378. public ContentHandler getContentHandler() throws FOPException {
  379. if (!isInitialized()) {
  380. initialize();
  381. }
  382. if (rendererType != RENDER_PRINT && rendererType != RENDER_AWT) {
  383. validateOutputStream();
  384. }
  385. // TODO: - do this stuff in a better way
  386. // PIJ: I guess the structure handler should be created by the renderer.
  387. if (rendererType == RENDER_MIF) {
  388. foInputHandler = new MIFHandler(userAgent, stream);
  389. } else if (rendererType == RENDER_RTF) {
  390. foInputHandler = new RTFHandler(userAgent, stream);
  391. } else {
  392. if (renderer == null) {
  393. throw new IllegalStateException(
  394. "Renderer not set when using standard foInputHandler");
  395. }
  396. foInputHandler = new FOTreeHandler(userAgent, renderer, stream, true);
  397. }
  398. treeBuilder.setFOInputHandler(foInputHandler);
  399. return treeBuilder;
  400. }
  401. /**
  402. * Render the FO document read by a SAX Parser from an InputHandler
  403. * @param inputHandler the input handler containing the source and
  404. * parser information.
  405. * @throws FOPException if anything goes wrong.
  406. */
  407. public synchronized void render(InputHandler inputHandler)
  408. throws FOPException {
  409. XMLReader parser = inputHandler.getParser();
  410. userAgent.setBaseURL(inputHandler.getBaseURL());
  411. render(parser, inputHandler.getInputSource());
  412. }
  413. /**
  414. * This is the main render() method. The other render() methods are for
  415. * convenience, and normalize to this form, then run this.
  416. * Renders the FO document read by a SAX Parser from an InputSource.
  417. * For versions not needing an FO Tree (e.g., Alt-Design), override this.
  418. *
  419. * @param parser the SAX parser.
  420. * @param source the input source the parser reads from.
  421. * @throws FOPException if anything goes wrong.
  422. */
  423. public synchronized void render(XMLReader parser, InputSource source)
  424. throws FOPException {
  425. parser.setContentHandler(getContentHandler());
  426. try {
  427. /**
  428. The following statement triggers virtually all of the processing
  429. for this document. The SAX parser fires events that are handled by
  430. the appropriate InputHandler object, which means that you will need
  431. to look in those objects to see where FOP picks up control of
  432. processing again. For Structure Renderers (e.g. MIF & RTF), the SAX
  433. events are handled directly. For Layout Renderers (e.g. PDF &
  434. PostScript), an FO Tree is built by the FOTreeHandler, which in
  435. turn fires events when a PageSequence object is complete. This
  436. allows higher-level control objects (such as this class) to work
  437. directly with PageSequence objects. See foPageSequenceComplete()
  438. where this level of control is implemented.
  439. */
  440. parser.parse(source);
  441. } catch (SAXException e) {
  442. if (e.getException() instanceof FOPException) {
  443. // Undo exception tunneling.
  444. throw (FOPException)e.getException();
  445. } else {
  446. throw new FOPException(e);
  447. }
  448. } catch (IOException e) {
  449. throw new FOPException(e);
  450. }
  451. }
  452. /**
  453. * This method overloads the main render() method, adding the convenience
  454. * of using a DOM Document as input.
  455. * @see #render(XMLReader, InputSource)
  456. * @param document the DOM document to read from
  457. * @throws FOPException if anything goes wrong.
  458. */
  459. public synchronized void render(Document document)
  460. throws FOPException {
  461. DocumentInputSource source = new DocumentInputSource(document);
  462. DocumentReader reader = new DocumentReader();
  463. render(reader, source);
  464. }
  465. /**
  466. * Runs the formatting and renderering process using the previously set
  467. * parser, input source, renderer and output stream.
  468. * If the renderer was not set, default to PDF.
  469. * If no parser was set, and the input source is not a dom document,
  470. * get a default SAX parser.
  471. * @throws IOException in case of IO errors.
  472. * @throws FOPException if anything else goes wrong.
  473. */
  474. public synchronized void run() throws IOException, FOPException {
  475. if (!isInitialized()) {
  476. initialize();
  477. }
  478. if (renderer == null && rendererType != RENDER_RTF
  479. && rendererType != RENDER_MIF) {
  480. setRenderer(RENDER_PDF);
  481. }
  482. if (source == null) {
  483. throw new FOPException("InputSource is not set.");
  484. }
  485. if (reader == null) {
  486. if (!(source instanceof DocumentInputSource)) {
  487. reader = FOFileHandler.createParser();
  488. }
  489. }
  490. if (source instanceof DocumentInputSource) {
  491. render(((DocumentInputSource)source).getDocument());
  492. } else {
  493. render(reader, source);
  494. }
  495. }
  496. }