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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. public Renderer getRenderer() {
  231. return _renderer;
  232. }
  233. /**
  234. * @deprecated use renderer.setProducer(version) + setRenderer(renderer) or just setRenderer(renderer_type) which will use the default producer string.
  235. * @see #setRenderer(int)
  236. * @see #setRenderer(Renderer)
  237. */
  238. public void setRenderer(String rendererClassName, String version)
  239. {
  240. setRenderer(rendererClassName);
  241. }
  242. /**
  243. * Set the class name of the Renderer to use as well as the
  244. * producer string for those renderers that can make use of it.
  245. * @param rendererClassName classname of the renderer to use such as
  246. * "org.apache.fop.render.pdf.PDFRenderer"
  247. * @exception IllegalArgumentException if the classname was invalid.
  248. * @see #setRenderer(int)
  249. */
  250. public void setRenderer(String rendererClassName)
  251. throws IllegalArgumentException
  252. {
  253. try {
  254. _renderer = (Renderer) Class.forName(rendererClassName).newInstance();
  255. _renderer.setProducer(Version.getVersion());
  256. }
  257. catch (ClassNotFoundException e) {
  258. throw new IllegalArgumentException("Could not find " +
  259. rendererClassName);
  260. }
  261. catch (InstantiationException e) {
  262. throw new IllegalArgumentException("Could not instantiate " +
  263. rendererClassName);
  264. }
  265. catch (IllegalAccessException e) {
  266. throw new IllegalArgumentException("Could not access " + rendererClassName);
  267. }
  268. catch (ClassCastException e) {
  269. throw new IllegalArgumentException(rendererClassName + " is not a renderer");
  270. }
  271. }
  272. /**
  273. * Add the given element mapping.
  274. * An element mapping maps element names to Java classes.
  275. *
  276. * @param mapping the element mappingto add
  277. */
  278. public void addElementMapping(ElementMapping mapping) {
  279. mapping.addToBuilder(_treeBuilder);
  280. }
  281. /**
  282. * add the element mapping with the given class name
  283. */
  284. public void addElementMapping(String mappingClassName)
  285. throws IllegalArgumentException
  286. {
  287. try {
  288. ElementMapping mapping = (ElementMapping) Class.forName(
  289. mappingClassName).newInstance();
  290. addElementMapping(mapping);
  291. } catch (ClassNotFoundException e) {
  292. throw new IllegalArgumentException("Could not find " + mappingClassName);
  293. }
  294. catch (InstantiationException e) {
  295. throw new IllegalArgumentException("Could not instantiate " +
  296. mappingClassName);
  297. }
  298. catch (IllegalAccessException e) {
  299. throw new IllegalArgumentException("Could not access " + mappingClassName);
  300. }
  301. catch (ClassCastException e) {
  302. throw new IllegalArgumentException(mappingClassName + " is not an ElementMapping");
  303. }
  304. }
  305. /**
  306. * Add the PropertyListMapping.
  307. */
  308. public void addPropertyList(PropertyListMapping mapping)
  309. {
  310. mapping.addToBuilder(_treeBuilder);
  311. }
  312. /**
  313. * Add the PropertyListMapping with the given class name.
  314. */
  315. public void addPropertyList(String listClassName)
  316. throws IllegalArgumentException
  317. {
  318. try {
  319. PropertyListMapping mapping = (PropertyListMapping) Class.forName(
  320. listClassName).newInstance();
  321. addPropertyList(mapping);
  322. } catch (ClassNotFoundException e) {
  323. throw new IllegalArgumentException("Could not find " + listClassName);
  324. }
  325. catch (InstantiationException e) {
  326. throw new IllegalArgumentException("Could not instantiate " +
  327. listClassName);
  328. }
  329. catch (IllegalAccessException e) {
  330. throw new IllegalArgumentException("Could not access " + 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. {
  354. parser.setContentHandler(_treeBuilder);
  355. try {
  356. parser.parse(source);
  357. } catch (SAXException e) {
  358. if (e.getException() instanceof FOPException) {
  359. throw (FOPException) e.getException();
  360. } else {
  361. throw new FOPException(e);
  362. }
  363. }
  364. catch (IOException e) {
  365. throw new FOPException(e);
  366. }
  367. }
  368. /**
  369. * Build the formatting object tree using the given DOM Document
  370. */
  371. public synchronized void buildFOTree(Document document)
  372. throws FOPException
  373. {
  374. try {
  375. DocumentInputSource source = new DocumentInputSource(document);
  376. DocumentReader reader = new DocumentReader();
  377. reader.setContentHandler(_treeBuilder);
  378. reader.parse(source);
  379. } catch (SAXException e) {
  380. throw new FOPException(e);
  381. } catch (IOException e) {
  382. throw new FOPException(e);
  383. }
  384. }
  385. /**
  386. * Dumps an error
  387. */
  388. public void dumpError(Exception e) {
  389. if (_errorDump) {
  390. if (e instanceof SAXException) {
  391. e.printStackTrace();
  392. if (((SAXException) e).getException() != null) {
  393. ((SAXException) e).getException().printStackTrace();
  394. }
  395. }
  396. else if (e instanceof FOPException) {
  397. e.printStackTrace();
  398. if (((FOPException) e).getException() != null) {
  399. ((FOPException) e).getException().printStackTrace();
  400. }
  401. }
  402. else {
  403. e.printStackTrace();
  404. }
  405. }
  406. }
  407. /* Set up the system buffers */
  408. public void setBufferFile(File bufferFile) {
  409. this._bufferManager.addBufferFile(bufferFile);
  410. }
  411. /**
  412. * format the formatting object tree into an area tree
  413. */
  414. public synchronized void format() throws FOPException {
  415. FontInfo fontInfo = new FontInfo();
  416. _renderer.setupFontInfo(fontInfo);
  417. _areaTree = new AreaTree();
  418. _areaTree.setFontInfo(fontInfo);
  419. _treeBuilder.format(_areaTree);
  420. }
  421. /**
  422. * render the area tree to the output form
  423. */
  424. public synchronized void render() throws IOException, FOPException {
  425. _renderer.render(_areaTree, _stream);
  426. }
  427. /**
  428. * Runs the formatting and renderering process using the previously set
  429. * inputsource and outputstream
  430. */
  431. public synchronized void run()
  432. throws IOException, FOPException
  433. {
  434. if (_renderer == null) {
  435. setRenderer(RENDER_PDF);
  436. }
  437. if (_source == null) {
  438. throw new FOPException("InputSource is not set.");
  439. }
  440. if (_reader == null) {
  441. if (!(_source instanceof DocumentInputSource)) {
  442. _reader = ConfigurationReader.createParser();
  443. }
  444. }
  445. if (_source instanceof DocumentInputSource) {
  446. buildFOTree(((DocumentInputSource)_source).getDocument());
  447. }
  448. else {
  449. buildFOTree(_reader, _source);
  450. }
  451. format();
  452. render();
  453. }
  454. }