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.

DOM2SAX.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.util;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.Stack;
  22. import org.w3c.dom.Document;
  23. import org.w3c.dom.NamedNodeMap;
  24. import org.w3c.dom.Node;
  25. import org.xml.sax.ContentHandler;
  26. import org.xml.sax.SAXException;
  27. import org.xml.sax.ext.LexicalHandler;
  28. import org.xml.sax.helpers.AttributesImpl;
  29. /**
  30. * Helper class that produces a SAX stream from a DOM Document.
  31. * <p>
  32. * Part of the code here copied and adapted from Apache Xalan-J,
  33. * src/org/apache/xalan/xsltc/trax/DOM2SAX.java
  34. */
  35. public class DOM2SAX {
  36. private static final String EMPTYSTRING = "";
  37. private static final String XMLNS_PREFIX = "xmlns";
  38. private ContentHandler contentHandler;
  39. private LexicalHandler lexicalHandler;
  40. private Map prefixes = new java.util.HashMap();
  41. /**
  42. * Main constructor
  43. * @param handler the ContentHandler to send SAX events to
  44. */
  45. public DOM2SAX(ContentHandler handler) {
  46. this.contentHandler = handler;
  47. if (handler instanceof LexicalHandler) {
  48. this.lexicalHandler = (LexicalHandler)handler;
  49. }
  50. }
  51. /**
  52. * Writes the given document using the given ContentHandler.
  53. * @param doc DOM document
  54. * @param fragment if false no startDocument() and endDocument() calls are issued.
  55. * @throws SAXException In case of a problem while writing XML
  56. */
  57. public void writeDocument(Document doc, boolean fragment) throws SAXException {
  58. if (!fragment) {
  59. contentHandler.startDocument();
  60. }
  61. for (Node n = doc.getFirstChild(); n != null;
  62. n = n.getNextSibling()) {
  63. writeNode(n);
  64. }
  65. if (!fragment) {
  66. contentHandler.endDocument();
  67. }
  68. }
  69. /**
  70. * Begin the scope of namespace prefix. Forward the event to the SAX handler
  71. * only if the prefix is unknown or it is mapped to a different URI.
  72. */
  73. private boolean startPrefixMapping(String prefix, String uri)
  74. throws SAXException {
  75. boolean pushed = true;
  76. Stack uriStack = (Stack)prefixes.get(prefix);
  77. if (uriStack != null) {
  78. if (uriStack.isEmpty()) {
  79. contentHandler.startPrefixMapping(prefix, uri);
  80. uriStack.push(uri);
  81. } else {
  82. final String lastUri = (String) uriStack.peek();
  83. if (!lastUri.equals(uri)) {
  84. contentHandler.startPrefixMapping(prefix, uri);
  85. uriStack.push(uri);
  86. } else {
  87. pushed = false;
  88. }
  89. }
  90. } else {
  91. contentHandler.startPrefixMapping(prefix, uri);
  92. uriStack = new Stack();
  93. prefixes.put(prefix, uriStack);
  94. uriStack.push(uri);
  95. }
  96. return pushed;
  97. }
  98. /*
  99. * End the scope of a name prefix by popping it from the stack and passing
  100. * the event to the SAX Handler.
  101. */
  102. private void endPrefixMapping(String prefix) throws SAXException {
  103. final Stack uriStack = (Stack)prefixes.get(prefix);
  104. if (uriStack != null) {
  105. contentHandler.endPrefixMapping(prefix);
  106. uriStack.pop();
  107. }
  108. }
  109. /**
  110. * If the DOM was created using a DOM 1.0 API, the local name may be null.
  111. * If so, get the local name from the qualified name before generating the
  112. * SAX event.
  113. */
  114. private static String getLocalName(Node node) {
  115. final String localName = node.getLocalName();
  116. if (localName == null) {
  117. final String qname = node.getNodeName();
  118. final int col = qname.lastIndexOf(':');
  119. return (col > 0) ? qname.substring(col + 1) : qname;
  120. }
  121. return localName;
  122. }
  123. /**
  124. * Writes a node using the given writer.
  125. * @param node node to serialize
  126. * @throws SAXException In case of a problem while writing XML
  127. */
  128. private void writeNode(Node node)
  129. throws SAXException {
  130. if (node == null) {
  131. return;
  132. }
  133. switch (node.getNodeType()) {
  134. case Node.ATTRIBUTE_NODE: // handled by ELEMENT_NODE
  135. case Node.DOCUMENT_FRAGMENT_NODE:
  136. case Node.DOCUMENT_TYPE_NODE:
  137. case Node.ENTITY_NODE:
  138. case Node.ENTITY_REFERENCE_NODE:
  139. case Node.NOTATION_NODE:
  140. // These node types are ignored!!!
  141. break;
  142. case Node.CDATA_SECTION_NODE:
  143. final String cdata = node.getNodeValue();
  144. if (lexicalHandler != null) {
  145. lexicalHandler.startCDATA();
  146. contentHandler.characters(cdata.toCharArray(), 0, cdata.length());
  147. lexicalHandler.endCDATA();
  148. } else {
  149. // in the case where there is no lex handler, we still
  150. // want the text of the cdate to make its way through.
  151. contentHandler.characters(cdata.toCharArray(), 0, cdata.length());
  152. }
  153. break;
  154. case Node.COMMENT_NODE: // should be handled!!!
  155. if (lexicalHandler != null) {
  156. final String value = node.getNodeValue();
  157. lexicalHandler.comment(value.toCharArray(), 0, value.length());
  158. }
  159. break;
  160. case Node.DOCUMENT_NODE:
  161. contentHandler.startDocument();
  162. Node next = node.getFirstChild();
  163. while (next != null) {
  164. writeNode(next);
  165. next = next.getNextSibling();
  166. }
  167. contentHandler.endDocument();
  168. break;
  169. case Node.ELEMENT_NODE:
  170. String prefix;
  171. List pushedPrefixes = new java.util.ArrayList();
  172. final AttributesImpl attrs = new AttributesImpl();
  173. final NamedNodeMap map = node.getAttributes();
  174. final int length = map.getLength();
  175. // Process all namespace declarations
  176. for (int i = 0; i < length; i++) {
  177. final Node attr = map.item(i);
  178. final String qnameAttr = attr.getNodeName();
  179. // Ignore everything but NS declarations here
  180. if (qnameAttr.startsWith(XMLNS_PREFIX)) {
  181. final String uriAttr = attr.getNodeValue();
  182. final int colon = qnameAttr.lastIndexOf(':');
  183. prefix = (colon > 0) ? qnameAttr.substring(colon + 1)
  184. : EMPTYSTRING;
  185. if (startPrefixMapping(prefix, uriAttr)) {
  186. pushedPrefixes.add(prefix);
  187. }
  188. }
  189. }
  190. // Process all other attributes
  191. for (int i = 0; i < length; i++) {
  192. final Node attr = map.item(i);
  193. final String qnameAttr = attr.getNodeName();
  194. // Ignore NS declarations here
  195. if (!qnameAttr.startsWith(XMLNS_PREFIX)) {
  196. final String uriAttr = attr.getNamespaceURI();
  197. // Uri may be implicitly declared
  198. if (uriAttr != null) {
  199. final int colon = qnameAttr.lastIndexOf(':');
  200. prefix = (colon > 0) ? qnameAttr.substring(0, colon)
  201. : EMPTYSTRING;
  202. if (startPrefixMapping(prefix, uriAttr)) {
  203. pushedPrefixes.add(prefix);
  204. }
  205. }
  206. // Add attribute to list
  207. attrs.addAttribute(attr.getNamespaceURI(),
  208. getLocalName(attr), qnameAttr, "CDATA", attr
  209. .getNodeValue());
  210. }
  211. }
  212. // Now process the element itself
  213. final String qname = node.getNodeName();
  214. final String uri = node.getNamespaceURI();
  215. final String localName = getLocalName(node);
  216. // Uri may be implicitly declared
  217. if (uri != null) {
  218. final int colon = qname.lastIndexOf(':');
  219. prefix = (colon > 0) ? qname.substring(0, colon) : EMPTYSTRING;
  220. if (startPrefixMapping(prefix, uri)) {
  221. pushedPrefixes.add(prefix);
  222. }
  223. }
  224. // Generate SAX event to start element
  225. contentHandler.startElement(uri, localName, qname, attrs);
  226. // Traverse all child nodes of the element (if any)
  227. next = node.getFirstChild();
  228. while (next != null) {
  229. writeNode(next);
  230. next = next.getNextSibling();
  231. }
  232. // Generate SAX event to close element
  233. contentHandler.endElement(uri, localName, qname);
  234. // Generate endPrefixMapping() for all pushed prefixes
  235. final int nPushedPrefixes = pushedPrefixes.size();
  236. for (int i = 0; i < nPushedPrefixes; i++) {
  237. endPrefixMapping((String)pushedPrefixes.get(i));
  238. }
  239. break;
  240. case Node.PROCESSING_INSTRUCTION_NODE:
  241. contentHandler.processingInstruction(node.getNodeName(), node.getNodeValue());
  242. break;
  243. case Node.TEXT_NODE:
  244. final String data = node.getNodeValue();
  245. contentHandler.characters(data.toCharArray(), 0, data.length());
  246. break;
  247. default:
  248. //nop
  249. }
  250. }
  251. }