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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /* $Id$
  2. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  3. * For details on use and redistribution please refer to the
  4. * LICENSE file included with these sources.
  5. */
  6. package org.apache.fop.apps;
  7. // FOP
  8. import org.apache.fop.fo.FOTreeBuilder;
  9. import org.apache.fop.fo.ElementMapping;
  10. import org.apache.fop.fo.PropertyListMapping;
  11. import org.apache.fop.layout.AreaTree;
  12. import org.apache.fop.layout.FontInfo;
  13. import org.apache.fop.render.Renderer;
  14. import org.apache.fop.messaging.MessageHandler;
  15. import org.apache.fop.configuration.ConfigurationReader;
  16. import org.apache.fop.configuration.Configuration;
  17. import org.apache.fop.tools.DocumentInputSource;
  18. import org.apache.fop.tools.DocumentReader;
  19. import org.apache.fop.system.BufferManager;
  20. // DOM
  21. import org.w3c.dom.Document;
  22. import org.w3c.dom.Node;
  23. import org.w3c.dom.NamedNodeMap;
  24. import org.w3c.dom.Attr;
  25. // SAX
  26. import org.xml.sax.ContentHandler;
  27. import org.xml.sax.InputSource;
  28. import org.xml.sax.XMLReader;
  29. import org.xml.sax.SAXException;
  30. import org.xml.sax.helpers.AttributesImpl;
  31. // Java
  32. import java.io.*;
  33. /**
  34. * Primary class that drives overall FOP process.
  35. * <P>
  36. * The simplest way to use this is to instantiate it with the
  37. * InputSource and OutputStream, then set the renderer desired, and
  38. * calling run();
  39. * <P>
  40. * Here is an example use of Driver which outputs PDF:
  41. *
  42. * <PRE>
  43. * Driver driver = new Driver(new InputSource (args[0]),
  44. * new FileOutputStream(args[1]));
  45. * driver.setRenderer(RENDER_PDF);
  46. * driver.run();
  47. * </PRE>
  48. * If neccessary, calling classes can call into the lower level
  49. * methods to setup and
  50. * render. Methods can be called to set the
  51. * Renderer to use, the (possibly multiple) ElementMapping(s) to
  52. * use and the OutputStream to use to output the results of the
  53. * rendering (where applicable). In the case of the Renderer and
  54. * ElementMapping(s), the Driver may be supplied either with the
  55. * object itself, or the name of the class, in which case Driver will
  56. * instantiate the class itself. The advantage of the latter is it
  57. * enables runtime determination of Renderer and ElementMapping(s).
  58. * <P>
  59. * Once the Driver is set up, the buildFOTree method
  60. * is called. Depending on whether DOM or SAX is being used, the
  61. * invocation of the method is either buildFOTree(Document) or
  62. * buildFOTree(Parser, InputSource) respectively.
  63. * <P>
  64. * A third possibility may be used to build the FO Tree, namely
  65. * calling getContentHandler() and firing the SAX events yourself.
  66. * <P>
  67. * Once the FO Tree is built, the format() and render() methods may be
  68. * called in that order.
  69. * <P>
  70. * Here is an example use of Driver which outputs to AWT:
  71. *
  72. * <PRE>
  73. * Driver driver = new Driver();
  74. * driver.setRenderer(new org.apache.fop.render.awt.AWTRenderer(translator));
  75. * driver.buildFOTree(parser, fileInputSource(args[0]));
  76. * driver.format();
  77. * driver.render();
  78. * </PRE>
  79. */
  80. public class Driver {
  81. /** Render to PDF. OutputStream must be set */
  82. public static final int RENDER_PDF = 1;
  83. /** Render to a GUI window. No OutputStream neccessary */
  84. public static final int RENDER_AWT = 2;
  85. /** Render to MIF. OutputStream must be set */
  86. public static final int RENDER_MIF = 3;
  87. /** Render to XML. OutputStream must be set */
  88. public static final int RENDER_XML = 4;
  89. /** Render to PRINT. No OutputStream neccessary */
  90. public static final int RENDER_PRINT = 5;
  91. /** Render to PCL. OutputStream must be set */
  92. public static final int RENDER_PCL = 6;
  93. /** Render to Postscript. OutputStream must be set */
  94. public static final int RENDER_PS = 7;
  95. /** Render to Text. OutputStream must be set */
  96. public static final int RENDER_TXT = 8;
  97. /** the FO tree builder */
  98. private FOTreeBuilder _treeBuilder;
  99. /** the area tree that is the result of formatting the FO tree */
  100. private AreaTree _areaTree;
  101. /** the renderer to use to output the area tree */
  102. private Renderer _renderer;
  103. /** the source of the FO file */
  104. private InputSource _source;
  105. /** the stream to use to output the results of the renderer */
  106. private OutputStream _stream;
  107. /** The XML parser to use when building the FO tree */
  108. private XMLReader _reader;
  109. /** If true, full error stacks are reported */
  110. private boolean _errorDump = false;
  111. /** the system resources that FOP will use */
  112. private BufferManager _bufferManager;
  113. /** create a new Driver */
  114. public Driver() {
  115. _stream = null;
  116. _bufferManager = new BufferManager();
  117. _treeBuilder = new FOTreeBuilder();
  118. _treeBuilder.setBufferManager(_bufferManager);
  119. setupDefaultMappings();
  120. }
  121. public Driver(InputSource source, OutputStream stream) {
  122. this();
  123. _source = source;
  124. _stream = stream;
  125. }
  126. /**
  127. * Resets the Driver so it can be reused. Property and element
  128. * mappings are reset to defaults.
  129. * The output stream is cleared. The renderer is cleared.
  130. */
  131. public synchronized void reset() {
  132. _areaTree = null;
  133. _treeBuilder.reset();
  134. }
  135. public boolean hasData() {
  136. return(_treeBuilder.hasData());
  137. }
  138. /**
  139. * Set the error dump option
  140. * @param dump if true, full stacks will be reported to the error log
  141. */
  142. public void setErrorDump(boolean dump) {
  143. _errorDump = dump;
  144. }
  145. /**
  146. * Set the OutputStream to use to output the result of the Renderer
  147. * (if applicable)
  148. * @param stream the stream to output the result of rendering to
  149. *
  150. */
  151. public void setOutputStream(OutputStream stream) {
  152. _stream = stream;
  153. }
  154. /**
  155. * Set the source for the FO document. This can be a normal SAX
  156. * InputSource, or an DocumentInputSource containing a DOM document.
  157. * @see DocumentInputSource
  158. */
  159. public void setInputSource(InputSource source) {
  160. _source = source;
  161. }
  162. /**
  163. * Sets the reader used when reading in the source. If not set,
  164. * this defaults to a basic SAX parser.
  165. */
  166. public void setXMLReader(XMLReader reader) {
  167. _reader = reader;
  168. }
  169. /**
  170. * Sets all the element and property list mappings to their default values.
  171. *
  172. */
  173. public void setupDefaultMappings() {
  174. addElementMapping("org.apache.fop.fo.StandardElementMapping");
  175. addPropertyList ("org.apache.fop.fo.StandardPropertyListMapping");
  176. addElementMapping("org.apache.fop.svg.SVGElementMapping");
  177. addPropertyList ("org.apache.fop.svg.SVGPropertyListMapping");
  178. addElementMapping("org.apache.fop.extensions.ExtensionElementMapping");
  179. addPropertyList ("org.apache.fop.extensions.ExtensionPropertyListMapping");
  180. }
  181. /**
  182. * Set the rendering type to use. Must be one of
  183. * <ul>
  184. * <li>RENDER_PDF
  185. * <li>RENDER_AWT
  186. * <li>RENDER_MIF
  187. * <li>RENDER_XML
  188. * <li>RENDER_PCL
  189. * <li>RENDER_PS
  190. * <li>RENDER_TXT
  191. * </ul>
  192. * @param renderer the type of renderer to use
  193. */
  194. public void setRenderer(int renderer) throws IllegalArgumentException {
  195. switch (renderer) {
  196. case RENDER_PDF:
  197. setRenderer(new org.apache.fop.render.pdf.PDFRenderer());
  198. break;
  199. case RENDER_AWT:
  200. throw new IllegalArgumentException("Use renderer form of setRenderer() for AWT");
  201. case RENDER_PRINT:
  202. throw new IllegalArgumentException("Use renderer form of setRenderer() for PRINT");
  203. case RENDER_PCL:
  204. setRenderer(new org.apache.fop.render.pcl.PCLRenderer());
  205. break;
  206. case RENDER_PS:
  207. setRenderer(new org.apache.fop.render.ps.PSRenderer());
  208. break;
  209. case RENDER_TXT:
  210. setRenderer(new org.apache.fop.render.txt.TXTRenderer());
  211. break;
  212. case RENDER_MIF:
  213. setRenderer(new org.apache.fop.render.mif.MIFRenderer());
  214. break;
  215. case RENDER_XML:
  216. setRenderer(new org.apache.fop.render.xml.XMLRenderer());
  217. break;
  218. default:
  219. throw new IllegalArgumentException("Unknown renderer type");
  220. }
  221. }
  222. /**
  223. * Set the Renderer to use
  224. * @param renderer the renderer instance to use
  225. */
  226. public void setRenderer(Renderer renderer) {
  227. _renderer = renderer;
  228. }
  229. public Renderer getRenderer() {
  230. return _renderer;
  231. }
  232. /**
  233. * @deprecated use renderer.setProducer(version) + setRenderer(renderer) or just setRenderer(renderer_type) which will use the default producer string.
  234. * @see #setRenderer(int)
  235. * @see #setRenderer(Renderer)
  236. */
  237. public void setRenderer(String rendererClassName, String version) {
  238. setRenderer(rendererClassName);
  239. }
  240. /**
  241. * Set the class name of the Renderer to use as well as the
  242. * producer string for those renderers that can make use of it.
  243. * @param rendererClassName classname of the renderer to use such as
  244. * "org.apache.fop.render.pdf.PDFRenderer"
  245. * @exception IllegalArgumentException if the classname was invalid.
  246. * @see #setRenderer(int)
  247. */
  248. public void setRenderer(String rendererClassName)
  249. throws IllegalArgumentException {
  250. try {
  251. _renderer = (Renderer) Class.forName(
  252. rendererClassName).newInstance();
  253. _renderer.setProducer(Version.getVersion());
  254. } catch (ClassNotFoundException e) {
  255. throw new IllegalArgumentException("Could not find " +
  256. rendererClassName);
  257. }
  258. catch (InstantiationException e) {
  259. throw new IllegalArgumentException(
  260. "Could not instantiate " + rendererClassName);
  261. }
  262. catch (IllegalAccessException e) {
  263. throw new IllegalArgumentException("Could not access " +
  264. rendererClassName);
  265. }
  266. catch (ClassCastException e) {
  267. throw new IllegalArgumentException(rendererClassName + " is not a renderer");
  268. }
  269. }
  270. /**
  271. * Add the given element mapping.
  272. * An element mapping maps element names to Java classes.
  273. *
  274. * @param mapping the element mappingto add
  275. */
  276. public void addElementMapping(ElementMapping mapping) {
  277. mapping.addToBuilder(_treeBuilder);
  278. }
  279. /**
  280. * add the element mapping with the given class name
  281. */
  282. public void addElementMapping(String mappingClassName)
  283. throws IllegalArgumentException {
  284. try {
  285. ElementMapping mapping = (ElementMapping) Class.forName(
  286. mappingClassName).newInstance();
  287. addElementMapping(mapping);
  288. } catch (ClassNotFoundException e) {
  289. throw new IllegalArgumentException("Could not find " +
  290. mappingClassName);
  291. }
  292. catch (InstantiationException e) {
  293. throw new IllegalArgumentException(
  294. "Could not instantiate " + mappingClassName);
  295. }
  296. catch (IllegalAccessException e) {
  297. throw new IllegalArgumentException("Could not access " +
  298. mappingClassName);
  299. }
  300. catch (ClassCastException e) {
  301. throw new IllegalArgumentException(mappingClassName + " is not an ElementMapping");
  302. }
  303. }
  304. /**
  305. * Add the PropertyListMapping.
  306. */
  307. public void addPropertyList(PropertyListMapping mapping) {
  308. mapping.addToBuilder(_treeBuilder);
  309. }
  310. /**
  311. * Add the PropertyListMapping with the given class name.
  312. */
  313. public void addPropertyList(String listClassName)
  314. throws IllegalArgumentException {
  315. try {
  316. PropertyListMapping mapping =
  317. (PropertyListMapping) Class.forName(
  318. listClassName).newInstance();
  319. addPropertyList(mapping);
  320. } catch (ClassNotFoundException e) {
  321. throw new IllegalArgumentException("Could not find " +
  322. listClassName);
  323. }
  324. catch (InstantiationException e) {
  325. throw new IllegalArgumentException(
  326. "Could not instantiate " + listClassName);
  327. }
  328. catch (IllegalAccessException e) {
  329. throw new IllegalArgumentException("Could not access " +
  330. listClassName);
  331. }
  332. catch (ClassCastException e) {
  333. throw new IllegalArgumentException(listClassName + " is not an ElementMapping");
  334. }
  335. }
  336. /**
  337. * Returns the tree builder (a SAX ContentHandler).
  338. *
  339. * Used in situations where SAX is used but not via a FOP-invoked
  340. * SAX parser. A good example is an XSLT engine that fires SAX
  341. * events but isn't a SAX Parser itself.
  342. */
  343. public ContentHandler getContentHandler() {
  344. return _treeBuilder;
  345. }
  346. /**
  347. * Build the formatting object tree using the given SAX Parser and
  348. * SAX InputSource
  349. */
  350. public synchronized void buildFOTree(XMLReader parser,
  351. InputSource source)
  352. throws FOPException {
  353. parser.setContentHandler(_treeBuilder);
  354. try {
  355. parser.parse(source);
  356. } catch (SAXException e) {
  357. if (e.getException() instanceof FOPException) {
  358. throw (FOPException) e.getException();
  359. } else {
  360. throw new FOPException(e);
  361. }
  362. }
  363. catch (IOException e) {
  364. throw new FOPException(e);
  365. }
  366. }
  367. /**
  368. * Build the formatting object tree using the given DOM Document
  369. */
  370. public synchronized void buildFOTree(Document document)
  371. throws FOPException {
  372. try {
  373. DocumentInputSource source = new DocumentInputSource(document);
  374. DocumentReader reader = new DocumentReader();
  375. reader.setContentHandler(_treeBuilder);
  376. reader.parse(source);
  377. } catch (SAXException e) {
  378. throw new FOPException(e);
  379. }
  380. catch (IOException e) {
  381. throw new FOPException(e);
  382. }
  383. }
  384. /**
  385. * Dumps an error
  386. */
  387. public void dumpError(Exception e) {
  388. if (_errorDump) {
  389. if (e instanceof SAXException) {
  390. e.printStackTrace();
  391. if (((SAXException) e).getException() != null) {
  392. ((SAXException) e).getException().printStackTrace();
  393. }
  394. } else if (e instanceof FOPException) {
  395. e.printStackTrace();
  396. if (((FOPException) e).getException() != null) {
  397. ((FOPException) e).getException().printStackTrace();
  398. }
  399. } else {
  400. e.printStackTrace();
  401. }
  402. }
  403. }
  404. /* Set up the system buffers */
  405. public void setBufferFile(File bufferFile) {
  406. this._bufferManager.addBufferFile(bufferFile);
  407. }
  408. /**
  409. * format the formatting object tree into an area tree
  410. */
  411. public synchronized void format()
  412. throws FOPException {
  413. FontInfo fontInfo = new FontInfo();
  414. _renderer.setupFontInfo(fontInfo);
  415. _areaTree = new AreaTree();
  416. _areaTree.setFontInfo(fontInfo);
  417. _treeBuilder.format(_areaTree);
  418. }
  419. /**
  420. * render the area tree to the output form
  421. */
  422. public synchronized void render()
  423. throws IOException, FOPException {
  424. _renderer.render(_areaTree, _stream);
  425. }
  426. /**
  427. * Runs the formatting and renderering process using the previously set
  428. * inputsource and outputstream
  429. */
  430. public synchronized void run()
  431. throws IOException, FOPException {
  432. if (_renderer == null) {
  433. setRenderer(RENDER_PDF);
  434. }
  435. if (_source == null) {
  436. throw new FOPException("InputSource is not set.");
  437. }
  438. if (_reader == null) {
  439. if (!(_source instanceof DocumentInputSource)) {
  440. _reader = ConfigurationReader.createParser();
  441. }
  442. }
  443. if (_source instanceof DocumentInputSource) {
  444. buildFOTree(((DocumentInputSource)_source).getDocument());
  445. } else {
  446. buildFOTree(_reader, _source);
  447. }
  448. format();
  449. render();
  450. }
  451. }