FileDocCategorySizeDatePackage
DOMUtil.javaAPI DocGlassfish v2 API18615Fri May 04 22:30:28 BST 2007com.sun.enterprise.jbi.serviceengine.util

DOMUtil

public class DOMUtil extends Object
This object provides utility methods to manipulate DOM tree.
author
bhavanishankar@dev.java.net.

Fields Summary
public static final DOMUtil
UTIL
static object to access the methods of this object.
Constructors Summary
public DOMUtil()
Creates a new instance of DOMUtil.

    
               
      
    
Methods Summary
public java.lang.StringDOM2String(org.w3c.dom.Document aDocument, java.io.Writer aWriter)
save document to stream.

param
aDocument Document
param
aWriter is what the aDocument is serialized to.
return
String representation of Document
throws
Exception If fails to construct a string.

        TransformerFactory transformerFactory =
                TransformerFactory.newInstance();
        Transformer transformer;
        
        try {
            transformer = transformerFactory.newTransformer();
        } catch (javax.xml.transform.TransformerConfigurationException e) {
            e.printStackTrace();
            transformer = null;
            throw e;
        }
        
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
        transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "");
        transformer.setOutputProperty(OutputKeys.INDENT, "no");
        
        try {
            transformer.transform(new DOMSource(aDocument),
                    new StreamResult(aWriter));
        } catch (javax.xml.transform.TransformerException e) {
            e.printStackTrace();
            throw e;
        }
        
        return aWriter.toString();
    
private java.lang.StringfixNull(java.lang.String s)

        return s == null ? "" : s;
    
public org.w3c.dom.NodeListgetChildElements(org.w3c.dom.Element aElement, java.lang.String aElementTagName)
get the children of the same type element tag name.

param
aElement Element for parent node
param
aElementTagName String for tagname
return
NodeList for list of children with the tagname

        NodeList nodeList = aElement.getChildNodes();
        NodeListImpl list = new NodeListImpl();
        int count = nodeList.getLength();
        
        for (int i = 0; i < count; ++i) {
            Node node = nodeList.item(i);
            
            if (node instanceof Element) {
                String tagName = getElementName((Element) node);
                
                if (tagName.equals(aElementTagName)) {
                    list.add(node);
                }
            }
        }
        
        return list;
    
public org.w3c.dom.ElementgetElement(org.w3c.dom.Document aParentDocument, java.lang.String aTagName)
gets the element.

param
aParentDocument Document for parent node
param
aTagName String for tagname
return
Element with tagname

        NodeList nodeList = aParentDocument.getElementsByTagName(aTagName);
        
        return (nodeList != null) ? (Element) nodeList.item(0) : null;
    
public org.w3c.dom.ElementgetElement(org.w3c.dom.Element elem, java.lang.String tagName)
gets the element.

param
elem Element for parent node
param
tagName String for tagname
return
Element with tagname

        NodeList nl = elem.getElementsByTagName(tagName);
        Element childElem = (Element) nl.item(0);
        
        return childElem;
    
public java.lang.StringgetElementName(org.w3c.dom.Element aElement)
get Element Tag Name with striped prefix.

param
aElement Element object
return
String with stripped prefix

        String tagName = aElement.getTagName();
        
        return getName(tagName);
    
public java.lang.StringgetElementValue(org.w3c.dom.Document doc, java.lang.String elemName)
gets the element value.

param
doc Document for parent node
param
elemName String for element name
return
Element value

        String elemVal = null;
        
        Element elem = getElement(doc, elemName);
        elemVal = getTextData(elem);
        
        return elemVal;
    
public org.w3c.dom.NodeListgetElements(org.w3c.dom.Element aParentElement, java.lang.String aTagName)
gets list of elements.

param
aParentElement Element for parent
param
aTagName String for tagname
return
NodeList with tagname

        return aParentElement.getElementsByTagNameNS(
                aParentElement.getNamespaceURI(),
                aTagName
                );
    
public org.w3c.dom.NodeListgetElements(org.w3c.dom.Document aParentDocument, java.lang.String aTagName)
gets set of elements.

param
aParentDocument Document for parent node
param
aTagName String for tagname
return
NodeList with tagname

        return aParentDocument.getElementsByTagNameNS("*", aTagName);
    
public java.lang.StringgetName(java.lang.String aName)
strips the prefix of the name if present.

