01:
02: /*
03: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
04: * for visualizing and manipulating spatial features with geometry and attributes.
05: *
06: * Copyright (C) 2003 Vivid Solutions
07: *
08: * This program is free software; you can redistribute it and/or
09: * modify it under the terms of the GNU General Public License
10: * as published by the Free Software Foundation; either version 2
11: * of the License, or (at your option) any later version.
12: *
13: * This program is distributed in the hope that it will be useful,
14: * but WITHOUT ANY WARRANTY; without even the implied warranty of
15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16: * GNU General Public License for more details.
17: *
18: * You should have received a copy of the GNU General Public License
19: * along with this program; if not, write to the Free Software
20: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21: *
22: * For more information, contact:
23: *
24: * Vivid Solutions
25: * Suite #1A
26: * 2328 Government Street
27: * Victoria BC V8T 5G5
28: * Canada
29: *
30: * (250)385-6040
31: * www.vividsolutions.com
32: */
33:
34: package com.vividsolutions.wms.util;
35:
36: import org.w3c.dom.Node;
37: import org.w3c.dom.NodeList;
38:
39: /**
40: * Provides some simple XML utilities for the WMS implementation to use.
41: *
42: * @author Chris Hodgson chodgson@refractions.net
43: */
44:
45: public class XMLTools {
46: /**
47: * Recursively prints out the DOM structure underneath a Node.
48: * The prefix parameter is used in the recursive call to indent properly,
49: * but it can also be used in the initial call to provide an initial prefix
50: * or indentation.
51: * @param n the Node to print out
52: * @param prefix the prefix to use
53: */
54: public static void printNode(Node n, String prefix) {
55: System.out.println(prefix + n.toString());
56: NodeList nl = n.getChildNodes();
57: for (int i = 0; i < nl.getLength(); i++) {
58: printNode(nl.item(i), prefix + " ");
59: }
60: }
61:
62: /**
63: * A very simple XPath implementation.
64: * Recursively drills down into the DOM tree, starting at the given parent
65: * Node, following the provided XPath. The XPath string is a slash-delimited
66: * list of element names to drill down into, the node with the last name in
67: * the list is returned
68: * @param parent the parent node to search into
69: * @param xpath the simplified XPath search string
70: * @return the Node found at the end of the search, or null if the search
71: * failed to find the specified node.
72: */
73: public static Node simpleXPath(Node parent, String xpath) {
74: String name;
75: String nextPath = null;
76: if (xpath.indexOf('/') > 0) {
77: name = xpath.substring(0, xpath.indexOf('/'));
78: nextPath = xpath.substring(xpath.indexOf('/') + 1);
79: } else {
80: name = xpath;
81: }
82: NodeList nl = parent.getChildNodes();
83: for (int i = 0; i < nl.getLength(); i++) {
84: Node n = nl.item(i);
85: if (n.getNodeType() == Node.ELEMENT_NODE
86: && n.getNodeName().equals(name)) {
87: if (nextPath == null) {
88: return n;
89: } else {
90: return simpleXPath(n, nextPath);
91: }
92: }
93: }
94: return null;
95: }
96:
97: }
|