Mercurial > repos > ganjoo > webservice_toolsuite
comparison WebServiceToolWorkflow/lib/SAWADLParser/src/edu/uga/cs/lsdis/meteors/wadls/util/xml/XPathUtils.java @ 0:d5cd409b8a18 default tip
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
author | ganjoo |
---|---|
date | Tue, 07 Jun 2011 18:00:50 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:d5cd409b8a18 |
---|---|
1 /* | |
2 * (c) Copyright IBM Corp 2001, 2005 | |
3 */ | |
4 | |
5 package edu.uga.cs.lsdis.meteors.wadls.util.xml; | |
6 | |
7 import java.util.Vector; | |
8 import org.w3c.dom.*; | |
9 | |
10 /** | |
11 * A <code>XPathUtils</code> ... | |
12 * | |
13 * @author Matthew J. Duftler (duftler@us.ibm.com) | |
14 * @author Sanjiva Weerawarana (sanjiva@watson.ibm.com) | |
15 */ | |
16 public class XPathUtils | |
17 { | |
18 private static Node getPreviousTypedNode(Node node, short nodeType) | |
19 { | |
20 node = node.getPreviousSibling(); | |
21 | |
22 while (node != null && node.getNodeType() != nodeType) | |
23 { | |
24 node = node.getPreviousSibling(); | |
25 } | |
26 | |
27 return node; | |
28 } | |
29 | |
30 private static Node getNextTypedNode(Node node, short nodeType) | |
31 { | |
32 node = node.getNextSibling(); | |
33 | |
34 while (node != null && node.getNodeType() != nodeType) | |
35 { | |
36 node = node.getNextSibling(); | |
37 } | |
38 | |
39 return node; | |
40 } | |
41 | |
42 private static String getValue(Node node, short nodeType) | |
43 { | |
44 switch (nodeType) | |
45 { | |
46 case Node.ELEMENT_NODE : | |
47 return ((Element)node).getTagName(); | |
48 | |
49 case Node.TEXT_NODE : | |
50 return ((Text)node).getData(); | |
51 | |
52 case Node.PROCESSING_INSTRUCTION_NODE : | |
53 return ((ProcessingInstruction)node).getData(); | |
54 | |
55 default : | |
56 return ""; | |
57 } | |
58 } | |
59 | |
60 private static short getNodeType(Node node) | |
61 { | |
62 return (node != null ? node.getNodeType() : -1); | |
63 } | |
64 | |
65 private static String getXPathFromVector(Vector path) | |
66 { | |
67 StringBuffer strBuf = new StringBuffer(); | |
68 int length = path.size(); | |
69 | |
70 for (int i = 0; i < length; i++) | |
71 { | |
72 Node tempNode = (Node)path.elementAt(i); | |
73 short nodeType = getNodeType(tempNode); | |
74 String targetValue = getValue(tempNode, nodeType); | |
75 int position = 1; | |
76 | |
77 tempNode = getPreviousTypedNode(tempNode, nodeType); | |
78 | |
79 while (tempNode != null) | |
80 { | |
81 if (nodeType == Node.ELEMENT_NODE) | |
82 { | |
83 if (getValue(tempNode, nodeType).equals(targetValue)) | |
84 { | |
85 position++; | |
86 } | |
87 } | |
88 else | |
89 { | |
90 position++; | |
91 } | |
92 | |
93 tempNode = getPreviousTypedNode(tempNode, nodeType); | |
94 } | |
95 | |
96 boolean hasMatchingSiblings = (position > 1); | |
97 | |
98 if (!hasMatchingSiblings) | |
99 { | |
100 tempNode = (Node)path.elementAt(i); | |
101 tempNode = getNextTypedNode(tempNode, nodeType); | |
102 | |
103 while (!hasMatchingSiblings && tempNode != null) | |
104 { | |
105 if (nodeType == Node.ELEMENT_NODE) | |
106 { | |
107 if (getValue(tempNode, nodeType).equals(targetValue)) | |
108 { | |
109 hasMatchingSiblings = true; | |
110 } | |
111 else | |
112 { | |
113 tempNode = getNextTypedNode(tempNode, nodeType); | |
114 } | |
115 } | |
116 else | |
117 { | |
118 hasMatchingSiblings = true; | |
119 } | |
120 } | |
121 } | |
122 | |
123 String step; | |
124 | |
125 switch (nodeType) | |
126 { | |
127 case Node.TEXT_NODE : | |
128 step = "text()"; | |
129 break; | |
130 case Node.PROCESSING_INSTRUCTION_NODE : | |
131 step = "processing-instruction()"; | |
132 break; | |
133 default : | |
134 step = targetValue; | |
135 break; | |
136 } | |
137 | |
138 if (step != null && step.length() > 0) | |
139 { | |
140 strBuf.append('/' + step); | |
141 } | |
142 | |
143 if (hasMatchingSiblings) | |
144 { | |
145 strBuf.append("[" + position + "]"); | |
146 } | |
147 } | |
148 | |
149 return strBuf.toString(); | |
150 } | |
151 | |
152 private static Vector getVectorPathFromNode(Node node) | |
153 { | |
154 Vector path = new Vector(); | |
155 | |
156 while (node != null) | |
157 { | |
158 path.insertElementAt(node, 0); | |
159 node = node.getParentNode(); | |
160 } | |
161 | |
162 return path; | |
163 } | |
164 | |
165 /** | |
166 * Generates an XPath expression that will return only the given node as its | |
167 * result. This method only works for element, text, document and PI nodes. | |
168 * | |
169 * @param node the node to generate an XPath expression for. This node must | |
170 * be an element node, a text node, a document node, or a processing | |
171 * instruction node. | |
172 * @return an XPath expression that will return only the given node as its | |
173 * result. | |
174 * @exception IllegalArgumentException if the given node is not an element, | |
175 * text, document or PI node. | |
176 */ | |
177 public static String getXPathExprFromNode(Node node) | |
178 throws IllegalArgumentException | |
179 { | |
180 short nodeType = getNodeType(node); | |
181 | |
182 switch (nodeType) | |
183 { | |
184 case Node.ELEMENT_NODE : | |
185 case Node.TEXT_NODE : | |
186 case Node.PROCESSING_INSTRUCTION_NODE : | |
187 return getXPathFromVector(getVectorPathFromNode(node)); | |
188 | |
189 case Node.DOCUMENT_NODE : | |
190 return "/"; | |
191 | |
192 default : | |
193 throw new IllegalArgumentException("Only works for element, text, " + | |
194 "document, and PI nodes."); | |
195 } | |
196 } | |
197 } |