FileDocCategorySizeDatePackage
DOM2TO.javaAPI DocJava SE 6 API12113Tue Jun 10 00:22:34 BST 2008com.sun.org.apache.xalan.internal.xsltc.trax

DOM2TO

public class DOM2TO extends Object implements Locator2, XMLReader
author
Santiago Pericas-Geertsen
author
Sunitha Reddy

Fields Summary
private static final String
EMPTYSTRING
private static final String
XMLNS_PREFIX
private Node
_dom
A reference to the DOM to be traversed.
private SerializationHandler
_handler
A reference to the output handler receiving the events.
private String
xmlVersion
private String
xmlEncoding
Constructors Summary
public DOM2TO(Node root, SerializationHandler handler)

    
    
         
	_dom = root;
	_handler = handler;
    
Methods Summary
public intgetColumnNumber()
This class is only used internally so this method should never be called.

 
	return 0; 
    
public org.xml.sax.ContentHandlergetContentHandler()

 
	return null;
    
public org.xml.sax.DTDHandlergetDTDHandler()
This class is only used internally so this method should never be called.

 
	return null;
    
public java.lang.StringgetEncoding()

        return xmlEncoding; 
    
public org.xml.sax.EntityResolvergetEntityResolver()
This class is only used internally so this method should never be called.

	return null;
    
public org.xml.sax.ErrorHandlergetErrorHandler()
This class is only used internally so this method should never be called.

	return null;
    
public booleangetFeature(java.lang.String name)
This class is only used internally so this method should never be called.

	return false;
    
public intgetLineNumber()
This class is only used internally so this method should never be called.

 
	return 0; 
    
private java.lang.StringgetNodeTypeFromCode(short code)

	String retval = null;
	switch (code) {
	case Node.ATTRIBUTE_NODE : 
	    retval = "ATTRIBUTE_NODE"; break; 
	case Node.CDATA_SECTION_NODE :
	    retval = "CDATA_SECTION_NODE"; break; 
	case Node.COMMENT_NODE :
	    retval = "COMMENT_NODE"; break; 
	case Node.DOCUMENT_FRAGMENT_NODE :
	    retval = "DOCUMENT_FRAGMENT_NODE"; break; 
	case Node.DOCUMENT_NODE :
	    retval = "DOCUMENT_NODE"; break; 
	case Node.DOCUMENT_TYPE_NODE :
	    retval = "DOCUMENT_TYPE_NODE"; break; 
	case Node.ELEMENT_NODE :
	    retval = "ELEMENT_NODE"; break; 
	case Node.ENTITY_NODE :
	    retval = "ENTITY_NODE"; break; 
	case Node.ENTITY_REFERENCE_NODE :
	    retval = "ENTITY_REFERENCE_NODE"; break; 
	case Node.NOTATION_NODE :
	    retval = "NOTATION_NODE"; break; 
	case Node.PROCESSING_INSTRUCTION_NODE :
	    retval = "PROCESSING_INSTRUCTION_NODE"; break; 
	case Node.TEXT_NODE:
	    retval = "TEXT_NODE"; break; 
        }
	return retval;
    
public java.lang.ObjectgetProperty(java.lang.String name)
This class is only used internally so this method should never be called.

	return null;
    
public java.lang.StringgetPublicId()
This class is only used internally so this method should never be called.

 
	return null; 
    
public java.lang.StringgetSystemId()
This class is only used internally so this method should never be called.

 
	return null; 
    
public java.lang.StringgetXMLVersion()

        return xmlVersion;
    
public voidparse(java.lang.String sysId)
This class is only used internally so this method should never be called.

	throw new IOException("This method is not yet implemented.");
    
public voidparse(org.xml.sax.InputSource unused)

        parse(_dom);
    
public voidparse()

	
        if (_dom != null) {
	    boolean isIncomplete = 
		(_dom.getNodeType() != org.w3c.dom.Node.DOCUMENT_NODE);
              
	    if (isIncomplete) {
		_handler.startDocument();
		parse(_dom);
		_handler.endDocument();
	    }
	    else {
		parse(_dom);
	    }
	}
    
