2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 * $Id: TreeWalker.java 468654 2006-10-28 07:09:23Z minchau $
21 package org.apache.xml.serializer;
25 import org.apache.xml.serializer.utils.AttList;
26 import org.apache.xml.serializer.utils.DOM2Helper;
27 import org.w3c.dom.Comment;
28 import org.w3c.dom.Element;
29 import org.w3c.dom.EntityReference;
30 import org.w3c.dom.NamedNodeMap;
31 import org.w3c.dom.Node;
32 import org.w3c.dom.ProcessingInstruction;
33 import org.w3c.dom.Text;
34 import org.xml.sax.ContentHandler;
35 import org.xml.sax.Locator;
36 import org.xml.sax.ext.LexicalHandler;
37 import org.xml.sax.helpers.LocatorImpl;
41 * This class does a pre-order walk of the DOM tree, calling a ContentHandler
42 * interface as it goes.
44 * This class is a copy of the one in org.apache.xml.utils.
45 * It exists to cut the serializers dependancy on that package.
50 public final class TreeWalker
53 /** Local reference to a ContentHandler */
54 final private ContentHandler m_contentHandler;
56 * If m_contentHandler is a SerializationHandler, then this is
57 * a reference to the same object.
59 final private SerializationHandler m_Serializer;
61 // ARGHH!! JAXP Uses Xerces without setting the namespace processing to ON!
62 // DOM2Helper m_dh = new DOM2Helper();
64 /** DomHelper for this TreeWalker */
65 final protected DOM2Helper m_dh;
67 /** Locator object for this TreeWalker */
68 final private LocatorImpl m_locator = new LocatorImpl();
71 * Get the ContentHandler used for the tree walk.
73 * @return the ContentHandler used for the tree walk
75 public ContentHandler getContentHandler()
77 return m_contentHandler;
80 public TreeWalker(ContentHandler ch) {
85 * @param contentHandler The implemention of the
86 * contentHandler operation (toXMLString, digest, ...)
88 public TreeWalker(ContentHandler contentHandler, String systemId)
90 // Set the content handler
91 m_contentHandler = contentHandler;
92 if (m_contentHandler instanceof SerializationHandler) {
93 m_Serializer = (SerializationHandler) m_contentHandler;
98 // Set the system ID, if it is given
99 m_contentHandler.setDocumentLocator(m_locator);
100 if (systemId != null)
101 m_locator.setSystemId(systemId);
104 // Bug see Bugzilla 26741
105 m_locator.setSystemId(System.getProperty("user.dir") + File.separator + "dummy.xsl");
107 catch (SecurityException se) {// user.dir not accessible from applet
111 // Set the document locator
112 if (m_contentHandler != null)
113 m_contentHandler.setDocumentLocator(m_locator);
115 // Bug see Bugzilla 26741
116 m_locator.setSystemId(System.getProperty("user.dir") + File.separator + "dummy.xsl");
118 catch (SecurityException se){// user.dir not accessible from applet
121 m_dh = new DOM2Helper();
125 * Perform a pre-order traversal non-recursive style.
127 * Note that TreeWalker assumes that the subtree is intended to represent
128 * a complete (though not necessarily well-formed) document and, during a
129 * traversal, startDocument and endDocument will always be issued to the
132 * @param pos Node in the tree where to start traversal
134 * @throws TransformerException
136 public void traverse(Node pos) throws org.xml.sax.SAXException
139 this.m_contentHandler.startDocument();
147 Node nextNode = pos.getFirstChild();
149 while (null == nextNode)
156 nextNode = pos.getNextSibling();
158 if (null == nextNode)
160 pos = pos.getParentNode();
162 if ((null == pos) || (top.equals(pos)))
176 this.m_contentHandler.endDocument();
180 * Perform a pre-order traversal non-recursive style.
182 * Note that TreeWalker assumes that the subtree is intended to represent
183 * a complete (though not necessarily well-formed) document and, during a
184 * traversal, startDocument and endDocument will always be issued to the
187 * @param pos Node in the tree where to start traversal
188 * @param top Node in the tree where to end traversal
190 * @throws TransformerException
192 public void traverse(Node pos, Node top) throws org.xml.sax.SAXException
195 this.m_contentHandler.startDocument();
201 Node nextNode = pos.getFirstChild();
203 while (null == nextNode)
207 if ((null != top) && top.equals(pos))
210 nextNode = pos.getNextSibling();
212 if (null == nextNode)
214 pos = pos.getParentNode();
216 if ((null == pos) || ((null != top) && top.equals(pos)))
227 this.m_contentHandler.endDocument();
230 /** Flag indicating whether following text to be processed is raw text */
231 boolean nextIsRaw = false;
234 * Optimized dispatch of characters.
236 private final void dispatachChars(Node node)
237 throws org.xml.sax.SAXException
239 if(m_Serializer != null)
241 this.m_Serializer.characters(node);
245 String data = ((Text) node).getData();
246 this.m_contentHandler.characters(data.toCharArray(), 0, data.length());
251 * Start processing given node
254 * @param node Node to process
256 * @throws org.xml.sax.SAXException
258 protected void startNode(Node node) throws org.xml.sax.SAXException
262 // A Serializer implements ContentHandler, but not NodeConsumer
263 // so drop this reference to NodeConsumer which would otherwise
264 // pull in all sorts of things
265 // if (m_contentHandler instanceof NodeConsumer)
267 // ((NodeConsumer) m_contentHandler).setOriginatingNode(node);
271 if (node instanceof Locator)
273 Locator loc = (Locator)node;
274 m_locator.setColumnNumber(loc.getColumnNumber());
275 m_locator.setLineNumber(loc.getLineNumber());
276 m_locator.setPublicId(loc.getPublicId());
277 m_locator.setSystemId(loc.getSystemId());
281 m_locator.setColumnNumber(0);
282 m_locator.setLineNumber(0);
285 switch (node.getNodeType())
287 case Node.COMMENT_NODE :
289 String data = ((Comment) node).getData();
291 if (m_contentHandler instanceof LexicalHandler)
293 LexicalHandler lh = ((LexicalHandler) this.m_contentHandler);
295 lh.comment(data.toCharArray(), 0, data.length());
299 case Node.DOCUMENT_FRAGMENT_NODE :
303 case Node.DOCUMENT_NODE :
306 case Node.ELEMENT_NODE :
307 Element elem_node = (Element) node;
309 // Make sure the namespace node
310 // for the element itself is declared
311 // to the ContentHandler
312 String uri = elem_node.getNamespaceURI();
314 String prefix = elem_node.getPrefix();
317 this.m_contentHandler.startPrefixMapping(prefix,uri);
320 NamedNodeMap atts = elem_node.getAttributes();
321 int nAttrs = atts.getLength();
322 // System.out.println("TreeWalker#startNode: "+node.getNodeName());
325 // Make sure the namespace node of
326 // each attribute is declared to the ContentHandler
327 for (int i = 0; i < nAttrs; i++)
329 final Node attr = atts.item(i);
330 final String attrName = attr.getNodeName();
331 final int colon = attrName.indexOf(':');
334 // System.out.println("TreeWalker#startNode: attr["+i+"] = "+attrName+", "+attr.getNodeValue());
335 if (attrName.equals("xmlns") || attrName.startsWith("xmlns:"))
337 // Use "" instead of null, as Xerces likes "" for the
338 // name of the default namespace. Fix attributed
339 // to "Steven Murray" <smurray@ebt.com>.
343 prefix = attrName.substring(colon + 1);
345 this.m_contentHandler.startPrefixMapping(prefix,
346 attr.getNodeValue());
348 else if (colon > 0) {
349 prefix = attrName.substring(0,colon);
350 String uri = attr.getNamespaceURI();
352 this.m_contentHandler.startPrefixMapping(prefix,uri);
356 String ns = m_dh.getNamespaceOfNode(node);
359 this.m_contentHandler.startElement(ns,
360 m_dh.getLocalNameOfNode(node),
362 new AttList(atts, m_dh));
364 case Node.PROCESSING_INSTRUCTION_NODE :
366 ProcessingInstruction pi = (ProcessingInstruction) node;
367 String name = pi.getNodeName();
369 // String data = pi.getData();
370 if (name.equals("xslt-next-is-raw"))
376 this.m_contentHandler.processingInstruction(pi.getNodeName(),
381 case Node.CDATA_SECTION_NODE :
383 boolean isLexH = (m_contentHandler instanceof LexicalHandler);
384 LexicalHandler lh = isLexH
385 ? ((LexicalHandler) this.m_contentHandler) : null;
392 dispatachChars(node);
402 case Node.TEXT_NODE :
404 //String data = ((Text) node).getData();
410 m_contentHandler.processingInstruction(javax.xml.transform.Result.PI_DISABLE_OUTPUT_ESCAPING, "");
411 dispatachChars(node);
412 m_contentHandler.processingInstruction(javax.xml.transform.Result.PI_ENABLE_OUTPUT_ESCAPING, "");
416 dispatachChars(node);
420 case Node.ENTITY_REFERENCE_NODE :
422 EntityReference eref = (EntityReference) node;
424 if (m_contentHandler instanceof LexicalHandler)
426 ((LexicalHandler) this.m_contentHandler).startEntity(
432 // warning("Can not output entity to a pure SAX ContentHandler");
441 * End processing of given node
444 * @param node Node we just finished processing
446 * @throws org.xml.sax.SAXException
448 protected void endNode(Node node) throws org.xml.sax.SAXException
451 switch (node.getNodeType())
453 case Node.DOCUMENT_NODE :
456 case Node.ELEMENT_NODE :
457 String ns = m_dh.getNamespaceOfNode(node);
460 this.m_contentHandler.endElement(ns,
461 m_dh.getLocalNameOfNode(node),
464 if (m_Serializer == null) {
465 // Don't bother with endPrefixMapping calls if the ContentHandler is a
466 // SerializationHandler because SerializationHandler's ignore the
467 // endPrefixMapping() calls anyways. . . . This is an optimization.
468 Element elem_node = (Element) node;
469 NamedNodeMap atts = elem_node.getAttributes();
470 int nAttrs = atts.getLength();
472 // do the endPrefixMapping calls in reverse order
473 // of the startPrefixMapping calls
474 for (int i = (nAttrs-1); 0 <= i; i--)
476 final Node attr = atts.item(i);
477 final String attrName = attr.getNodeName();
478 final int colon = attrName.indexOf(':');
481 if (attrName.equals("xmlns") || attrName.startsWith("xmlns:"))
483 // Use "" instead of null, as Xerces likes "" for the
484 // name of the default namespace. Fix attributed
485 // to "Steven Murray" <smurray@ebt.com>.
489 prefix = attrName.substring(colon + 1);
491 this.m_contentHandler.endPrefixMapping(prefix);
493 else if (colon > 0) {
494 prefix = attrName.substring(0, colon);
495 this.m_contentHandler.endPrefixMapping(prefix);
499 String uri = elem_node.getNamespaceURI();
501 String prefix = elem_node.getPrefix();
504 this.m_contentHandler.endPrefixMapping(prefix);
509 case Node.CDATA_SECTION_NODE :
511 case Node.ENTITY_REFERENCE_NODE :
513 EntityReference eref = (EntityReference) node;
515 if (m_contentHandler instanceof LexicalHandler)
517 LexicalHandler lh = ((LexicalHandler) this.m_contentHandler);
519 lh.endEntity(eref.getNodeName());