Package XMLDOM2

Source Code of XMLDOM2.Document

/*
Copyright (c) 2003-2009 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package XMLDOM2;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.w3c.dom.Attr;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Comment;
import org.w3c.dom.DOMConfiguration;
import org.w3c.dom.DOMException;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
import org.w3c.dom.EntityReference;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.ProcessingInstruction;
import org.w3c.dom.Text;
import org.w3c.dom.UserDataHandler;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;

import Framework.ErrorMgr;
import Framework.File;
import Framework.MemoryStream;
import Framework.TextData;
import Framework.UsageException;

/**
* The Document class represents the entire XML document. Conceptually, it is the root of the document tree, and provides the primary access to the document's data.
*
*/
public class Document implements org.w3c.dom.Document {

    //private File xmlfile = null;
    private org.w3c.dom.Document docRoot = null;

    // TF:23/06/2008:We need to remember this document, that created the document root
    // so it can be returned when asked for the owner document
    private static final String THIS_DOCUMENT_KEY = "qqThisDocumentKey";
   
    public Document() {
        docRoot = DomFactory.newDocument();
        // TF:23/06/2008:Remember "this" document so we can return it as the real owner.
        docRoot.setUserData(THIS_DOCUMENT_KEY, this, null);
    }

    public Document(InputStream is) {
        docRoot = DomFactory.newDocument(is);
        // TF:23/06/2008:Remember "this" document so we can return it as the real owner.
        docRoot.setUserData(THIS_DOCUMENT_KEY, this, null);
    }

//  public void setFile(File xmlfile) {
//  this.xmlfile = xmlfile;
//  }