private voidparse(org.w3c.dom.Node node)
Traverse the DOM and generate TO events for a handler. Notice that we need to handle implicit namespace declarations too.

 	if (node == null) return;

        switch (node.getNodeType()) {
	case Node.ATTRIBUTE_NODE:         // handled by ELEMENT_NODE
	case Node.DOCUMENT_TYPE_NODE :
	case Node.ENTITY_NODE :
	case Node.ENTITY_REFERENCE_NODE:
	case Node.NOTATION_NODE :
	    // These node types are ignored!!!
	    break;
	case Node.CDATA_SECTION_NODE:
	    _handler.startCDATA();
	    _handler.characters(node.getNodeValue());
	    _handler.endCDATA();
	    break;

	case Node.COMMENT_NODE:           // should be handled!!!
	    _handler.comment(node.getNodeValue());
	    break;

	case Node.DOCUMENT_NODE:
             setDocumentInfo((Document)node);
             _handler.setDocumentLocator(this); 
             _handler.startDocument();
	    Node next = node.getFirstChild();
	    while (next != null) {
		parse(next);
		next = next.getNextSibling();
	    }
	    _handler.endDocument();
	    break;

	case Node.DOCUMENT_FRAGMENT_NODE:
	    next = node.getFirstChild();
	    while (next != null) {
		parse(next);
		next = next.getNextSibling();
	    }
	    break;

	case Node.ELEMENT_NODE:
	    // Generate SAX event to start element
	    final String qname = node.getNodeName();
	    _handler.startElement(null, null, qname);

            int colon;
	    String prefix;
	    final NamedNodeMap map = node.getAttributes();
	    final int length = map.getLength();

	    // Process all namespace attributes first
	    for (int i = 0; i < length; i++) {
		final Node attr = map.item(i);
		final String qnameAttr = attr.getNodeName();

                // Is this a namespace declaration?
		if (qnameAttr.startsWith(XMLNS_PREFIX)) {
		    final String uriAttr = attr.getNodeValue();
		    colon = qnameAttr.lastIndexOf(':");
		    prefix = (colon > 0) ? qnameAttr.substring(colon + 1) 
			                 : EMPTYSTRING;
		    _handler.namespaceAfterStartElement(prefix, uriAttr);
		}
	    }
            
	    // Process all non-namespace attributes next
            NamespaceMappings nm = new NamespaceMappings();
	    for (int i = 0; i < length; i++) {
		final Node attr = map.item(i);
		final String qnameAttr = attr.getNodeName();

                // Is this a regular attribute?
		if (!qnameAttr.startsWith(XMLNS_PREFIX)) {
		    final String uriAttr = attr.getNamespaceURI();
		    // Uri may be implicitly declared
		    if (uriAttr != null && !uriAttr.equals(EMPTYSTRING) ) {	
			colon = qnameAttr.lastIndexOf(':");
                           
                        // Fix for bug 26319
                        // For attributes not given an prefix explictly
                        // but having a namespace uri we need
                        // to explicitly generate the prefix
                        String newPrefix = nm.lookupPrefix(uriAttr);
                        if (newPrefix == null) 
                            newPrefix = nm.generateNextPrefix();
			prefix = (colon > 0) ? qnameAttr.substring(0, colon) 
			    : newPrefix;
			_handler.namespaceAfterStartElement(prefix, uriAttr);
		        _handler.addAttribute((prefix + ":" + qnameAttr),
                            attr.getNodeValue());
		    } else {
                         _handler.addAttribute(qnameAttr, attr.getNodeValue());
                    }
                }
	    }

	    // Now element namespace and children
	    final String uri = node.getNamespaceURI();
            final String localName = node.getLocalName();

	    // Uri may be implicitly declared
	    if (uri != null) {	
		colon = qname.lastIndexOf(':");
		prefix = (colon > 0) ? qname.substring(0, colon) : EMPTYSTRING;
		_handler.namespaceAfterStartElement(prefix, uri);
	    }else {
                  // Fix for bug 26319
                  // If an element foo is created using
                  // createElementNS(null,locName)
                  // then the  element should be serialized
                  // <foo xmlns=" "/> 
                  if (uri == null  && localName != null) {
 		     prefix = EMPTYSTRING;
		     _handler.namespaceAfterStartElement(prefix, EMPTYSTRING);
                 }
            }

	    // Traverse all child nodes of the element (if any)
	    next = node.getFirstChild();
	    while (next != null) {
		parse(next);
		next = next.getNextSibling();
	    }

	    // Generate SAX event to close element
	    _handler.endElement(qname);
	    break;

	case Node.PROCESSING_INSTRUCTION_NODE:
	    _handler.processingInstruction(node.getNodeName(),
					   node.getNodeValue());
	    break;

	case Node.TEXT_NODE:
	    _handler.characters(node.getNodeValue());
	    break;
	}
    
public voidsetContentHandler(org.xml.sax.ContentHandler handler)

	// Empty
    
public voidsetDTDHandler(org.xml.sax.DTDHandler handler)
This class is only used internally so this method should never be called.

    
private voidsetDocumentInfo(org.w3c.dom.Document document)

        if (!document.getXmlStandalone())
            _handler.setStandalone(Boolean.toString(document.getXmlStandalone()));
        setXMLVersion(document.getXmlVersion());
        setEncoding(document.getXmlEncoding()); 
    
private voidsetEncoding(java.lang.String encoding)

        if (encoding != null) {
            xmlEncoding = encoding;
            _handler.setEncoding(encoding);
        }
    
public voidsetEntityResolver(org.xml.sax.EntityResolver resolver)
This class is only used internally so this method should never be called.

    
public voidsetErrorHandler(org.xml.sax.ErrorHandler handler)
This class is only used internally so this method should never be called.

    
public voidsetFeature(java.lang.String name, boolean value)
This class is only used internally so this method should never be called.

    
public voidsetProperty(java.lang.String name, java.lang.Object value)
This class is only used internally so this method should never be called.

    
private voidsetXMLVersion(java.lang.String version)

        if (version != null) {
            xmlVersion = version;
            _handler.setVersion(xmlVersion);
        }