LibreOffice Module xmerge (master) 1
XmlUtil.java
Go to the documentation of this file.
1/*
2 * This file is part of the LibreOffice project.
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 *
8 * This file incorporates work covered by the following license notice:
9 *
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
17 */
18
19package org.openoffice.xmerge.util;
20
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;
26
30public final class XmlUtil {
31
42 public static Node deepClone(Node oldNode, Node newNode) {
43 Document docNode = oldNode.getOwnerDocument();
44
45 // clone the starting node
46 Node clonedNode = cloneNode(docNode, newNode);
47
48 if (clonedNode != null) {
49 // then clone the sub-tree recursively
50 cloneTree(docNode, clonedNode, newNode);
51 }
52
53 return clonedNode;
54 }
55
63 private static void cloneTree(Document docNode, Node oldNode, Node newNode) {
64
65 NodeList nodeList = newNode.getChildNodes();
66 int nodeListLen = nodeList.getLength();
67
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));
73 }
74 }
75 }
76
85 private static Node cloneNode(Document docNode, Node newNode) {
86
87 Node clonedNode = null;
88
89 // only support text node and element node (will copy the attributes)
90 switch (newNode.getNodeType()) {
91 case Node.TEXT_NODE:
92 String textStr = newNode.getNodeValue();
93 clonedNode = docNode.createTextNode(textStr);
94 break;
95 case Node.ELEMENT_NODE:
96 Element oldElem = (Element)newNode;
97 String tagName = newNode.getNodeName();
98 Element newElem = docNode.createElement(tagName);
99
100 // copy the attributes
101 NamedNodeMap attrs = oldElem.getAttributes();
102
103 for (int i = 0; i < attrs.getLength(); i++) {
104 newElem.setAttribute(attrs.item(i).getNodeName(),
105 attrs.item(i).getNodeValue());
106 }
107 clonedNode = newElem;
108 break;
109 }
110 return clonedNode;
111 }
112
120 public static String getNodeInfo(Node node) {
121
122 String str = null;
123 switch (node.getNodeType()) {
124
125 case Node.ELEMENT_NODE:
126 str = "ELEMENT";
127 break;
128 case Node.ATTRIBUTE_NODE:
129 str = "ATTRIBUTE";
130 break;
131 case Node.TEXT_NODE:
132 str = "TEXT";
133 break;
134 case Node.CDATA_SECTION_NODE:
135 str = "CDATA_SECTION";
136 break;
137 case Node.ENTITY_REFERENCE_NODE:
138 str = "ENTITY_REFERENCE";
139 break;
140 case Node.ENTITY_NODE:
141 str = "ENTITY";
142 break;
143 case Node.PROCESSING_INSTRUCTION_NODE:
144 str = "PROCESSING_INSTRUCTION";
145 break;
146 case Node.COMMENT_NODE:
147 str = "COMMENT";
148 break;
149 case Node.DOCUMENT_NODE:
150 str = "DOCUMENT";
151 break;
152 case Node.DOCUMENT_TYPE_NODE:
153 str = "DOCUMENT_TYPE";
154 break;
155 case Node.DOCUMENT_FRAGMENT_NODE:
156 str = "DOCUMENT_FRAGMENT";
157 break;
158 case Node.NOTATION_NODE:
159 str = "NOTATION";
160 break;
161 }
162
163 StringBuffer buffer = new StringBuffer("name=\"");
164 buffer.append(node.getNodeName());
165 buffer.append("\" type=\"");
166 buffer.append(str);
167 buffer.append("\"");
168
169 return buffer.toString();
170 }
171}
Class containing static utility methods for handling XML trees.
Definition: XmlUtil.java:30
static String getNodeInfo(Node node)
Returns the name and type of an XML DOM Node.
Definition: XmlUtil.java:120
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.
Definition: XmlUtil.java:42
static void cloneTree(Document docNode, Node oldNode, Node newNode)
Clone the sub-tree under certain given Node.
Definition: XmlUtil.java:63
static Node cloneNode(Document docNode, Node newNode)
Clone a Node (either text or element).
Definition: XmlUtil.java:85
A Document represents any Document to be converted and the resulting Document from any conversion.
Definition: Document.java:36
int i