Skip to main content
summaryrefslogtreecommitdiffstats
blob: 12fb15a84226ebdcbe2758096efe7eca30600d69 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*******************************************************************************
 * Copyright (c) 2003, 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - Initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.server.tomcat.core.internal.xml;

import java.util.LinkedHashMap;
import java.util.Map;

import org.w3c.dom.*;
/**
 * An XML element.
 */
public class XMLElement {
	private Element xmlElement;
	protected Factory factory;

	public XMLElement() {
		// do nothing
	}
	
	public Element getElementNode() {
		return xmlElement;
	}

	public Attr addAttribute(String s, String s1) {
		Attr attr = factory.createAttribute(s, xmlElement);
		attr.setValue(s1);
		return attr;
	}

	public XMLElement createElement(int index, String s) {
		return factory.createElement(index, s, xmlElement);
	}

	public XMLElement createElement(String s) {
		return factory.createElement(s, xmlElement);
	}

	public XMLElement findElement(String s) {
		NodeList nodelist = xmlElement.getElementsByTagName(s);
		int i = nodelist == null ? 0 : nodelist.getLength();
		for (int j = 0; j < i; j++) {
			Node node = nodelist.item(j);
			String s1 = node.getNodeName().trim();
			if (s1.equals(s))
				return factory.newInstance((Element) node);
		}
	
		return createElement(s);
	}

	public XMLElement findElement(String s, int i) {
		NodeList nodelist = xmlElement.getElementsByTagName(s);
		int j = nodelist == null ? 0 : nodelist.getLength();
		for (int k = 0; k < j; k++) {
			Node node = nodelist.item(k);
			String s1 = node.getNodeName().trim();
			if (s1.equals(s) && k == i)
				return factory.newInstance((Element) node);
		}
	
		return createElement(s);
	}

	public String getAttributeValue(String s) {
		Attr attr = xmlElement.getAttributeNode(s);
		if (attr != null)
			return attr.getValue();
		
		return null;
	}
	
	public Map getAttributes() {
		Map attributes = new LinkedHashMap();
		NamedNodeMap attrs = xmlElement.getAttributes();
		if (null != attrs) {
			for (int i = 0; i < attrs.getLength(); i++) {
				Node attr = attrs.item(i);
				String name = attr.getNodeName();
				String value = attr.getNodeValue();
				attributes.put(name, value);
			}
		}
		return attributes;
	}
	
	public String getElementName() {
		return xmlElement.getNodeName();
	}
	
	public String getElementValue() {
		return getElementValue(xmlElement);
	}
	
	protected static String getElementValue(Element element) {
		String s = element.getNodeValue();
		if (s != null)
			return s;
		NodeList nodelist = element.getChildNodes();
		for (int i = 0; i < nodelist.getLength(); i++)
			if (nodelist.item(i) instanceof Text)
				return ((Text) nodelist.item(i)).getData();
	
		return null;
	}
	
	public Element getSubElement(String s) {
		NodeList nodelist = xmlElement.getElementsByTagName(s);
		int i = nodelist == null ? 0 : nodelist.getLength();
		for (int j = 0; j < i; j++) {
			Node node = nodelist.item(j);
			String s1 = node.getNodeName().trim();
			if (s1.equals(s))
				return (Element) node;
		}
	
		return null;
	}

	public String getSubElementValue(String s) {
		Element element = getSubElement(s);
		if (element == null)
			return null;
	
		String value = getElementValue(element);
		if (value == null)
			return null;
		
		return value.trim();
	}

	public boolean removeAttribute(String s) {
		try {
			xmlElement.removeAttribute(s);
			return true;
		} catch (Exception ex) {
			return false;
		}
	}

	public boolean removeElement(String s, int i) {
		NodeList nodelist = xmlElement.getElementsByTagName(s);
		int j = nodelist == null ? 0 : nodelist.getLength();
		for (int k = 0; k < j; k++) {
			Node node = nodelist.item(k);
			String s1 = node.getNodeName().trim();
			if (s1.equals(s) && k == i) {
				xmlElement.removeChild(node);
				return true;
			}
		}
	
		return false;
	}

	public void setAttributeValue(String s, String s1) {
		Attr attr = xmlElement.getAttributeNode(s);
		if (attr == null)
			attr = addAttribute(s, s1);
		else
			attr.setValue(s1);
	}

	void setElement(Element element) {
		xmlElement = element;
	}

	protected static void setElementValue(Element element, String value) {
		String s = element.getNodeValue();
		if (s != null) {
			element.setNodeValue(value);
			return;
		}
		NodeList nodelist = element.getChildNodes();
		for (int i = 0; i < nodelist.getLength(); i++)
			if (nodelist.item(i) instanceof Text) {
				Text text = (Text) nodelist.item(i);
				text.setData(value);
				return;
			}
	
		return;
	}

	void setFactory(Factory factory1) {
		factory = factory1;
	}

	public void setSubElementValue(String s, String value) {
		Element element = getSubElement(s);
		if (element == null) {
			element = factory.document.createElement(s);
			element.appendChild(factory.document.createTextNode("temp"));
			xmlElement.appendChild(element);
		}
		setElementValue(element, value);
	}

	public int sizeOfElement(String s) {
		NodeList nodelist = xmlElement.getElementsByTagName(s);
		int i = nodelist == null ? 0 : nodelist.getLength();
		return i;
	}

