Skip to main content
summaryrefslogtreecommitdiffstats
blob: 196a87c7eb5df595efec057149bb87db49f4495b (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
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. 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:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.pagedesigner.utils;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.wst.xml.core.internal.document.ElementImpl;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

/**
 * @author mengbo
 */
public class DOMUtil {
	/**
	 * @param parent
	 * @param tag
	 * @return the list of child elements  of parent that are Elements
	 * and that have name 'tag' ignoring case
	 */
	public static List getChildElementsByTagIgnoreCase(Element parent,
			String tag) {
		List ret = new ArrayList();
		NodeList nodeList = parent.getChildNodes();
		for (int i = 0, size = nodeList.getLength(); i < size; i++) {
			Node node = nodeList.item(i);
			if (node.getNodeType() == Node.ELEMENT_NODE) {
				String t = node.getNodeName();
				if (tag.equalsIgnoreCase(t)) {
					ret.add(node);
				}
			}
		}
		return ret;
	}

	/**
	 * @param ele
	 * @return the element value of the TEXT_NODE children of element
	 * concat'd together
	 */
	public static String getTextElementValue(Element ele) {
		StringBuffer buffer = new StringBuffer();
		Node node = ele.getFirstChild();
		while (node != null) {
			if (node.getNodeType() == Node.TEXT_NODE) {
				buffer.append(node.getNodeValue());
			} else if (node.getNodeType() == Node.CDATA_SECTION_NODE) {
				buffer.append(node.getNodeValue());
			}
			node = node.getNextSibling();
		}
		return buffer.toString();
	}

	/**
	 * @param element
	 * @param string
	 * @return the attribute named string on element ignoring case in the comparison
	 * or null if not found
	 */
	public static String getAttributeIgnoreCase(Element element, String string) {
		NamedNodeMap map = element.getAttributes();
		for (int i = 0; i < map.getLength(); i++) {
			Node attr = map.item(i);
			if (string.equalsIgnoreCase(attr.getNodeName())) {
				return attr.getNodeValue();
			}
		}
		return null;
	}

	/**
	 * @param parent
	 * @param tags
	 * @return the list of children of parent with name in tags ignoring case
	 */
	public static List getChildrenByTagsIgnoreCase(Element parent, String[] tags) {
		List result = new ArrayList();
		NodeList nodeList = parent.getChildNodes();
		for (int i = 0, size = nodeList.getLength(); i < size; i++) {
			Node node = nodeList.item(i);
			if (node.getNodeType() == Node.ELEMENT_NODE) {
				String t = node.getNodeName();
				for (int k = 0; k < tags.length; k++) {
					if (tags[k].equalsIgnoreCase(t)) {
						result.add(node);
						break;
					}
				}
			}
		}
		return result;
	}

	/**
	 * @param ele
	 */
	public static void removeAllChildren(Element ele) {
		((ElementImpl) ele).removeChildNodes();
	}

	/**
	 * @param ele
	 * @param value
	 */
	public static void setTextElementValue(Element ele, String value) {
		removeAllChildren(ele);
		Text txt = ele.getOwnerDocument().createTextNode(value);
		ele.appendChild(txt);
	}

	/**
	 * @param ele 
	 * @param attr 
	 * @param defaultvalue 
	 * @return the integer attribute of ele called attr.  Default value
	 * is returned if the attribute is not found.
	 */
	public static int getIntAttributeIgnoreCase(Element ele, String attr,
			int defaultvalue) {
		if (ele == null) {
			return defaultvalue;
		}
		String attrvalue = getAttributeIgnoreCase(ele, attr);
		if (attrvalue == null) {
			return defaultvalue;
		}
        try {
        	return Integer.parseInt(attrvalue);
        } catch (NumberFormatException  ex) {
        	return defaultvalue;
        }
	}

	/**
	 * get all child elements
	 * 
	 * @param ele
	 * @return the list of element children of type ELEMENT_NODE
	 */
	public static List getElementChildren(Element ele) {
		List ret = new ArrayList();
		NodeList nodeList = ele.getChildNodes();
		for (int i = 0, size = nodeList.getLength(); i < size; i++) {
			Node node = nodeList.item(i);
			if (node.getNodeType() == Node.ELEMENT_NODE) {
				ret.add(node);
			}
		}
		return ret;
	}

	/**
	 * judge whether element has an attribute named attrName
	 * 
	 * @param ele
	 * @param attrName
	 * @return true if element has attribute called attrName ignoring
	 * case  in the comparison.
	 */
	public static boolean hasAttribute(Element ele, String attrName) {
		NamedNodeMap map = ele.getAttributes();
		for (int i = 0; i < map.getLength(); i++) {
			Node attr = map.item(i);
			if (attr.getNodeName().equalsIgnoreCase(attrName)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * @param node
	 * @param sb
	 */
	public static void nodeToString(Node node, StringBuffer sb) {
		int type = node.getNodeType();
		switch (type) {
		case Node.DOCUMENT_NODE:
			sb.append("<?xml version=\"1.0\" ?>"); //$NON-NLS-1$
			nodeToString(((Document) node).getDocumentElement(), sb);
			break;

		case Node.ELEMENT_NODE:
			sb.append("<"); //$NON-NLS-1$
			sb.append(node.getNodeName());
			NamedNodeMap attrs = node.getAttributes();
			for (int i = 0; i < attrs.getLength(); i++) {
				Node attr = attrs.item(i);
				sb.append(" " + attr.getNodeName() + "=\"" //$NON-NLS-1$ //$NON-NLS-2$
						+ attr.getNodeValue() + "\""); //$NON-NLS-1$
			}

			NodeList children = node.getChildNodes();
			if (children != null) {
				int len = children.getLength();
				if (len != 0) {
					sb.append(">"); //$NON-NLS-1$
				}
				for (int i = 0; i < len; i++) {
					nodeToString(children.item(i), sb);
				}
			}
			break;

		case Node.ENTITY_REFERENCE_NODE:
			sb.append("&"); //$NON-NLS-1$
			sb.append(node.getNodeName());
			sb.append(";"); //$NON-NLS-1$
			break;

		case Node.CDATA_SECTION_NODE:
			sb.append("<![CDATA["); //$NON-NLS-1$
			sb.append(node.getNodeValue());
			sb.append("]]>"); //$NON-NLS-1$
			break;

		case Node.TEXT_NODE:
			sb.append(node.getNodeValue());
			break;

		case Node.PROCESSING_INSTRUCTION_NODE:
			sb.append("<?"); //$NON-NLS-1$
			sb.append(node.getNodeName());
			String data = node.getNodeValue();
			{
				sb.append(" "); //$NON-NLS-1$
				sb.append(data);
			}
			sb.append("?>"); //$NON-NLS-1$
			break;
		}

		if (type == Node.ELEMENT_NODE) {
			if (node.getFirstChild() != null) {
				sb.append("</"); //$NON-NLS-1$
				sb.append(node.getNodeName());
				sb.append(">"); //$NON-NLS-1$
			} else {
				sb.append("/>"); //$NON-NLS-1$
			}

		}
	}
}

Back to the top