param
aName String value of Name with prefix
return
String for name after striping prefix

        int lastIdx = aName.lastIndexOf(':");
        
        if (lastIdx >= 0) {
            return aName.substring(lastIdx + 1);
        }
        
        return aName;
    
private org.w3c.dom.TextgetText(org.w3c.dom.Element aElement)
use this util method to create/retrieve a Text Node in a element.

param
aElement Element node
return
Text node for text data

        Node node = null;
        aElement.normalize();
        node = aElement.getFirstChild();
        
        if ((node == null) || !(node instanceof Text)) {
            node = aElement.getOwnerDocument().createTextNode("");
            aElement.appendChild(node);
        }
        
        return (Text) node;
    
public java.lang.StringgetTextData(org.w3c.dom.Element aElement)
use this util method to retrieve a Text Data in a element.

param
aElement Element for text node
return
String contains text

        return getText(aElement).getData();
    
private voidresolvePrefix(org.w3c.dom.Element e, java.util.Map resolvedPrefixes)
Inherit the namespace attributes from e's parents and set it in e, if the prefix is not already resolved.

        String nodePrefix = fixNull(e.getPrefix());
        String nodeNamespace = e.getNamespaceURI();
        if(nodeNamespace != null) {
            /**
             * Set the namespace attribute if it is not already set
             */
            if(!resolvedPrefixes.containsKey(nodePrefix)) {
                String attrName = (nodePrefix.trim().length() == 0)
                ? "xmlns" : "xmlns:" + nodePrefix;
                e.setAttribute(attrName, nodeNamespace);
                resolvedPrefixes.put(nodePrefix, nodeNamespace);
            }
        }
    
public voidsetTextData(org.w3c.dom.Element aElement, java.lang.String aData)
use this util method to set a Text Data in a element.

param
aElement Element for text node
param
aData String contains text

        getText(aElement).setData(aData);
    
private voidwriteAncestorNamespaceAttrs(javax.xml.stream.XMLStreamReader reader, javax.xml.stream.XMLStreamWriter writer, java.util.Map ancestorNamespaceAttrs)

        try {
            String prefix = reader.getPrefix();
            String namespace = ancestorNamespaceAttrs.remove(prefix);
            if(namespace != null) {
                writer.writeNamespace(prefix, namespace);
            }
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    
voidwriteAttributes(javax.xml.stream.XMLStreamReader reader, javax.xml.stream.XMLStreamWriter writer)
Writes namespace attributes and other attributes from the current node of the reader to the current node of the writer.

param
reader XMLStreamReader pointing to a START_ELEMENT.
param
writer XMLStreamWriter pointing to a START_ELEMENT.

        int namespaceCount = reader.getNamespaceCount();
        int attributeCount = reader.getAttributeCount();
        for(int i=0; i<namespaceCount; i++) {
            writer.writeNamespace(reader.getNamespacePrefix(i),
                    reader.getNamespaceURI(i));
        }
        for(int i=0; i<attributeCount; i++) {
            String attrNs = reader.getAttributeNamespace(i);
            if(attrNs != null) {
                writer.writeAttribute(attrNs,
                        reader.getAttributeLocalName(i),
                        reader.getAttributeValue(i));
                
            } else {
                writer.writeAttribute(reader.getAttributeLocalName(i),
                        reader.getAttributeValue(i));
            }
        }
    
public voidwriteChildren(javax.xml.stream.XMLStreamWriter writer, org.w3c.dom.Node node)

        NodeList l = node.getChildNodes();
        int size = l.getLength();
        for ( int i = 0; i < size; i++ ) {
            writeNode(l.item(i), writer, new HashMap<String,String>());
        }
    
private voidwriteChildren(javax.xml.stream.XMLStreamWriter writer, org.w3c.dom.Node node, java.util.Map resolvedPrefixes)
This method should be used only from writeNode(node,writer,resolvedPrefixes) method.

        NodeList l = node.getChildNodes();
        int size = l.getLength();
        for ( int i = 0; i < size; i++ ) {
            writeNode(l.item(i), writer, resolvedPrefixes);
        }
    
public voidwriteChildren(javax.xml.stream.XMLStreamReader reader, javax.xml.stream.XMLStreamWriter writer)

        writeChildren(reader, writer, new HashMap<String,String>());
    
public voidwriteChildren(javax.xml.stream.XMLStreamReader reader, javax.xml.stream.XMLStreamWriter writer, java.util.Map ancestorNamespaceAttrs)

        reader.next();
        while(reader.getEventType() != XMLStreamReader.END_ELEMENT) {
            writeNode(reader, writer, ancestorNamespaceAttrs);
        }
        reader.next();
    
public voidwriteNode(org.w3c.dom.Node node, javax.xml.stream.XMLStreamWriter writer)
Writes the node to the XMLStreamWriter as text.

param
node Node from where the contents should be read.
param
writer XMLStreamWriter to where the contents should be written.

        writeNode(node, writer, new HashMap<String,String>());
    
public voidwriteNode(org.w3c.dom.Node node, javax.xml.stream.XMLStreamWriter writer, java.util.Map resolvedPrefixes)
namespaceMap keeps track of the namespace attributes set in the the ancestor node.

        
        short type = node.getNodeType();
        switch ( type ) {
            
            case Node.DOCUMENT_NODE: {
                writer.writeStartDocument();
                writeChildren(writer, node, resolvedPrefixes);
                break;
            }
            
            case Node.DOCUMENT_FRAGMENT_NODE: {
                writeChildren(writer, node, resolvedPrefixes);
                break;
            }
            
            case Node.ELEMENT_NODE: {
                Element e = (Element)node;
                String n = e.getTagName();
                
                resolvePrefix(e, resolvedPrefixes);
                
                writer.writeStartElement(n);
                
                NamedNodeMap a = e.getAttributes();
                int size = a.getLength();
                for ( int i = 0; i < size; i++ ) {
                    Attr att = (Attr)a.item(i);
                    writeNode(att, writer, resolvedPrefixes);
                }
                
                if ( e.hasChildNodes() ) {
                    writeChildren(writer, node, resolvedPrefixes);
                    writer.writeEndElement();
                } else
                    writer.writeEndElement();
                break;
            }
            
            case Node.ATTRIBUTE_NODE:
                Attr a = (Attr)node;
                writer.writeAttribute(a.getName(), a.getValue());
                break;
                
            case Node.PROCESSING_INSTRUCTION_NODE: {
                ProcessingInstruction pi = (ProcessingInstruction)node;
                writer.writeProcessingInstruction(pi.getTarget(), pi.getData());
                break;
            }
            
            case Node.TEXT_NODE: {
                writer.writeCharacters(node.getNodeValue());
                break;
            }
            
            case Node.CDATA_SECTION_NODE: {
                writer.writeCData(node.getNodeValue());
                break;
            }
            
            case Node.COMMENT_NODE: {
                writer.writeComment(node.getNodeValue());
                break;
            }
        }
    
public voidwriteNode(javax.xml.stream.XMLStreamReader reader, javax.xml.stream.XMLStreamWriter writer)
writes the node pointed by XMLStreamReader to the XMLStreamWriter Strictly speaking, this method should not be in this class.

param
reader XMLStreamReader from where the contents should be read.
param
writer XMLStreamWriter to where the contents should be written.

        writeNode(reader, writer, new HashMap<String,String>());
    
public voidwriteNode(javax.xml.stream.XMLStreamReader reader, javax.xml.stream.XMLStreamWriter writer, java.util.Map ancestorNamespaceAttrs)

        int depth = 0;
        int event = reader.getEventType();
        do {
            switch(event) {
                case XMLStreamReader.START_DOCUMENT:
                    reader.next();
                    writeNode(reader, writer);
                    break;
                case XMLStreamReader.START_ELEMENT:
                    String tagName =
                            (reader.getPrefix() != null && reader.getPrefix().length() != 0)
                            ? reader.getPrefix() + ":" + reader.getLocalName()
                            : reader.getLocalName();
                    writer.writeStartElement(tagName);
                    writeAttributes(reader, writer);
                    writeAncestorNamespaceAttrs(reader, writer, ancestorNamespaceAttrs);
                    ++depth;
                    break;
                case XMLStreamReader.CHARACTERS:
                    writer.writeCharacters(reader.getText());
                    break;
                case XMLStreamReader.END_ELEMENT:
                    writer.writeEndElement();
                    --depth;
            }
            event = reader.next();
        } while(depth > 0);