Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import org.w3c.dom.*;

public class Main {
    /**
     * Clone given Node into target Document. If targe is null, same Document will be used.
     * If deep is specified, all children below will also be cloned.
     */
    public final static Node cloneNode(Node node, Document target, boolean deep) throws DOMException {
        if ((target == null) || (node.getOwnerDocument() == target)) {
            // same Document
            return node.cloneNode(deep);
        } else {
            //DOM level 2 provides this in Document, so once xalan switches to that,
            //we can take out all the below and just call target.importNode(node, deep);
            //For now, we implement based on the javadocs for importNode
            Node newNode;
            int nodeType = node.getNodeType();

            switch (nodeType) {
            case Node.ATTRIBUTE_NODE:
                newNode = target.createAttribute(node.getNodeName());

                break;

            case Node.DOCUMENT_FRAGMENT_NODE:
                newNode = target.createDocumentFragment();

                break;

            case Node.ELEMENT_NODE:

                Element newElement = target.createElement(node.getNodeName());
                NamedNodeMap nodeAttr = node.getAttributes();

                if (nodeAttr != null) {
                    for (int i = 0; i < nodeAttr.getLength(); i++) {
                        Attr attr = (Attr) nodeAttr.item(i);

                        if (attr.getSpecified()) {
                            Attr newAttr = (Attr) cloneNode(attr, target, true);
                            newElement.setAttributeNode(newAttr);
                        }
                    }
                }

                newNode = newElement;

                break;

            case Node.ENTITY_REFERENCE_NODE:
                newNode = target.createEntityReference(node.getNodeName());

                break;

            case Node.PROCESSING_INSTRUCTION_NODE:
                newNode = target.createProcessingInstruction(node.getNodeName(), node.getNodeValue());

                break;

            case Node.TEXT_NODE:
                newNode = target.createTextNode(node.getNodeValue());

                break;

            case Node.CDATA_SECTION_NODE:
                newNode = target.createCDATASection(node.getNodeValue());

                break;

            case Node.COMMENT_NODE:
                newNode = target.createComment(node.getNodeValue());

                break;

            case Node.NOTATION_NODE:
            case Node.ENTITY_NODE:
            case Node.DOCUMENT_TYPE_NODE:
            case Node.DOCUMENT_NODE:
            default:
                throw new IllegalArgumentException("Importing of " + node + " not supported yet");
            }

            if (deep) {
                for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
                    newNode.appendChild(cloneNode(child, target, true));
                }
            }

            return newNode;
        }
    }
}