19package org.openoffice.xmerge.util;
21import org.w3c.dom.Node;
22import org.w3c.dom.Document;
23import org.w3c.dom.NodeList;
24import org.w3c.dom.Element;
25import org.w3c.dom.NamedNodeMap;
42 public static Node
deepClone(Node oldNode, Node newNode) {
43 Document docNode = oldNode.getOwnerDocument();
46 Node clonedNode =
cloneNode(docNode, newNode);
48 if (clonedNode !=
null) {
65 NodeList nodeList = newNode.getChildNodes();
66 int nodeListLen = nodeList.getLength();
68 for (
int i = 0;
i < nodeListLen;
i++) {
69 Node newClonedChild =
cloneNode(docNode, nodeList.item(
i));
70 if (newClonedChild !=
null) {
71 oldNode.appendChild(newClonedChild);
72 cloneTree(docNode, newClonedChild, nodeList.item(
i));
87 Node clonedNode =
null;
90 switch (newNode.getNodeType()) {
92 String textStr = newNode.getNodeValue();
93 clonedNode = docNode.createTextNode(textStr);
95 case Node.ELEMENT_NODE:
96 Element oldElem = (Element)newNode;
97 String tagName = newNode.getNodeName();
98 Element newElem = docNode.createElement(tagName);
101 NamedNodeMap attrs = oldElem.getAttributes();
103 for (
int i = 0;
i < attrs.getLength();
i++) {
104 newElem.setAttribute(attrs.item(
i).getNodeName(),
105 attrs.item(
i).getNodeValue());
107 clonedNode = newElem;
123 switch (node.getNodeType()) {
125 case Node.ELEMENT_NODE:
128 case Node.ATTRIBUTE_NODE:
134 case Node.CDATA_SECTION_NODE:
135 str =
"CDATA_SECTION";
137 case Node.ENTITY_REFERENCE_NODE:
138 str =
"ENTITY_REFERENCE";
140 case Node.ENTITY_NODE:
143 case Node.PROCESSING_INSTRUCTION_NODE:
144 str =
"PROCESSING_INSTRUCTION";
146 case Node.COMMENT_NODE:
149 case Node.DOCUMENT_NODE:
152 case Node.DOCUMENT_TYPE_NODE:
153 str =
"DOCUMENT_TYPE";
155 case Node.DOCUMENT_FRAGMENT_NODE:
156 str =
"DOCUMENT_FRAGMENT";
158 case Node.NOTATION_NODE:
163 StringBuffer buffer =
new StringBuffer(
"name=\"");
164 buffer.append(node.getNodeName());
165 buffer.append(
"\" type=\"");
169 return buffer.toString();
Class containing static utility methods for handling XML trees.
static String getNodeInfo(Node node)
Returns the name and type of an XML DOM Node.
static Node deepClone(Node oldNode, Node newNode)
Perform a deep clone of certain Node which will base on the document Node of the old Node.
static void cloneTree(Document docNode, Node oldNode, Node newNode)
Clone the sub-tree under certain given Node.
static Node cloneNode(Document docNode, Node newNode)
Clone a Node (either text or element).
A Document represents any Document to be converted and the resulting Document from any conversion.