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

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 Text. OutputStream must be set */
  94. public static final int RENDER_TXT = 7;
  95. /** the FO tree builder */
  96. private FOTreeBuilder _treeBuilder;
  97. /** the area tree that is the result of formatting the FO tree */
  98. private AreaTree _areaTree;
  99. /** the renderer to use to output the area tree */
  100. private Renderer _renderer;
  101. /** the source of the FO file */
  102. private InputSource _source;
  103. /** the stream to use to output the results of the renderer */
  104. private OutputStream _stream;
  105. /** The XML parser to use when building the FO tree */
  106. private XMLReader _reader;
  107. /** If true, full error stacks are reported */
  108. private boolean _errorDump = false;
  109. /** the system resources that FOP will use */
  110. private BufferManager _bufferManager;
  111. /** create a new Driver */
  112. public Driver() {
  113. _stream = null;
  114. _bufferManager = new BufferManager();
  115. _treeBuilder = new FOTreeBuilder();
  116. _treeBuilder.setBufferManager(_bufferManager);
  117. setupDefaultMappings();
  118. }
  119. public Driver(InputSource source, OutputStream stream) {
  120. this();
  121. _source = source;
  122. _stream = stream;
  123. }
  124. /**
  125. * Resets the Driver so it can be reused. Property and element
  126. * mappings are reset to defaults.
  127. * The output stream is cleared. The renderer is cleared.
  128. */
  129. public synchronized void reset()
  130. {
  131. _areaTree = null;
  132. _treeBuilder.reset();
  133. }
  134. public boolean hasData()
  135. {
  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. {
  161. _source = source;
  162. }
  163. /**
  164. * Sets the reader used when reading in the source. If not set,
  165. * this defaults to a basic SAX parser.
  166. */
  167. public void setXMLReader(XMLReader reader)
  168. {
  169. _reader = reader;
  170. }
  171. /**
  172. * Sets all the element and property list mappings to their default values.
  173. *
  174. */
  175. public void setupDefaultMappings()
  176. {
  177. addElementMapping("org.apache.fop.fo.StandardElementMapping");
  178. addPropertyList ("org.apache.fop.fo.StandardPropertyListMapping");
  179. addElementMapping("org.apache.fop.svg.SVGElementMapping");
  180. addPropertyList ("org.apache.fop.svg.SVGPropertyListMapping");
  181. addElementMapping("org.apache.fop.extensions.ExtensionElementMapping");
  182. addPropertyList ("org.apache.fop.extensions.ExtensionPropertyListMapping");
  183. }
  184. /**
  185. * Set the rendering type to use. Must be one of
  186. * <ul>
  187. * <li>RENDER_PDF
  188. * <li>RENDER_AWT
  189. * <li>RENDER_MIF
  190. * <li>RENDER_XML
  191. * <li>RENDER_PCL
  192. * <li>RENDER_TXT
  193. * </ul>
  194. * @param renderer the type of renderer to use
  195. */
  196. public void setRenderer(int renderer)
  197. throws IllegalArgumentException
  198. {
  199. switch (renderer) {
  200. case RENDER_PDF:
  201. setRenderer(new org.apache.fop.render.pdf.PDFRenderer());
  202. break;
  203. case RENDER_AWT:
  204. throw new IllegalArgumentException("Use renderer form of setRenderer() for AWT");
  205. case RENDER_PRINT:
  206. throw new IllegalArgumentException("Use renderer form of setRenderer() for PRINT");
  207. case RENDER_PCL:
  208. setRenderer(new org.apache.fop.render.pcl.PCLRenderer());
  209. break;
  210. case RENDER_TXT:
  211. setRenderer(new org.apache.fop.render.txt.TXTRenderer());
  212. break;
  213. case RENDER_MIF:
  214. setRenderer(new org.apache.fop.render.mif.MIFRenderer());
  215. break;
  216. case RENDER_XML:
  217. setRenderer(new org.apache.fop.render.xml.XMLRenderer());
  218. break;
  219. default:
  220. throw new IllegalArgumentException("Unknown renderer type");
  221. }
  222. }
  223. /**
  224. * Set the Renderer to use
  225. * @param renderer the renderer instance to use
  226. */
  227. public void setRenderer(Renderer renderer) {
  228. _renderer = renderer;
  229. }
  230. /**
  231. * @deprecated use renderer.setProducer(version) + setRenderer(renderer) or just setRenderer(renderer_type) which will use the default producer string.
  232. * @see #setRenderer(int)
  233. * @see #setRenderer(Renderer)
  234. */
  235. public void setRenderer(String rendererClassName, String version)
  236. {
  237. setRenderer(rendererClassName);
  238. }
  239. /**
  240. * Set the class name of the Renderer to use as well as the
  241. * producer string for those renderers that can make use of it.
  242. * @param rendererClassName classname of the renderer to use such as
  243. * "org.apache.fop.render.pdf.PDFRenderer"
  244. * @exception IllegalArgumentException if the classname was invalid.
  245. * @see #setRenderer(int)
  246. */
  247. public void setRenderer(String rendererClassName)
  248. throws IllegalArgumentException
  249. {
  250. try {
  251. _renderer = (Renderer) Class.forName(rendererClassName).newInstance();
  252. _renderer.setProducer(Version.getVersion());
  253. }
  254. catch (ClassNotFoundException e) {
  255. throw new IllegalArgumentException("Could not find " +
  256. rendererClassName);
  257. }
  258. catch (InstantiationException e) {
  259. throw new IllegalArgumentException("Could not instantiate " +
  260. rendererClassName);
  261. }
  262. catch (IllegalAccessException e) {
  263. throw new IllegalArgumentException("Could not access " + rendererClassName);
  264. }
  265. catch (ClassCastException e) {
  266. throw new IllegalArgumentException(rendererClassName + " is not a renderer");
  267. }
  268. }
  269. /**
  270. * Add the given element mapping.
  271. * An element mapping maps element names to Java classes.
  272. *
  273. * @param mapping the element mappingto add
  274. */
  275. public void addElementMapping(ElementMapping mapping) {
  276. mapping.addToBuilder(_treeBuilder);
  277. }
  278. /**
  279. * add the element mapping with the given class name
  280. */
  281. public void addElementMapping(String mappingClassName)
  282. throws IllegalArgumentException
  283. {
  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 " + mappingClassName);
  290. }
  291. catch (InstantiationException e) {
  292. throw new IllegalArgumentException("Could not instantiate " +
  293. mappingClassName);
  294. }
  295. catch (IllegalAccessException e) {
  296. throw new IllegalArgumentException("Could not access " + mappingClassName);
  297. }
  298. catch (ClassCastException e) {
  299. throw new IllegalArgumentException(mappingClassName + " is not an ElementMapping");
  300. }
  301. }
  302. /**
  303. * Add the PropertyListMapping.
  304. */
  305. public void addPropertyList(PropertyListMapping mapping)
  306. {
  307. mapping.addToBuilder(_treeBuilder);
  308. }
  309. /**
  310. * Add the PropertyListMapping with the given class name.
  311. */
  312. public void addPropertyList(String listClassName)
  313. throws IllegalArgumentException
  314. {
  315. try {
  316. PropertyListMapping mapping = (PropertyListMapping) Class.forName(
  317. listClassName).newInstance();
  318. addPropertyList(mapping);
  319. } catch (ClassNotFoundException e) {
  320. throw new IllegalArgumentException("Could not find " + listClassName);
  321. }
  322. catch (InstantiationException e) {
  323. throw new IllegalArgumentException("Could not instantiate " +
  324. listClassName);
  325. }
  326. catch (IllegalAccessException e) {
  327. throw new IllegalArgumentException("Could not access " + listClassName);
  328. }
  329. catch (ClassCastException e) {
  330. throw new IllegalArgumentException(listClassName + " is not an ElementMapping");
  331. }
  332. }
  333. /**
  334. * Returns the tree builder (a SAX ContentHandler).
  335. *
  336. * Used in situations where SAX is used but not via a FOP-invoked
  337. * SAX parser. A good example is an XSLT engine that fires SAX
  338. * events but isn't a SAX Parser itself.
  339. */
  340. public ContentHandler getContentHandler() {
  341. return _treeBuilder;
  342. }
  343. /**
  344. * Build the formatting object tree using the given SAX Parser and
  345. * SAX InputSource
  346. */
  347. public synchronized void buildFOTree(XMLReader parser,
  348. InputSource source)
  349. throws FOPException
  350. {
  351. parser.setContentHandler(_treeBuilder);
  352. try {
  353. parser.parse(source);
  354. } catch (SAXException e) {
  355. if (e.getException() instanceof FOPException) {
  356. throw (FOPException) e.getException();
  357. } else {
  358. throw new FOPException(e);
  359. }
  360. }
  361. catch (IOException e) {
  362. throw new FOPException(e);
  363. }
  364. }
  365. /**
  366. * Build the formatting object tree using the given DOM Document
  367. */
  368. public synchronized void buildFOTree(Document document)
  369. throws FOPException
  370. {
  371. try {
  372. DocumentInputSource source = new DocumentInputSource(document);
  373. DocumentReader reader = new DocumentReader();
  374. reader.setContentHandler(_treeBuilder);
  375. reader.parse(source);
  376. } catch (SAXException e) {
  377. throw new FOPException(e);
  378. } catch (IOException e) {
  379. throw new FOPException(e);
  380. }
  381. }
  382. /**
  383. * Dumps an error
  384. */
  385. public void dumpError(Exception e) {
  386. if (_errorDump) {
  387. if (e instanceof SAXException) {
  388. e.printStackTrace();
  389. if (((SAXException) e).getException() != null) {
  390. ((SAXException) e).getException().printStackTrace();
  391. }
  392. }
  393. else if (e instanceof FOPException) {
  394. e.printStackTrace();
  395. if (((FOPException) e).getException() != null) {
  396. ((FOPException) e).getException().printStackTrace();
  397. }
  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() throws FOPException {
  412. FontInfo fontInfo = new FontInfo();
  413. _renderer.setupFontInfo(fontInfo);
  414. _areaTree = new AreaTree();
  415. _areaTree.setFontInfo(fontInfo);
  416. _treeBuilder.format(_areaTree);
  417. }
  418. /**
  419. * render the area tree to the output form
  420. */
  421. public synchronized void render() throws IOException, FOPException {
  422. _renderer.render(_areaTree, _stream);
  423. }
  424. /**
  425. * Runs the formatting and renderering process using the previously set
  426. * inputsource and outputstream
  427. */
  428. public synchronized void run()
  429. throws IOException, FOPException
  430. {
  431. if (_renderer == null) {
  432. setRenderer(RENDER_PDF);
  433. }
  434. if (_source == null) {
  435. throw new FOPException("InputSource is not set.");
  436. }
  437. if (_reader == null) {
  438. if (!(_source instanceof DocumentInputSource)) {
  439. _reader = ConfigurationReader.createParser();
  440. }
  441. }
  442. if (_source instanceof DocumentInputSource) {
  443. buildFOTree(((DocumentInputSource)_source).getDocument());
  444. }
  445. else {
  446. buildFOTree(_reader, _source);
  447. }
  448. format();
  449. render();
  450. }
  451. }