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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*-- $Id$ --
  2. ============================================================================
  3. The Apache Software License, Version 1.1
  4. ============================================================================
  5. Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modifica-
  7. tion, are permitted provided that the following conditions are met:
  8. 1. Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright notice,
  11. this list of conditions and the following disclaimer in the documentation
  12. and/or other materials provided with the distribution.
  13. 3. The end-user documentation included with the redistribution, if any, must
  14. include the following acknowledgment: "This product includes software
  15. developed by the Apache Software Foundation (http://www.apache.org/)."
  16. Alternately, this acknowledgment may appear in the software itself, if
  17. and wherever such third-party acknowledgments normally appear.
  18. 4. The names "FOP" and "Apache Software Foundation" must not be used to
  19. endorse or promote products derived from this software without prior
  20. written permission. For written permission, please contact
  21. apache@apache.org.
  22. 5. Products derived from this software may not be called "Apache", nor may
  23. "Apache" appear in their name, without prior written permission of the
  24. Apache Software Foundation.
  25. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  26. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  27. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  29. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  30. DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  31. OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  32. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  34. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. This software consists of voluntary contributions made by many individuals
  36. on behalf of the Apache Software Foundation and was originally created by
  37. James Tauber <jtauber@jtauber.com>. For more information on the Apache
  38. Software Foundation, please see <http://www.apache.org/>.
  39. */
  40. package org.apache.fop.apps;
  41. // FOP
  42. import org.apache.fop.fo.FOTreeBuilder;
  43. import org.apache.fop.fo.ElementMapping;
  44. import org.apache.fop.layout.AreaTree;
  45. import org.apache.fop.layout.FontInfo;
  46. import org.apache.fop.render.Renderer;
  47. import org.apache.fop.messaging.MessageHandler;
  48. // DOM
  49. import org.w3c.dom.Document;
  50. import org.w3c.dom.Node;
  51. import org.w3c.dom.NamedNodeMap;
  52. import org.w3c.dom.Attr;
  53. // SAX
  54. import org.xml.sax.DocumentHandler;
  55. import org.xml.sax.InputSource;
  56. import org.xml.sax.Parser;
  57. import org.xml.sax.SAXException;
  58. import org.xml.sax.helpers.AttributeListImpl;
  59. // Java
  60. import java.io.PrintWriter;
  61. import java.io.IOException;
  62. /**
  63. * <P>Primary class that drives overall FOP process.
  64. *
  65. * <P>Once this class is instantiated, methods are called to set the
  66. * Renderer to use, the (possibly multiple) ElementMapping(s) to
  67. * use and the PrintWriter to use to output the results of the
  68. * rendering (where applicable). In the case of the Renderer and
  69. * ElementMapping(s), the Driver may be supplied either with the
  70. * object itself, or the name of the class, in which case Driver will
  71. * instantiate the class itself. The advantage of the latter is it
  72. * enables runtime determination of Renderer and ElementMapping(s).
  73. *
  74. * <P>Once the Driver is set up, the buildFOTree method
  75. * is called. Depending on whether DOM or SAX is being used, the
  76. * invocation of the method is either buildFOTree(Document) or
  77. * buildFOTree(Parser, InputSource) respectively.
  78. *
  79. * <P>A third possibility may be used to build the FO Tree, namely
  80. * calling getDocumentHandler() and firing the SAX events yourself.
  81. *
  82. * <P>Once the FO Tree is built, the format() and render() methods may be
  83. * called in that order.
  84. *
  85. * <P>Here is an example use of Driver from CommandLine.java:
  86. *
  87. * <PRE>
  88. * Driver driver = new Driver();
  89. * driver.setRenderer("org.apache.fop.render.pdf.PDFRenderer", version);
  90. * driver.addElementMapping("org.apache.fop.fo.StandardElementMapping");
  91. * driver.addElementMapping("org.apache.fop.svg.SVGElementMapping");
  92. * driver.setWriter(new PrintWriter(new FileWriter(args[1])));
  93. * driver.buildFOTree(parser, fileInputSource(args[0]));
  94. * driver.format();
  95. * driver.render();
  96. * </PRE>
  97. */
  98. public class Driver {
  99. /** the FO tree builder */
  100. protected FOTreeBuilder treeBuilder;
  101. /** the area tree that is the result of formatting the FO tree */
  102. protected AreaTree areaTree;
  103. /** the renderer to use to output the area tree */
  104. protected Renderer renderer;
  105. /** the PrintWriter to use to output the results of the renderer */
  106. protected PrintWriter writer;
  107. /** create a new Driver */
  108. public Driver() {
  109. this.treeBuilder = new FOTreeBuilder();
  110. }
  111. /** set the Renderer to use */
  112. public void setRenderer(Renderer renderer) {
  113. this.renderer = renderer;
  114. }
  115. /**
  116. * set the class name of the Renderer to use as well as the
  117. * producer string for those renderers that can make use of it
  118. */
  119. public void setRenderer(String rendererClassName, String producer) {
  120. this.renderer = createRenderer(rendererClassName);
  121. this.renderer.setProducer(producer);
  122. }
  123. /**
  124. * protected method used by setRenderer(String, String) to
  125. * instantiate the Renderer class
  126. */
  127. protected Renderer createRenderer(String rendererClassName) {
  128. MessageHandler.logln("using renderer " + rendererClassName);
  129. try {
  130. return (Renderer)
  131. Class.forName(rendererClassName).newInstance();
  132. } catch (ClassNotFoundException e) {
  133. MessageHandler.errorln("Could not find " + rendererClassName);
  134. } catch (InstantiationException e) {
  135. MessageHandler.errorln("Could not instantiate "
  136. + rendererClassName);
  137. } catch (IllegalAccessException e) {
  138. MessageHandler.errorln("Could not access " + rendererClassName);
  139. } catch (ClassCastException e) {
  140. MessageHandler.errorln(rendererClassName + " is not a renderer");
  141. }
  142. return null;
  143. }
  144. /**
  145. * add the given element mapping.
  146. *
  147. * an element mapping maps element names to Java classes
  148. */
  149. public void addElementMapping(ElementMapping mapping) {
  150. mapping.addToBuilder(this.treeBuilder);
  151. }
  152. /**
  153. * add the element mapping with the given class name
  154. */
  155. public void addElementMapping(String mappingClassName) {
  156. createElementMapping(mappingClassName).addToBuilder(this.treeBuilder);
  157. }
  158. /**
  159. * protected method used by addElementMapping(String) to
  160. * instantiate element mapping class
  161. */
  162. protected ElementMapping createElementMapping(String mappingClassName) {
  163. MessageHandler.logln("using element mapping " + mappingClassName);
  164. try {
  165. return (ElementMapping)
  166. Class.forName(mappingClassName).newInstance();
  167. } catch (ClassNotFoundException e) {
  168. MessageHandler.errorln("Could not find " + mappingClassName);
  169. } catch (InstantiationException e) {
  170. MessageHandler.errorln("Could not instantiate "
  171. + mappingClassName);
  172. } catch (IllegalAccessException e) {
  173. MessageHandler.errorln("Could not access " + mappingClassName);
  174. } catch (ClassCastException e) {
  175. MessageHandler.errorln(mappingClassName + " is not an element mapping");
  176. }
  177. return null;
  178. }
  179. /**
  180. * return the tree builder (a SAX DocumentHandler).
  181. *
  182. * used in situations where SAX is used but not via a FOP-invoked
  183. * SAX parser. A good example is an XSLT engine that fires SAX
  184. * events but isn't a SAX Parser itself.
  185. */
  186. public DocumentHandler getDocumentHandler() {
  187. return this.treeBuilder;
  188. }
  189. /**
  190. * build the formatting object tree using the given SAX Parser and
  191. * SAX InputSource
  192. */
  193. public void buildFOTree(Parser parser, InputSource source)
  194. throws FOPException {
  195. parser.setDocumentHandler(this.treeBuilder);
  196. try {
  197. parser.parse(source);
  198. } catch (SAXException e) {
  199. if (e.getException() instanceof FOPException)
  200. throw (FOPException) e.getException();
  201. else
  202. throw new FOPException(e.getMessage());
  203. } catch (IOException e) {
  204. throw new FOPException(e.getMessage());
  205. }
  206. }
  207. /**
  208. * build the formatting object tree using the given DOM Document
  209. */
  210. public void buildFOTree(Document document)
  211. throws FOPException {
  212. /* most of this code is modified from John Cowan's */
  213. Node currentNode;
  214. AttributeListImpl currentAtts;
  215. /* temporary array for making Strings into character arrays */
  216. char[] array = null;
  217. currentAtts = new AttributeListImpl();
  218. /* start at the document element */
  219. currentNode = document;
  220. try {
  221. while (currentNode != null) {
  222. switch (currentNode.getNodeType()) {
  223. case Node.DOCUMENT_NODE:
  224. this.treeBuilder.startDocument();
  225. break;
  226. case Node.CDATA_SECTION_NODE:
  227. case Node.TEXT_NODE:
  228. String data = currentNode.getNodeValue();
  229. int datalen = data.length();
  230. if (array == null || array.length < datalen) {
  231. /* if the array isn't big enough, make a new
  232. one */
  233. array = new char[datalen];
  234. }
  235. data.getChars(0, datalen, array, 0);
  236. this.treeBuilder.characters(array, 0, datalen);
  237. break;
  238. case Node.PROCESSING_INSTRUCTION_NODE:
  239. this.treeBuilder.processingInstruction(
  240. currentNode.getNodeName(),
  241. currentNode.getNodeValue());
  242. break;
  243. case Node.ELEMENT_NODE:
  244. NamedNodeMap map = currentNode.getAttributes();
  245. currentAtts.clear();
  246. for (int i = map.getLength() - 1; i >= 0; i--) {
  247. Attr att = (Attr)(map.item(i));
  248. currentAtts.addAttribute(att.getName(),
  249. "CDATA",
  250. att.getValue());
  251. }
  252. this.treeBuilder.startElement(
  253. currentNode.getNodeName(), currentAtts);
  254. break;
  255. }
  256. Node nextNode = currentNode.getFirstChild();
  257. if (nextNode != null) {
  258. currentNode = nextNode;
  259. continue;
  260. }
  261. while (currentNode != null) {
  262. switch (currentNode.getNodeType()) {
  263. case Node.DOCUMENT_NODE:
  264. this.treeBuilder.endDocument();
  265. break;
  266. case Node.ELEMENT_NODE:
  267. this.treeBuilder.endElement(
  268. currentNode.getNodeName());
  269. break;
  270. }
  271. nextNode = currentNode.getNextSibling();
  272. if (nextNode != null) {
  273. currentNode = nextNode;
  274. break;
  275. }
  276. currentNode = currentNode.getParentNode();
  277. }
  278. }
  279. } catch (SAXException e) {
  280. throw new FOPException(e.getMessage());
  281. }
  282. }
  283. /**
  284. * set the PrintWriter to use to output the result of the Renderer
  285. * (if applicable)
  286. */
  287. public void setWriter(PrintWriter writer) {
  288. this.writer = writer;
  289. }
  290. /**
  291. * format the formatting object tree into an area tree
  292. */
  293. public void format()
  294. throws FOPException {
  295. FontInfo fontInfo = new FontInfo();
  296. this.renderer.setupFontInfo(fontInfo);
  297. this.areaTree = new AreaTree();
  298. this.areaTree.setFontInfo(fontInfo);
  299. this.treeBuilder.format(areaTree);
  300. }
  301. /**
  302. * render the area tree to the output form
  303. */
  304. public void render()
  305. throws IOException, FOPException {
  306. this.renderer.render(areaTree, this.writer);
  307. }
  308. }