org.apache.cocoon.xml.dom
Class DOMStreamer.NamespaceNormalizingDOMStreamer

java.lang.Object
  extended by org.apache.avalon.framework.logger.AbstractLogEnabled
      extended by org.apache.cocoon.xml.AbstractXMLProducer
          extended by org.apache.cocoon.xml.dom.DOMStreamer.NamespaceNormalizingDOMStreamer
All Implemented Interfaces:
Poolable, Recyclable, LogEnabled, XMLProducer
Enclosing class:
DOMStreamer

public static class DOMStreamer.NamespaceNormalizingDOMStreamer
extends AbstractXMLProducer

Streams a DOM tree to SAX events and normalizes namespace declarations on the way.

The code in this class is based on the org.apache.xml.utils.TreeWalker class from Xalan, though it differs in some important ways.

This class will automatically fix up ("normalize") namespace declarations while streaming to SAX. The original DOM-tree is not modified. The algorithm used is described in an appendix of the DOM Level 3 spec.

This class will NOT check the correctness of namespaces, e.g. it will not check that the "xml" prefix is not misused etc.

Author:
Bruno Dumon (bruno at outerthought dot org), Xalan team

Nested Class Summary
static class DOMStreamer.NamespaceNormalizingDOMStreamer.ElementInfo
           
 
Field Summary
protected  DOMStreamer.NamespaceNormalizingDOMStreamer.ElementInfo currentElementInfo
          Information about the current element.
protected  int newPrefixCounter
          Counter used when generating new namespace prefixes.
 
Fields inherited from class org.apache.cocoon.xml.AbstractXMLProducer
contentHandler, EMPTY_CONTENT_HANDLER, lexicalHandler, xmlConsumer
 
Constructor Summary
DOMStreamer.NamespaceNormalizingDOMStreamer()
           
 
Method Summary
protected  void endNode(Node node)
          End processing of given node
 String getNamespaceForPrefix(String prefix, Element namespaceContext)
          Searches the namespace for a given namespace prefix starting from a given Element.
 void recycle()
          Recycle the producer by removing references, and resetting handlers to null (empty) implementations.
protected  void startNode(Node node)
          Start processing given node
protected  void stream(Node pos)
          Start the production of SAX events.
 
Methods inherited from class org.apache.cocoon.xml.AbstractXMLProducer
setConsumer, setContentHandler, setLexicalHandler
 
Methods inherited from class org.apache.avalon.framework.logger.AbstractLogEnabled
enableLogging, getLogger, setupLogger, setupLogger, setupLogger
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

currentElementInfo

protected DOMStreamer.NamespaceNormalizingDOMStreamer.ElementInfo currentElementInfo
Information about the current element. Used to remember the localName, qName and namespaceURI for generating the endElement event, and holds the namespaces declared on the element. This extra class is needed because we don't want to modify the DOM-tree itself. The currentElementInfo has a pointer to its parent elementInfo.


newPrefixCounter

protected int newPrefixCounter
Counter used when generating new namespace prefixes.

Constructor Detail

DOMStreamer.NamespaceNormalizingDOMStreamer

public DOMStreamer.NamespaceNormalizingDOMStreamer()
Method Detail

recycle

public void recycle()
Description copied from class: AbstractXMLProducer
Recycle the producer by removing references, and resetting handlers to null (empty) implementations.

Specified by:
recycle in interface Recyclable
Overrides:
recycle in class AbstractXMLProducer

stream

protected void stream(Node pos)
               throws SAXException
Start the production of SAX events.

Perform a pre-order traversal non-recursive style.

Note that TreeWalker assumes that the subtree is intended to represent a complete (though not necessarily well-formed) document and, during a traversal, startDocument and endDocument will always be issued to the SAX listener.

Parameters:
pos - Node in the tree where to start traversal
Throws:
SAXException

startNode

protected void startNode(Node node)
                  throws SAXException
Start processing given node

Parameters:
node - Node to process
Throws:
SAXException

getNamespaceForPrefix

public String getNamespaceForPrefix(String prefix,
                                    Element namespaceContext)
Searches the namespace for a given namespace prefix starting from a given Element.

Note that this resolves the prefix in the orginal DOM-tree, not in the DOMStreamer.NamespaceNormalizingDOMStreamer.ElementInfo objects. This is used to resolve prefixes of elements or attributes created with createElement or setAttribute instead of createElementNS or setAttributeNS.

The code in this method is largely based on org.apache.xml.utils.DOMHelper.getNamespaceForPrefix() (from Xalan).

Parameters:
prefix - the prefix to look for, can be empty or null to find the default namespace
Returns:
the namespace, or null if not found.

endNode

protected void endNode(Node node)
                throws SAXException
End processing of given node

Parameters:
node - Node we just finished processing
Throws:
SAXException


Copyright © 1999-2010 The Apache Software Foundation. All Rights Reserved.