1 /***
2 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3 */
4 package net.sourceforge.pmd.jaxen;
5
6 import net.sourceforge.pmd.ast.ASTCompilationUnit;
7 import net.sourceforge.pmd.ast.Node;
8 import org.jaxen.DefaultNavigator;
9 import org.jaxen.XPath;
10 import org.jaxen.util.SingleObjectIterator;
11 import org.saxpath.SAXPathException;
12
13 import java.util.ArrayList;
14 import java.util.Iterator;
15
16 /***
17 * @author daniels
18 *
19 * To change this generated comment go to
20 * Window>Preferences>Java>Code Generation>Code Template
21 */
22 public class DocumentNavigator extends DefaultNavigator {
23
24 /***
25 * Constant: empty iterator.
26 */
27 private final static Iterator EMPTY_ITERATOR = new ArrayList().iterator();
28
29
30
31
32 public String getAttributeName(Object arg0) {
33
34 return ((Attribute) arg0).getName();
35 }
36
37
38
39
40 public String getAttributeNamespaceUri(Object arg0) {
41
42 return null;
43 }
44
45
46
47
48 public String getAttributeQName(Object arg0) {
49
50 return ((Attribute) arg0).getName();
51 }
52
53
54
55
56 public String getAttributeStringValue(Object arg0) {
57
58 return ((Attribute) arg0).getValue();
59 }
60
61
62
63
64 public String getCommentStringValue(Object arg0) {
65
66 return null;
67 }
68
69
70
71
72 public String getElementName(Object node) {
73 return node.toString();
74 }
75
76
77
78
79 public String getElementNamespaceUri(Object arg0) {
80
81 return null;
82 }
83
84
85
86
87 public String getElementQName(Object arg0) {
88 return getElementName(arg0);
89 }
90
91
92
93
94 public String getElementStringValue(Object arg0) {
95
96 return null;
97 }
98
99
100
101
102 public String getNamespacePrefix(Object arg0) {
103
104 return null;
105 }
106
107
108
109
110 public String getNamespaceStringValue(Object arg0) {
111
112 return null;
113 }
114
115
116
117
118 public String getTextStringValue(Object arg0) {
119
120 return null;
121 }
122
123
124
125
126 public boolean isAttribute(Object arg0) {
127
128 return arg0 instanceof Attribute;
129 }
130
131
132
133
134 public boolean isComment(Object arg0) {
135
136 return false;
137 }
138
139
140
141
142 public boolean isDocument(Object arg0) {
143
144 return arg0 instanceof ASTCompilationUnit;
145 }
146
147
148
149
150 public boolean isElement(Object arg0) {
151
152 return arg0 instanceof Node;
153 }
154
155
156
157
158 public boolean isNamespace(Object arg0) {
159
160 return false;
161 }
162
163
164
165
166 public boolean isProcessingInstruction(Object arg0) {
167
168 return false;
169 }
170
171
172
173
174 public boolean isText(Object arg0) {
175
176 return false;
177 }
178
179
180
181
182 public XPath parseXPath(String arg0) throws SAXPathException {
183
184 return null;
185 }
186
187
188
189
190 public Object getParentNode(Object arg0) {
191 if (arg0 instanceof Node) {
192 return ((Node) arg0).jjtGetParent();
193 } else {
194 return ((Attribute) arg0).getParent();
195 }
196 }
197
198
199
200
201 public Iterator getAttributeAxisIterator(Object arg0) {
202 Node contextNode = (Node) arg0;
203 return new AttributeAxisIterator(contextNode);
204 }
205
206 /***
207 * Get an iterator over all of this node's children.
208 *
209 * @param contextNode The context node for the child axis.
210 * @return A possibly-empty iterator (not null).
211 */
212 public Iterator getChildAxisIterator(Object contextNode) {
213 return new NodeIterator((Node) contextNode) {
214 protected Node getFirstNode(Node node) {
215 return getFirstChild(node);
216 }
217
218 protected Node getNextNode(Node node) {
219 return getNextSibling(node);
220 }
221 };
222 }
223
224 /***
225 * Get a (single-member) iterator over this node's parent.
226 *
227 * @param contextNode the context node for the parent axis.
228 * @return A possibly-empty iterator (not null).
229 */
230 public Iterator getParentAxisIterator(Object contextNode) {
231 if (isAttribute(contextNode)) {
232 return new SingleObjectIterator(((Attribute) contextNode).getParent());
233 } else {
234 Node parent = ((Node) contextNode).jjtGetParent();
235 if (parent != null) {
236 return new SingleObjectIterator(parent);
237 } else {
238 return EMPTY_ITERATOR;
239 }
240 }
241 }
242
243 /***
244 * Get an iterator over all following siblings.
245 *
246 * @param contextNode the context node for the sibling iterator.
247 * @return A possibly-empty iterator (not null).
248 */
249 public Iterator getFollowingSiblingAxisIterator(Object contextNode) {
250 return new NodeIterator((Node) contextNode) {
251 protected Node getFirstNode(Node node) {
252 return getNextNode(node);
253 }
254
255 protected Node getNextNode(Node node) {
256 return getNextSibling(node);
257 }
258 };
259 }
260
261 /***
262 * Get an iterator over all preceding siblings.
263 *
264 * @param contextNode The context node for the preceding sibling axis.
265 * @return A possibly-empty iterator (not null).
266 */
267 public Iterator getPrecedingSiblingAxisIterator(Object contextNode) {
268 return new NodeIterator((Node) contextNode) {
269 protected Node getFirstNode(Node node) {
270 return getNextNode(node);
271 }
272
273 protected Node getNextNode(Node node) {
274 return getPreviousSibling(node);
275 }
276 };
277 }
278
279 /***
280 * Get an iterator over all following nodes, depth-first.
281 *
282 * @param contextNode The context node for the following axis.
283 * @return A possibly-empty iterator (not null).
284 */
285 public Iterator getFollowingAxisIterator(Object contextNode) {
286 return new NodeIterator((Node) contextNode) {
287 protected Node getFirstNode(Node node) {
288 if (node == null)
289 return null;
290 else {
291 Node sibling = getNextSibling(node);
292 if (sibling == null)
293 return getFirstNode(node.jjtGetParent());
294 else
295 return sibling;
296 }
297 }
298
299 protected Node getNextNode(Node node) {
300 if (node == null)
301 return null;
302 else {
303 Node n = getFirstChild(node);
304 if (n == null)
305 n = getNextSibling(node);
306 if (n == null)
307 return getFirstNode(node.jjtGetParent());
308 else
309 return n;
310 }
311 }
312 };
313 }
314
315 /***
316 * Get an iterator over all preceding nodes, depth-first.
317 *
318 * @param contextNode The context node for the preceding axis.
319 * @return A possibly-empty iterator (not null).
320 */
321 public Iterator getPrecedingAxisIterator(Object contextNode) {
322 return new NodeIterator((Node) contextNode) {
323 protected Node getFirstNode(Node node) {
324 if (node == null)
325 return null;
326 else {
327 Node sibling = getPreviousSibling(node);
328 if (sibling == null)
329 return getFirstNode(node.jjtGetParent());
330 else
331 return sibling;
332 }
333 }
334
335 protected Node getNextNode(Node node) {
336 if (node == null)
337 return null;
338 else {
339 Node n = getLastChild(node);
340 if (n == null)
341 n = getPreviousSibling(node);
342 if (n == null)
343 return getFirstNode(node.jjtGetParent());
344 else
345 return n;
346 }
347 }
348 };
349 }
350
351
352
353
354 public Object getDocumentNode(Object contextNode) {
355 if (isDocument(contextNode)) {
356 return contextNode;
357 } else {
358 return getDocumentNode(getParentNode(contextNode));
359 }
360 }
361
362 }