Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Driver.java 14KB

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