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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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.fo;
  41. // FOP
  42. import org.apache.fop.layout.AreaTree;
  43. import org.apache.fop.messaging.MessageHandler;
  44. import org.apache.fop.apps.FOPException;
  45. import org.apache.fop.fo.pagination.Root;
  46. import org.apache.fop.system.BufferManager;
  47. // SAX
  48. import org.xml.sax.helpers.DefaultHandler;
  49. import org.xml.sax.SAXException;
  50. import org.xml.sax.InputSource;
  51. import org.xml.sax.Attributes;
  52. // Java
  53. import java.util.Hashtable;
  54. import java.util.Stack;
  55. import java.io.IOException;
  56. /**
  57. * SAX Handler that builds the formatting object tree.
  58. */
  59. public class FOTreeBuilder extends DefaultHandler implements TreeBuilder {
  60. /**
  61. * table mapping element names to the makers of objects
  62. * representing formatting objects
  63. */
  64. protected Hashtable fobjTable = new Hashtable();
  65. /**
  66. * class that builds a property list for each formatting object
  67. */
  68. protected Hashtable propertylistTable = new Hashtable();
  69. /**
  70. * current formatting object being handled
  71. */
  72. protected FObj currentFObj = null;
  73. /**
  74. * the root of the formatting object tree
  75. */
  76. protected FObj rootFObj = null;
  77. public BufferManager bufferManager;
  78. /**
  79. * set of names of formatting objects encountered but unknown
  80. */
  81. protected Hashtable unknownFOs = new Hashtable();
  82. // namespace implementation ideas pinched from John Cowan
  83. // protected static class NSMap {
  84. // String prefix;
  85. // String uri;
  86. // int level;
  87. // NSMap(String prefix, String uri, int level) {
  88. // this.prefix = prefix;
  89. // this.uri = uri;
  90. // this.level = level;
  91. // }
  92. // }
  93. // protected int level = 0;
  94. // protected Stack namespaceStack = new Stack();
  95. // {
  96. // namespaceStack.push(new NSMap("xml",
  97. // "http://www.w3.org/XML/1998/namespace",
  98. // -1));
  99. // namespaceStack.push(new NSMap("", "", -1));
  100. // }
  101. // protected String findURI(String prefix) {
  102. // for (int i = namespaceStack.size() - 1; i >= 0; i--) {
  103. // NSMap nsMap = (NSMap) (namespaceStack.elementAt(i));
  104. // if (prefix.equals(nsMap.prefix)) return nsMap.uri;
  105. // }
  106. // return null;
  107. // }
  108. // protected String mapName(String name)
  109. // throws SAXException {
  110. // int colon = name.indexOf(':');
  111. // String prefix = "";
  112. // String localPart = name;
  113. // if (colon != -1) {
  114. // prefix = name.substring(0, colon);
  115. // localPart = name.substring(colon + 1);
  116. // }
  117. // String uri = findURI(prefix);
  118. // if (uri == null) {
  119. // if (prefix.equals("")) {
  120. // return name;
  121. // } else {
  122. // throw new SAXException(new FOPException("Unknown namespace prefix " + prefix));
  123. // }
  124. // }
  125. // return uri + "^" + localPart;
  126. // }
  127. /**
  128. * add a mapping from element name to maker.
  129. *
  130. * @param namespaceURI namespace URI of formatting object element
  131. * @param localName local name of formatting object element
  132. * @param maker Maker for class representing formatting object
  133. */
  134. public void addMapping(String namespaceURI, String localName,
  135. FObj.Maker maker) {
  136. this.fobjTable.put(namespaceURI + "^" + localName, maker);
  137. }
  138. /**
  139. * add a mapping from element name to maker.
  140. *
  141. * @param namespaceURI namespace URI of formatting object element
  142. * @param localName local name of formatting object element
  143. * @param maker Maker for class representing formatting object
  144. */
  145. public void addPropertyList(String namespaceURI, Hashtable list) {
  146. PropertyListBuilder plb;
  147. plb = (PropertyListBuilder) this.propertylistTable.get(
  148. namespaceURI);
  149. if (plb == null) {
  150. plb = new PropertyListBuilder();
  151. plb.addList(list);
  152. this.propertylistTable.put(namespaceURI, plb);
  153. } else {
  154. plb.addList(list);
  155. }
  156. }
  157. /**
  158. * add a mapping from element name to maker.
  159. *
  160. * @param namespaceURI namespace URI of formatting object element
  161. * @param localName local name of formatting object element
  162. * @param maker Maker for class representing formatting object
  163. */
  164. public void addElementPropertyList(String namespaceURI,
  165. String localName, Hashtable list) {
  166. PropertyListBuilder plb;
  167. plb = (PropertyListBuilder) this.propertylistTable.get(
  168. namespaceURI);
  169. if (plb == null) {
  170. plb = new PropertyListBuilder();
  171. plb.addElementList(localName, list);
  172. this.propertylistTable.put(namespaceURI, plb);
  173. } else {
  174. plb.addElementList(localName, list);
  175. }
  176. }
  177. /** SAX Handler for characters */
  178. public void characters(char data[], int start, int length) {
  179. currentFObj.addCharacters(data, start, start + length);
  180. }
  181. /** SAX Handler for the end of an element */
  182. public void endElement(String uri, String localName, String rawName) {
  183. currentFObj.end();
  184. currentFObj = (FObj) currentFObj.getParent();
  185. // level--;
  186. // while (((NSMap) namespaceStack.peek()).level > level) {
  187. // namespaceStack.pop();
  188. // }
  189. }
  190. /** SAX Handler for the start of the document */
  191. public void startDocument() {
  192. MessageHandler.logln("building formatting object tree");
  193. }
  194. /** SAX Handler for the start of an element */
  195. public void startElement(String uri, String localName,
  196. String rawName, Attributes attlist) throws SAXException {
  197. /* the formatting object started */
  198. FObj fobj;
  199. /* the maker for the formatting object started */
  200. FObj.Maker fobjMaker;
  201. // level++;
  202. // int length = attlist.getLength();
  203. // for (int i = 0; i < length; i++) {
  204. // String att = attlist.getQName(i);
  205. // if (att.equals("xmlns")) {
  206. // namespaceStack.push( new NSMap("",
  207. // attlist.getValue(i),
  208. // level));
  209. // } else if (att.startsWith("xmlns:")) {
  210. // String value = attlist.getValue(i);
  211. // namespaceStack.push(new NSMap(att.substring(6), value,
  212. // level));
  213. // }
  214. // }
  215. //String fullName = mapName(rawName);
  216. String fullName = uri + "^" + localName;
  217. fobjMaker = (FObj.Maker) fobjTable.get(fullName);
  218. PropertyListBuilder currentListBuilder =
  219. (PropertyListBuilder) this.propertylistTable.get(uri);
  220. if (fobjMaker == null) {
  221. if (!this.unknownFOs.containsKey(fullName)) {
  222. this.unknownFOs.put(fullName, "");
  223. MessageHandler.errorln(
  224. "WARNING: Unknown formatting object " + fullName);
  225. }
  226. fobjMaker = new FObjMixed.Maker(); // fall back
  227. }
  228. try {
  229. PropertyList list = null;
  230. if (currentListBuilder != null) {
  231. list = currentListBuilder.makeList(fullName, attlist,
  232. (currentFObj == null) ? null :
  233. currentFObj.properties, currentFObj);
  234. } else {
  235. list = currentFObj.properties;
  236. }
  237. fobj = fobjMaker.make(currentFObj, list);
  238. } catch (FOPException e) {
  239. throw new SAXException(e);
  240. }
  241. if (rootFObj == null) {
  242. rootFObj = fobj;
  243. rootFObj.setBufferManager(this.bufferManager);
  244. if (!fobj.getName().equals("fo:root")) {
  245. throw new SAXException(
  246. new FOPException("Root element must" +
  247. " be root, not " + fobj.getName()));
  248. }
  249. } else {
  250. currentFObj.addChild(fobj);
  251. }
  252. currentFObj = fobj;
  253. }
  254. /**
  255. * format this formatting object tree
  256. *
  257. * @param areaTree the area tree to format into
  258. */
  259. public void format(AreaTree areaTree) throws FOPException {
  260. MessageHandler.logln("formatting FOs into areas");
  261. this.bufferManager.readComplete();
  262. ((Root) this.rootFObj).format(areaTree);
  263. }
  264. public void reset()
  265. {
  266. currentFObj = null;
  267. rootFObj = null;
  268. }
  269. public boolean hasData()
  270. {
  271. return (rootFObj != null);
  272. }
  273. public void setBufferManager(BufferManager bufferManager) {
  274. this.bufferManager = bufferManager;
  275. }
  276. }