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.

XTDriver.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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.XTFOTreeBuilder;
  43. import org.apache.fop.fo.ElementMapping;
  44. import org.apache.fop.fo.PropertyListMapping;
  45. import org.apache.fop.layout.AreaTree;
  46. import org.apache.fop.layout.FontInfo;
  47. import org.apache.fop.render.Renderer;
  48. import org.apache.fop.messaging.MessageHandler;
  49. // DOM
  50. import org.w3c.dom.Document;
  51. import org.w3c.dom.Node;
  52. import org.w3c.dom.NamedNodeMap;
  53. import org.w3c.dom.Attr;
  54. // SAX
  55. import org.xml.sax.DocumentHandler;
  56. import org.xml.sax.InputSource;
  57. import org.xml.sax.Parser;
  58. import org.xml.sax.SAXException;
  59. import org.xml.sax.helpers.AttributesImpl;
  60. // Java
  61. import java.io.PrintWriter;
  62. import java.io.IOException;
  63. /**
  64. * <P>Primary class that drives overall FOP process.
  65. *
  66. * <P>Once this class is instantiated, methods are called to set the
  67. * Renderer to use, the (possibly multiple) ElementMapping(s) to
  68. * use and the PrintWriter to use to output the results of the
  69. * rendering (where applicable). In the case of the Renderer and
  70. * ElementMapping(s), the Driver may be supplied either with the
  71. * object itself, or the name of the class, in which case Driver will
  72. * instantiate the class itself. The advantage of the latter is it
  73. * enables runtime determination of Renderer and ElementMapping(s).
  74. *
  75. * <P>Once the Driver is set up, the buildFOTree method
  76. * is called. Depending on whether DOM or SAX is being used, the
  77. * invocation of the method is either buildFOTree(Document) or
  78. * buildFOTree(Parser, InputSource) respectively.
  79. *
  80. * <P>A third possibility may be used to build the FO Tree, namely
  81. * calling getDocumentHandler() and firing the SAX events yourself.
  82. *
  83. * <P>Once the FO Tree is built, the format() and render() methods may be
  84. * called in that order.
  85. *
  86. * <P>Here is an example use of Driver from CommandLine.java:
  87. *
  88. * <PRE>
  89. * Driver driver = new Driver();
  90. * driver.setRenderer("org.apache.fop.render.pdf.PDFRenderer", version);
  91. * driver.addElementMapping("org.apache.fop.fo.StandardElementMapping");
  92. * driver.addElementMapping("org.apache.fop.svg.SVGElementMapping");
  93. * driver.setWriter(new PrintWriter(new FileWriter(args[1])));
  94. * driver.buildFOTree(parser, fileInputSource(args[0]));
  95. * driver.format();
  96. * driver.render();
  97. * </PRE>
  98. */
  99. public class XTDriver {
  100. /** the FO tree builder */
  101. protected XTFOTreeBuilder treeBuilder;
  102. /** the area tree that is the result of formatting the FO tree */
  103. protected AreaTree areaTree;
  104. /** the renderer to use to output the area tree */
  105. protected Renderer renderer;
  106. /** the PrintWriter to use to output the results of the renderer */
  107. protected PrintWriter writer;
  108. /** create a new Driver */
  109. public XTDriver() {
  110. this.treeBuilder = new XTFOTreeBuilder();
  111. }
  112. /** set the Renderer to use */
  113. public void setRenderer(Renderer renderer) {
  114. this.renderer = renderer;
  115. }
  116. /**
  117. * set the class name of the Renderer to use as well as the
  118. * producer string for those renderers that can make use of it
  119. */
  120. public void setRenderer(String rendererClassName, String producer) {
  121. this.renderer = createRenderer(rendererClassName);
  122. this.renderer.setProducer(producer);
  123. }
  124. /**
  125. * protected method used by setRenderer(String, String) to
  126. * instantiate the Renderer class
  127. */
  128. protected Renderer createRenderer(String rendererClassName) {
  129. MessageHandler.logln("using renderer " + rendererClassName);
  130. try {
  131. return (Renderer)
  132. Class.forName(rendererClassName).newInstance();
  133. } catch (ClassNotFoundException e) {
  134. MessageHandler.errorln("Could not find " + rendererClassName);
  135. } catch (InstantiationException e) {
  136. MessageHandler.errorln("Could not instantiate "
  137. + rendererClassName);
  138. } catch (IllegalAccessException e) {
  139. MessageHandler.errorln("Could not access " + rendererClassName);
  140. } catch (ClassCastException e) {
  141. MessageHandler.errorln(rendererClassName + " is not a renderer");
  142. }
  143. return null;
  144. }
  145. /**
  146. * add the given element mapping.
  147. *
  148. * an element mapping maps element names to Java classes
  149. */
  150. public void addElementMapping(ElementMapping mapping) {
  151. mapping.addToBuilder(this.treeBuilder);
  152. }
  153. /**
  154. * add the element mapping with the given class name
  155. */
  156. public void addElementMapping(String mappingClassName) {
  157. createElementMapping(mappingClassName).addToBuilder(this.treeBuilder);
  158. }
  159. /**
  160. * protected method used by addElementMapping(String) to
  161. * instantiate element mapping class
  162. */
  163. protected ElementMapping createElementMapping(String mappingClassName) {
  164. MessageHandler.logln("using element mapping " + mappingClassName);
  165. try {
  166. return (ElementMapping)
  167. Class.forName(mappingClassName).newInstance();
  168. } catch (ClassNotFoundException e) {
  169. MessageHandler.errorln("Could not find " + mappingClassName);
  170. } catch (InstantiationException e) {
  171. MessageHandler.errorln("Could not instantiate "
  172. + mappingClassName);
  173. } catch (IllegalAccessException e) {
  174. MessageHandler.errorln("Could not access " + mappingClassName);
  175. } catch (ClassCastException e) {
  176. MessageHandler.errorln(mappingClassName + " is not an element mapping");
  177. }
  178. return null;
  179. }
  180. /**
  181. * add the element mapping with the given class name
  182. */
  183. public void addPropertyList(String listClassName) {
  184. createPropertyList(listClassName).addToBuilder(this.treeBuilder);
  185. }
  186. /**
  187. * protected method used by addPropertyList(String) to
  188. * instantiate list mapping class
  189. */
  190. protected PropertyListMapping createPropertyList(String listClassName) {
  191. MessageHandler.logln("using property list mapping " + listClassName);
  192. try {
  193. return (PropertyListMapping)
  194. Class.forName(listClassName).newInstance();
  195. } catch (ClassNotFoundException e) {
  196. MessageHandler.errorln("Could not find " + listClassName);
  197. } catch (InstantiationException e) {
  198. MessageHandler.errorln("Could not instantiate "
  199. + listClassName);
  200. } catch (IllegalAccessException e) {
  201. MessageHandler.errorln("Could not access " + listClassName);
  202. } catch (ClassCastException e) {
  203. MessageHandler.errorln(listClassName + " is not an property list");
  204. }
  205. return null;
  206. }
  207. /**
  208. * return the tree builder (a SAX DocumentHandler).
  209. *
  210. * used in situations where SAX is used but not via a FOP-invoked
  211. * SAX parser. A good example is an XSLT engine that fires SAX
  212. * events but isn't a SAX Parser itself.
  213. */
  214. public DocumentHandler getDocumentHandler() {
  215. return this.treeBuilder;
  216. }
  217. /**
  218. * build the formatting object tree using the given SAX Parser and
  219. * SAX InputSource
  220. */
  221. public void buildFOTree(Parser parser, InputSource source)
  222. throws FOPException {
  223. parser.setDocumentHandler(this.treeBuilder);
  224. try {
  225. parser.parse(source);
  226. } catch (SAXException e) {
  227. if (e.getException() instanceof FOPException)
  228. throw (FOPException) e.getException();
  229. else
  230. throw new FOPException(e.getMessage());
  231. } catch (IOException e) {
  232. throw new FOPException(e.getMessage());
  233. }
  234. }
  235. /**
  236. * build the formatting object tree using the given DOM Document
  237. */
  238. public void buildFOTree(Document document)
  239. throws FOPException {
  240. /* most of this code is modified from John Cowan's */
  241. Node currentNode;
  242. AttributesImpl currentAtts;
  243. /* temporary array for making Strings into character arrays */
  244. char[] array = null;
  245. currentAtts = new AttributesImpl();
  246. /* start at the document element */
  247. currentNode = document;
  248. try {
  249. while (currentNode != null) {
  250. switch (currentNode.getNodeType()) {
  251. case Node.DOCUMENT_NODE:
  252. this.treeBuilder.startDocument();
  253. break;
  254. case Node.CDATA_SECTION_NODE:
  255. case Node.TEXT_NODE:
  256. String data = currentNode.getNodeValue();
  257. int datalen = data.length();
  258. if (array == null || array.length < datalen) {
  259. /* if the array isn't big enough, make a new
  260. one */
  261. array = new char[datalen];
  262. }
  263. data.getChars(0, datalen, array, 0);
  264. this.treeBuilder.characters(array, 0, datalen);
  265. break;
  266. case Node.PROCESSING_INSTRUCTION_NODE:
  267. this.treeBuilder.processingInstruction(
  268. currentNode.getNodeName(),
  269. currentNode.getNodeValue());
  270. break;
  271. case Node.ELEMENT_NODE:
  272. NamedNodeMap map = currentNode.getAttributes();
  273. currentAtts.clear();
  274. for (int i = map.getLength() - 1; i >= 0; i--) {
  275. Attr att = (Attr)map.item(i);
  276. currentAtts.addAttribute("",
  277. att.getName(),
  278. "",
  279. "CDATA",
  280. att.getValue());
  281. }
  282. this.treeBuilder.startElement(
  283. "", currentNode.getNodeName(), "", currentAtts);
  284. break;
  285. }
  286. Node nextNode = currentNode.getFirstChild();
  287. if (nextNode != null) {
  288. currentNode = nextNode;
  289. continue;
  290. }
  291. while (currentNode != null) {
  292. switch (currentNode.getNodeType()) {
  293. case Node.DOCUMENT_NODE:
  294. this.treeBuilder.endDocument();
  295. break;
  296. case Node.ELEMENT_NODE:
  297. this.treeBuilder.endElement(
  298. "", currentNode.getNodeName(), "" );
  299. break;
  300. }
  301. nextNode = currentNode.getNextSibling();
  302. if (nextNode != null) {
  303. currentNode = nextNode;
  304. break;
  305. }
  306. currentNode = currentNode.getParentNode();
  307. }
  308. }
  309. } catch (SAXException e) {
  310. throw new FOPException(e.getMessage());
  311. }
  312. }
  313. /**
  314. * set the PrintWriter to use to output the result of the Renderer
  315. * (if applicable)
  316. */
  317. public void setWriter(PrintWriter writer) {
  318. this.writer = writer;
  319. }
  320. /**
  321. * format the formatting object tree into an area tree
  322. */
  323. public void format()
  324. throws FOPException {
  325. FontInfo fontInfo = new FontInfo();
  326. this.renderer.setupFontInfo(fontInfo);
  327. this.areaTree = new AreaTree();
  328. this.areaTree.setFontInfo(fontInfo);
  329. this.treeBuilder.format(areaTree);
  330. }
  331. /**
  332. * render the area tree to the output form
  333. */
  334. public void render()
  335. throws IOException, FOPException {
  336. this.renderer.render(areaTree, this.writer);
  337. }
  338. }