Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e99f73b4aa8e52bea48abef76f9763bb4fe4e8c8 (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
/*******************************************************************************
 * Copyright (c) 2008, 2011 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.wizards.settingswizards;

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

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

public class XMLUtils {

	private XMLUtils() {
	}

	public static List<Element> extractChildElements(Element node, String childElementName)
			throws SettingsImportExportException {
		List<Element> extracted = new ArrayList<Element>();

		NodeList children = node.getChildNodes();
		for (int i = 0; i < children.getLength(); i++) {
			Node child = children.item(i);
			switch (child.getNodeType()) {
			case Node.ELEMENT_NODE:
				Element element = (Element) child;
				if (element.getTagName().equals(childElementName)) {
					extracted.add(element);
				} else
					throw new SettingsImportExportException("Unknown tag: " + element.getTagName()); //$NON-NLS-1$
				break;
			case Node.TEXT_NODE:
				Text text = (Text) child;
				if (isWhitespace(text.getData()))
					break;
				throw new SettingsImportExportException("Unknown text: '" + text.getData() + "'"); //$NON-NLS-1$ //$NON-NLS-2$
			default:
				throw new SettingsImportExportException("Unknown node: " + child.getNodeName()); //$NON-NLS-1$
			}
		}

		return extracted;
	}

	public static boolean isWhitespace(String s) {
		if (s == null)
			return false;

		for (char c : s.toCharArray()) {
			if (!Character.isWhitespace(c)) {
				return false;
			}
		}

		return true;
	}
}

Back to the top