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.

FOTreeBuilder.java 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * $Id: FOTreeBuilder.java,v 1.43 2003/03/05 21:48:01 jeremias Exp $
  3. * ============================================================================
  4. * The Apache Software License, Version 1.1
  5. * ============================================================================
  6. *
  7. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modifica-
  10. * tion, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if any, must
  20. * include the following acknowledgment: "This product includes software
  21. * developed by the Apache Software Foundation (http://www.apache.org/)."
  22. * Alternately, this acknowledgment may appear in the software itself, if
  23. * and wherever such third-party acknowledgments normally appear.
  24. *
  25. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  26. * endorse or promote products derived from this software without prior
  27. * written permission. For written permission, please contact
  28. * apache@apache.org.
  29. *
  30. * 5. Products derived from this software may not be called "Apache", nor may
  31. * "Apache" appear in their name, without prior written permission of the
  32. * Apache Software Foundation.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  37. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  38. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  39. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. * ============================================================================
  45. *
  46. * This software consists of voluntary contributions made by many individuals
  47. * on behalf of the Apache Software Foundation and was originally created by
  48. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  49. * Software Foundation, please see <http://www.apache.org/>.
  50. */
  51. package org.apache.fop.fo;
  52. // FOP
  53. import org.apache.fop.apps.FOPException;
  54. import org.apache.fop.apps.StructureHandler;
  55. // Java
  56. import java.util.HashMap;
  57. import java.util.Map;
  58. import java.util.Set;
  59. // SAX
  60. import org.xml.sax.helpers.DefaultHandler;
  61. import org.xml.sax.SAXException;
  62. import org.xml.sax.Attributes;
  63. // Avalon
  64. import org.apache.avalon.framework.logger.Logger;
  65. /**
  66. * SAX Handler that builds the formatting object tree.
  67. *
  68. * Modified by Mark Lillywhite mark-fop@inomial.com. Now uses
  69. * StreamRenderer to automagically render the document as
  70. * soon as it receives a page-sequence end-tag. Also,
  71. * calls methods to set up and shut down the renderer at
  72. * the beginning and end of the FO document. Finally,
  73. * supresses adding the PageSequence object to the Root,
  74. * since it is parsed immediately.
  75. */
  76. public class FOTreeBuilder extends DefaultHandler {
  77. /**
  78. * Table mapping element names to the makers of objects
  79. * representing formatting objects.
  80. */
  81. protected Map fobjTable = new java.util.HashMap();
  82. /**
  83. * Set of mapped namespaces.
  84. */
  85. protected Set namespaces = new java.util.HashSet();
  86. /**
  87. * Current formatting object being handled
  88. */
  89. protected FONode currentFObj = null;
  90. /**
  91. * The root of the formatting object tree
  92. */
  93. protected FONode rootFObj = null;
  94. /**
  95. * The class that handles formatting and rendering to a stream
  96. * (mark-fop@inomial.com)
  97. */
  98. private StructureHandler structHandler;
  99. private FOUserAgent userAgent;
  100. /**
  101. * Default constructor
  102. */
  103. public FOTreeBuilder() {
  104. }
  105. private Logger getLogger() {
  106. return userAgent.getLogger();
  107. }
  108. /**
  109. * Sets the user agent
  110. * @param ua the user agent
  111. */
  112. public void setUserAgent(FOUserAgent ua) {
  113. userAgent = ua;
  114. }
  115. private FOUserAgent getUserAgent() {
  116. return userAgent;
  117. }
  118. /**
  119. * Sets the structure handler to receive events.
  120. * @param sh StructureHandler instance
  121. */
  122. public void setStructHandler(StructureHandler sh) {
  123. this.structHandler = sh;
  124. }
  125. /**
  126. * Adds a mapping from a namespace to a table of makers.
  127. *
  128. * @param namespaceURI namespace URI of formatting object elements
  129. * @param table table of makers
  130. */
  131. public void addMapping(String namespaceURI, HashMap table) {
  132. this.fobjTable.put(namespaceURI, table);
  133. this.namespaces.add(namespaceURI.intern());
  134. }
  135. /**
  136. * SAX Handler for characters
  137. * @see org.xml.sax.ContentHandler#characters(char[], int, int)
  138. */
  139. public void characters(char data[], int start, int length) {
  140. if (currentFObj != null) {
  141. currentFObj.addCharacters(data, start, start + length);
  142. }
  143. }
  144. /**
  145. * SAX Handler for the end of an element
  146. * @see org.xml.sax.ContentHandler#endElement(String, String, String)
  147. */
  148. public void endElement(String uri, String localName, String rawName)
  149. throws SAXException {
  150. currentFObj.end();
  151. currentFObj = currentFObj.getParent();
  152. }
  153. /**
  154. * SAX Handler for the start of the document
  155. * @see org.xml.sax.ContentHandler#startDocument()
  156. */
  157. public void startDocument() throws SAXException {
  158. rootFObj = null; // allows FOTreeBuilder to be reused
  159. if (getLogger().isDebugEnabled()) {
  160. getLogger().debug("Building formatting object tree");
  161. }
  162. structHandler.startDocument();
  163. }
  164. /**
  165. * SAX Handler for the end of the document
  166. * @see org.xml.sax.ContentHandler#endDocument()
  167. */
  168. public void endDocument() throws SAXException {
  169. rootFObj = null;
  170. currentFObj = null;
  171. if (getLogger().isDebugEnabled()) {
  172. getLogger().debug("Parsing of document complete");
  173. }
  174. structHandler.endDocument();
  175. }
  176. /**
  177. * SAX Handler for the start of an element
  178. * @see org.xml.sax.ContentHandler#startElement(String, String, String, Attributes)
  179. */
  180. public void startElement(String uri, String localName, String rawName,
  181. Attributes attlist) throws SAXException {
  182. /* the formatting object started */
  183. FONode fobj;
  184. /* the maker for the formatting object started */
  185. ElementMapping.Maker fobjMaker = null;
  186. Map table = (Map)fobjTable.get(uri);
  187. if (table != null) {
  188. fobjMaker = (ElementMapping.Maker)table.get(localName);
  189. // try default
  190. if (fobjMaker == null) {
  191. fobjMaker = (ElementMapping.Maker)table.get(ElementMapping.DEFAULT);
  192. }
  193. }
  194. if (fobjMaker == null) {
  195. if (getLogger().isWarnEnabled()) {
  196. getLogger().warn("Unknown formatting object " + uri + "^" + localName);
  197. }
  198. if (namespaces.contains(uri.intern())) {
  199. // fall back
  200. fobjMaker = new Unknown.Maker();
  201. } else {
  202. fobjMaker = new UnknownXMLObj.Maker(uri);
  203. }
  204. }
  205. try {
  206. fobj = fobjMaker.make(currentFObj);
  207. fobj.setName(localName);
  208. // set the user agent for resolving user agent values
  209. fobj.setUserAgent(userAgent);
  210. // set the structure handler so that appropriate
  211. // elements can signal structure events
  212. fobj.setStructHandler(structHandler);
  213. fobj.handleAttrs(attlist);
  214. } catch (FOPException e) {
  215. throw new SAXException(e);
  216. }
  217. if (rootFObj == null) {
  218. if (!fobj.getName().equals("fo:root")) {
  219. throw new SAXException(new FOPException("Root element must"
  220. + " be fo:root, not "
  221. + fobj.getName()));
  222. }
  223. rootFObj = fobj;
  224. } else {
  225. currentFObj.addChild(fobj);
  226. }
  227. currentFObj = fobj;
  228. }
  229. /**
  230. * Resets this object for another run.
  231. */
  232. public void reset() {
  233. currentFObj = null;
  234. rootFObj = null;
  235. structHandler = null;
  236. }
  237. /**
  238. * Indicates if data has been processed.
  239. * @return True if data has been processed
  240. */
  241. public boolean hasData() {
  242. return (rootFObj != null);
  243. }
  244. }