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.

XTFOTreeBuilder.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. // SAX
  47. import org.xml.sax.SAXException;
  48. import org.xml.sax.Attributes;
  49. import org.xml.sax.helpers.AttributesImpl;
  50. import org.xml.sax.DocumentHandler;
  51. import org.xml.sax.AttributeList;
  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 XTFOTreeBuilder extends HandlerBase {
  60. public class XTFOTreeBuilder extends FOTreeBuilder
  61. implements DocumentHandler {
  62. // namespace implementation ideas pinched from John Cowan
  63. protected static class NSMap {
  64. String prefix;
  65. String uri;
  66. int level;
  67. NSMap(String prefix, String uri, int level) {
  68. this.prefix = prefix;
  69. this.uri = uri;
  70. this.level = level;
  71. }
  72. }
  73. protected int level = 0;
  74. protected String m_uri = null;
  75. protected String m_localPart = null;
  76. protected Stack namespaceStack = new Stack();
  77. {
  78. namespaceStack.push(new NSMap("xml",
  79. "http://www.w3.org/XML/1998/namespace",
  80. -1));
  81. namespaceStack.push(new NSMap("", "", -1));
  82. }
  83. protected String findURI(String prefix) {
  84. for (int i = namespaceStack.size() - 1; i >= 0; i--) {
  85. NSMap nsMap = (NSMap) (namespaceStack.elementAt(i));
  86. if (prefix.equals(nsMap.prefix)) return nsMap.uri;
  87. }
  88. return null;
  89. }
  90. protected String mapName(String name)
  91. throws SAXException {
  92. int colon = name.indexOf(':');
  93. String prefix = "";
  94. m_localPart = name;
  95. if (colon != -1) {
  96. prefix = name.substring(0, colon);
  97. m_localPart = name.substring(colon + 1);
  98. }
  99. m_uri = findURI(prefix);
  100. if (m_uri == null) {
  101. if (prefix.equals("")) {
  102. return name;
  103. } else {
  104. throw new SAXException(new FOPException("Unknown namespace prefix " + prefix));
  105. }
  106. }
  107. return m_uri + "^" + m_localPart;
  108. }
  109. /** SAX1 Handler for the end of an element */
  110. public void endElement(String rawName) throws SAXException {
  111. mapName(rawName);
  112. super.endElement(m_uri, m_localPart, rawName);
  113. level--;
  114. while (((NSMap) namespaceStack.peek()).level > level) {
  115. namespaceStack.pop();
  116. }
  117. }
  118. /** SAX1 Handler for the start of an element */
  119. public void startElement(String rawName, AttributeList attlist)
  120. throws SAXException {
  121. // SAX2 version of AttributeList
  122. AttributesImpl newAttrs = new AttributesImpl();
  123. level++;
  124. int length = attlist.getLength();
  125. for (int i = 0; i < length; i++) {
  126. String att = attlist.getName(i);
  127. if (att.equals("xmlns")) {
  128. namespaceStack.push( new NSMap("",
  129. attlist.getValue(i),
  130. level));
  131. } else if (att.startsWith("xmlns:")) {
  132. String value = attlist.getValue(i);
  133. namespaceStack.push(new NSMap(att.substring(6), value,
  134. level));
  135. } else {
  136. mapName(att);
  137. newAttrs.addAttribute(m_uri, m_localPart, att,
  138. attlist.getType(i), attlist.getValue(i));
  139. }
  140. }
  141. mapName(rawName);
  142. super.startElement(m_uri, m_localPart, rawName, newAttrs);
  143. }
  144. }