	public void updateElementValue(String s) {
		try {
			xmlElement.setNodeValue(s);
		} catch (DOMException ex) {
			NodeList nodelist = xmlElement.getChildNodes();
			int i = nodelist == null ? 0 : nodelist.getLength();
			if (i > 0) {
				for (int j = 0; j < i; j++)
					if (nodelist.item(j) instanceof Text) {
						((Text) nodelist.item(j)).setData(s);
						return;
					}
			} else {
				xmlElement.appendChild(factory.document.createTextNode(s));
			}
		}
	}
	
	public boolean hasChildNodes() {
		return xmlElement.hasChildNodes();
	}
	
	public void removeChildren()
	{
		while (xmlElement.hasChildNodes()) {
			xmlElement.removeChild(xmlElement.getFirstChild());
		}
	}
	
	public void copyChildrenTo(XMLElement destination) {
		NodeList nodelist = xmlElement.getChildNodes();
		int len = nodelist == null ? 0 : nodelist.getLength();
		for (int i = 0; i < len; i++) {
			Node node = nodelist.item(i);
			destination.importNode(node, true);
		}
	}
	
	public void importNode(Node node, boolean deep) {
		xmlElement.appendChild(xmlElement.getOwnerDocument().importNode(node, deep));
	}

	/**
	 * This method tries to compare two XMLElements for equivalence. Due to
	 * the lack of normalization, they aren't compared for equality. Elements
	 * are required to have the same attributes or the same node value
	 * if attributes aren't present. Attributes and node value are assumed
	 * to be mutually exclusive for Tomcat configuration XML files. The
	 * same non-text child nodes are required to be present in an element
	 * and appear in the same order. If a node type other than element or
	 * comment is encountered, this method punts and returns false.
	 * 
	 * @param obj XMLElement to compare
	 * @return true if the elements are equivalent
	 */
	public boolean isEquivalent(XMLElement obj) {
		if (obj != null) {
			try {
				return elementsAreEquivalent(xmlElement, obj.getElementNode());
			}
			catch (Exception e) {
				// Catch and ignore just to be safe
			}
		}
		return false;
	}

	/**
	 * Same as isEquivalent() but doesn't ignore exceptions for test purposes.
	 * This avoids hiding an expected mismatch behind an unexpected exception.
	 * 
	 * @param obj XMLElement to compare
	 * @return true if the elements are equivalent
	 */
	public boolean isEquivalentTest(XMLElement obj) {
		if (obj != null) {
			return elementsAreEquivalent(xmlElement, obj.getElementNode());
		}
		return false;
	}
	
	private static boolean elementsAreEquivalent(Element element, Element otherElement) {
		if (element == otherElement)
			return true;
		
		if (!element.getNodeName().equals(otherElement.getNodeName()))
			return false;
		
		if (element.hasChildNodes()) {
			if (otherElement.hasChildNodes() && attributesAreEqual(element, otherElement)) {
				// Compare child nodes
				NodeList nodelist = element.getChildNodes();
				NodeList otherNodelist = otherElement.getChildNodes();
				if (nodelist.getLength() == otherNodelist.getLength()) {
					Node node = nextNonTextNode(element.getFirstChild());
					Node otherNode = nextNonTextNode(otherElement.getFirstChild());
					while (node != null) {
						if (otherNode == null)
							return false;
						short nextNodeType = node.getNodeType();
						if (nextNodeType != otherNode.getNodeType())
							return false;
						// If elements, compare 
						if (nextNodeType == Node.ELEMENT_NODE) {
							if (!elementsAreEquivalent((Element)node, (Element)otherNode))
								return false;
						}
						// Else if comment, compare
						else if (nextNodeType == Node.COMMENT_NODE) {
							if (!nodeValuesAreEqual(node, otherNode))
								return false;
						}
						// Else punt on other node types
						else {
							return false;
						}
						node = nextNonTextNode(node.getNextSibling());
						otherNode = nextNonTextNode(otherNode.getNextSibling());
					}
					// If also at end of other children, return equal
					if (otherNode == null)
						return true;
				}
			}
		}
		else if (!otherElement.hasChildNodes()) {
			return attributesAreEqual(element, otherElement);
		}
		return false;
	}
	
	private static Node nextNonTextNode(Node node) {
		while (node != null && node.getNodeType() == Node.TEXT_NODE)
			node = node.getNextSibling();
		return node;
	}

	private static boolean attributesAreEqual(Element element, Element otherElement) {
		NamedNodeMap attrs = element.getAttributes();
		NamedNodeMap otherAttrs = otherElement.getAttributes();
		if (attrs == null && otherAttrs == null) {
			// Return comparison of element values if there are no attributes
			return nodeValuesAreEqual(element, otherElement);
		}
		
		if (attrs.getLength() == otherAttrs.getLength()) {
			if (attrs.getLength() == 0)
				// Return comparison of element values if there are no attributes
				return nodeValuesAreEqual(element, otherElement);
			
			for (int i = 0; i < attrs.getLength(); i++) {
				Node attr = attrs.item(i);
				Node otherAttr = otherAttrs.getNamedItem(attr.getNodeName());
				if (!nodeValuesAreEqual(attr, otherAttr))
					return false;
			}
			return true;
		}
		return false;
	}
	
	private static boolean nodeValuesAreEqual(Node node, Node otherNode) {
		String value = node.getNodeValue();
		String otherValue = otherNode.getNodeValue();
		if (value != null && otherValue != null) {
			if (value.equals(otherValue))
				return true;
		}
		else if (value == null && otherValue == null)
			return true;
		return false;
	}
}

Back to the top