    public Node adoptNode(Node source) throws DOMException {     
        try {     
            return docRoot.adoptNode(source);     
        } catch (DOMException e) {     
            XMLDOM2.DOMException errorVar = new XMLDOM2.DOMException("Cannot adoptNode", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }     
    }     

    public Attr createAttribute(TextData name) {
        return this.createAttribute(name.toString());
    }

    public Attr createAttribute(String name) {
        try {
            return docRoot.createAttribute(name);
        } catch (DOMException e) {
            XMLDOM2.DOMException errorVar = new XMLDOM2.DOMException("Cannot create Attribute", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }
    }

    public Attr createAttributeNS(String namespaceURI, String qualifiedName) {
        try {
            return docRoot.createAttributeNS(namespaceURI, qualifiedName);
        } catch (DOMException e) {
            XMLDOM2.DOMException errorVar = new XMLDOM2.DOMException("Cannot create Attribute", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }
    }

    public CDATASection createCDATASection(String data) {
        try {
            return docRoot.createCDATASection(data);
        } catch (DOMException e) {
            XMLDOM2.DOMException errorVar = new XMLDOM2.DOMException("Cannot create CDATA", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }
    }

    public Comment createComment(String data) {
        return docRoot.createComment(data);
    }

    public DocumentFragment createDocumentFragment() {
        return docRoot.createDocumentFragment();
    }

    public Element createElement(String tagName)  {
        try {
            return docRoot.createElement(tagName);
        } catch (DOMException e) {
            XMLDOM2.DOMException errorVar = new XMLDOM2.DOMException("Cannot create element", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }
    }

    public Element createElement(TextData tagName) {
        return this.createElement(tagName.toString());
    }

    public Element createElementNS(String namespaceURI, String qualifiedName)  {
        try {
            return docRoot.createElementNS(namespaceURI, qualifiedName);
        } catch (DOMException e) {
            XMLDOM2.DOMException errorVar = new XMLDOM2.DOMException("Cannot create element", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }
    }

    public EntityReference createEntityReference(String name) {
        try {
            return docRoot.createEntityReference(name);
        } catch (DOMException e) {
            XMLDOM2.DOMException errorVar = new XMLDOM2.DOMException("Cannot create Entity reference", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }
    }

    public ProcessingInstruction createProcessingInstruction(String target, String data) {
        try {
            return docRoot.createProcessingInstruction(target, data);
        } catch (DOMException e) {
            XMLDOM2.DOMException errorVar = new XMLDOM2.DOMException("Cannot create processing instruction", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }
    }

  /**
   * The createTextNode method creates a text node with the value of the data
   * parameter.
   *
   *
   * @param data
   *            The contents of the TextNode
   * @return a Text node
   */
    public Text createTextNode(String data) {
        return docRoot.createTextNode(data);
    }

  /**
   * The createTextNode method creates a text node with the value of the data
   * parameter.
   *
   *
   * @param data
   *            The contents of the TextNode
   * @return a Text node
   */
  public Text createTextNode(TextData data) {
    if (data == null) {
      return this.createTextNode((String)null);
    } else {
      return this.createTextNode(data.getValue());
    }
  }     
 
    public DocumentType getDoctype() {
        return docRoot.getDoctype();
    }

    public Element getDocumentElement() {
        return docRoot.getDocumentElement();
    }

    public String getDocumentURI() {     
        return docRoot.getDocumentURI();     
    }     

    public DOMConfiguration getDomConfig() {
        return docRoot.getDomConfig();
    }

    public Element getElementById(String elementId) {
        return docRoot.getElementById(elementId);
    }

    public NodeList getElementsByTagName(String tagname) {
        return docRoot.getElementsByTagName(tagname);
    }
   
    public NodeList getElementsByTagName(TextData tagName) {
      if (tagName == null) {
        return null;
      }
      else {
        return getElementsByTagName(tagName.getValue());
      }
    }

    public NodeList getElementsByTagNameNS(String namespaceURI, String localName) {
        return docRoot.getElementsByTagNameNS(namespaceURI, localName);
    }

    public DOMImplementation getImplementation() {
        return docRoot.getImplementation();
    }

    public String getInputEncoding() {     
        return docRoot.getInputEncoding();     
    }     

    public boolean getStrictErrorChecking() {     
        return docRoot.getStrictErrorChecking();     
    }     

    public String getXmlEncoding() {     
        return docRoot.getXmlEncoding();     
    }     

    public boolean getXmlStandalone() {     
        return docRoot.getXmlStandalone();     
    }     

    public String getXmlVersion() {     
        return docRoot.getXmlVersion();     
    }     

    /**
     * Import a new document from the passed input stream
     * @param is
     */
    public void importDocument(InputStream is) {
        docRoot = DomFactory.newDocument(is);
        // TF:23/06/2008:Remember "this" document so we can return it as the real owner.
        docRoot.setUserData(THIS_DOCUMENT_KEY, this, null);
    }

    /**
     * Using a MemoryStream (not an InputStream) seems much more reliable.  CraigM:12/01/2009.
     */
    public void importDocument(MemoryStream is) {
      docRoot = DomFactory.newDocument(is);
      // TF:23/06/2008:Remember "this" document so we can return it as the real owner.
      docRoot.setUserData(THIS_DOCUMENT_KEY, this, null);
    }
   
    public void importDocument(java.io.File file) {
        try {
      docRoot = DomFactory.newDocument(new FileInputStream(file));
          // TF:23/06/2008:Remember "this" document so we can return it as the real owner.
      docRoot.setUserData(THIS_DOCUMENT_KEY, this, null);
    }
    catch (FileNotFoundException e) {
            UsageException errorVar = new UsageException("Unable to open file " + file.getAbsolutePath() + " as stream");
            ErrorMgr.addError(errorVar);
            throw errorVar;
    }
    }
   
    public void importDocument(File file) {
        docRoot = DomFactory.newDocument(file.toInputStream());
        // TF:23/06/2008:Remember "this" document so we can return it as the real owner.
        docRoot.setUserData(THIS_DOCUMENT_KEY, this, null);
    }
   
    public Node importNode(Node importedNode, boolean deep) {
        try {
            return docRoot.importNode(importedNode, deep);
        } catch (DOMException e) {
            XMLDOM2.DOMException errorVar = new XMLDOM2.DOMException("Cannot import node", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }
    }

    public void normalizeDocument() {     
        docRoot.normalizeDocument();     
    }     

    public Node renameNode(Node n, String namespaceURI, String qualifiedName) {     
        try {     
            return docRoot.renameNode(n, namespaceURI, qualifiedName);     
        } catch (DOMException e) {     
            XMLDOM2.DOMException errorVar = new XMLDOM2.DOMException("Cannot rename node", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }     
    }     

    public void setDocumentURI(String documentURI) {     
        docRoot.setDocumentURI(documentURI);     
    }     

    public void setStrictErrorChecking(boolean strictErrorChecking) {     
        docRoot.setStrictErrorChecking(strictErrorChecking);     
    }     

    public void setXmlStandalone(boolean xmlStandalone) {     
        try {     
            docRoot.setXmlStandalone(xmlStandalone);     
        } catch (DOMException e) {     
            XMLDOM2.DOMException errorVar = new XMLDOM2.DOMException("Cannot set XML standalone", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }     
    }     

    public void setXmlVersion(String xmlVersion) {     
        try {     
            docRoot.setXmlVersion(xmlVersion);     
        } catch (DOMException e) {     
            XMLDOM2.DOMException errorVar = new XMLDOM2.DOMException("Cannot set XML version", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }     
    }     

    public Node appendChild(Node newChild) {
        try {
            return docRoot.appendChild(newChild);
        } catch (DOMException e) {
            XMLDOM2.DOMException errorVar = new XMLDOM2.DOMException("Cannot append child", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }
    }

    public Node cloneNode(boolean deep) {
        return docRoot.cloneNode(deep);
    }

    public short compareDocumentPosition(Node other) {     
        try {     
            return docRoot.compareDocumentPosition(other);     
        } catch (DOMException e) {     
            XMLDOM2.DOMException errorVar = new XMLDOM2.DOMException("Cannot compare document position", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }     
    }     

    public NamedNodeMap getAttributes() {
        return docRoot.getAttributes();
    }

    public String getBaseURI() {     
        return docRoot.getBaseURI();     
    }     

    public NodeList getChildNodes() {
        return docRoot.getChildNodes();
    }

    public Object getFeature(String feature, String version) {     
        return docRoot.getFeature(feature, version);     
    }     

    public Node getFirstChild() {
        return docRoot.getFirstChild();
    }

    public Node getLastChild() {
        return docRoot.getLastChild();
    }

    public String getLocalName() {
        return docRoot.getLocalName();
    }

    public String getNamespaceURI() {
        return docRoot.getNamespaceURI();
    }

    public Node getNextSibling() {
        return docRoot.getNextSibling();
    }

    public String getNodeName() {
        return docRoot.getNodeName();
    }

    public short getNodeType() {
        return docRoot.getNodeType();
    }

    public String getNodeValue() {
        try {
            return docRoot.getNodeValue();
        } catch (DOMException e) {
            XMLDOM2.DOMException errorVar = new XMLDOM2.DOMException("Cannot get Node Value", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }
    }

    /**
     * Get the document root. Unlike standard java, this node will return this object. (Java returns null,
     * however, forte would return this object, so we mirror Forte's behaviour)
     */
    public org.w3c.dom.Document getOwnerDocument() {
      return this;
        // return docRoot.getOwnerDocument();
    }
   
    /**
     * Get the owner document for any given node. This method is slightly different to Node.getOwnerDocument()
     * in that if the passed object is a Document, the passed document will be returned rather than null. The
     * other difference is that the return value will always be an instance of this Document class, not
     * org.w3c.dom.Document.
     * @param pNode
     * @return
     */
    public static Document getOwnerDocument(Node pNode) {
      org.w3c.dom.Document doc = pNode.getOwnerDocument();
      if (doc instanceof Document) {
        return (Document)doc;
      }
      else {
            // TF:23/06/2008:Return the document that was attached when the document implementation was created.
        Document realDoc = (Document)doc.getUserData(THIS_DOCUMENT_KEY);
        return realDoc;
      }
    }

    public Node getParentNode() {
        return docRoot.getParentNode();
    }

    public String getPrefix() {
        return docRoot.getPrefix();
    }

    public Node getPreviousSibling() {
        return docRoot.getPreviousSibling();
    }

    public String getTextContent() {     
        try {     
            return docRoot.getTextContent();     
        } catch (DOMException e) {     
            XMLDOM2.DOMException errorVar = new XMLDOM2.DOMException("Cannot get Text Content", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }     
    }     

    public Object getUserData(String key) {     
        return docRoot.getUserData(key);     
    }     

    public boolean hasAttributes() {
        return docRoot.hasAttributes();
    }

    public boolean hasChildNodes() {
        return docRoot.hasChildNodes();
    }

    public Node insertBefore(Node newChild, Node refChild) {
        try {
            return docRoot.insertBefore(newChild, refChild);
        } catch (DOMException e) {
            XMLDOM2.DOMException errorVar = new XMLDOM2.DOMException("Cannot insert before", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }
    }

    public boolean isDefaultNamespace(String namespaceURI) {     
        return docRoot.isDefaultNamespace(namespaceURI);     
    }     

    public boolean isEqualNode(Node arg) {     
        return docRoot.isEqualNode(arg);     
    }     

    public boolean isSameNode(Node other) {     
        return docRoot.isSameNode(other);     
    }     

    public boolean isSupported(String feature, String version) {
        return docRoot.isSupported(feature, version);
    }

    public String lookupNamespaceURI(String prefix) {     
        return docRoot.lookupNamespaceURI(prefix);     
    }     

    public String lookupPrefix(String namespaceURI) {     
        return docRoot.lookupPrefix(namespaceURI);     
    }     

    public void normalize() {
        docRoot.normalize();
    }

    public Node removeChild(Node oldChild) {
        try {
            return docRoot.removeChild(oldChild);
        } catch (DOMException e) {
            XMLDOM2.DOMException errorVar = new XMLDOM2.DOMException("Cannot remove child", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }
    }

    public Node replaceChild(Node newChild, Node oldChild) {
        try {
            return docRoot.replaceChild(newChild, oldChild);
        } catch (DOMException e) {
            XMLDOM2.DOMException errorVar = new XMLDOM2.DOMException("Cannot replace child", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }
    }

    public void setNodeValue(String nodeValue) {
        try {
            docRoot.setNodeValue(nodeValue);
        } catch (DOMException e) {
            XMLDOM2.DOMException errorVar = new XMLDOM2.DOMException("Cannot set Node Value", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }
    }

    public void setPrefix(String prefix)  {
        try {
            docRoot.setPrefix(prefix);
        } catch (DOMException e) {
            XMLDOM2.DOMException errorVar = new XMLDOM2.DOMException("Cannot set prefix", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }
    }

    public void setTextContent(String textContent) {     
        try {     
            docRoot.setTextContent(textContent);     
        } catch (DOMException e) {     
            XMLDOM2.DOMException errorVar = new XMLDOM2.DOMException("Cannot set Text Context", e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }     
    }     

    public Object setUserData(String key, Object data, UserDataHandler handler) {     
        return docRoot.setUserData(key, data, handler);     
    }     

    public void exportDocument(MemoryStream stream){     
        DOMImplementationLS implls=(DOMImplementationLS)getImplementation();     
        LSSerializer domWriter = implls.createLSSerializer();     
        LSOutput output=implls.createLSOutput();     
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();     
        output.setByteStream(outputStream);     
        output.setEncoding("UTF-8");     
        domWriter.write(this.docRoot, output);     
        stream.writeByteArray(outputStream.toByteArray());           
    }

}
TOP

Related Classes of XMLDOM2.Document

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.