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

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