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