View Javadoc

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      /* (non-Javadoc)
30       * @see org.jaxen.Navigator#getAttributeName(java.lang.Object)
31       */
32      public String getAttributeName(Object arg0) {
33          // TODO Auto-generated method stub
34          return ((Attribute) arg0).getName();
35      }
36  
37      /* (non-Javadoc)
38       * @see org.jaxen.Navigator#getAttributeNamespaceUri(java.lang.Object)
39       */
40      public String getAttributeNamespaceUri(Object arg0) {
41          // TODO Auto-generated method stub
42          return null;
43      }
44  
45      /* (non-Javadoc)
46       * @see org.jaxen.Navigator#getAttributeQName(java.lang.Object)
47       */
48      public String getAttributeQName(Object arg0) {
49          // TODO Auto-generated method stub
50          return ((Attribute) arg0).getName();
51      }
52  
53      /* (non-Javadoc)
54       * @see org.jaxen.Navigator#getAttributeStringValue(java.lang.Object)
55       */
56      public String getAttributeStringValue(Object arg0) {
57          // TODO Auto-generated method stub
58          return ((Attribute) arg0).getValue();
59      }
60  
61      /* (non-Javadoc)
62       * @see org.jaxen.Navigator#getCommentStringValue(java.lang.Object)
63       */
64      public String getCommentStringValue(Object arg0) {
65          // TODO Auto-generated method stub
66          return null;
67      }
68  
69      /* (non-Javadoc)
70       * @see org.jaxen.Navigator#getElementName(java.lang.Object)
71       */
72      public String getElementName(Object node) {
73          return node.toString();
74      }
75  
76      /* (non-Javadoc)
77       * @see org.jaxen.Navigator#getElementNamespaceUri(java.lang.Object)
78       */
79      public String getElementNamespaceUri(Object arg0) {
80          // TODO Auto-generated method stub
81          return null;
82      }
83  
84      /* (non-Javadoc)
85       * @see org.jaxen.Navigator#getElementQName(java.lang.Object)
86       */
87      public String getElementQName(Object arg0) {
88          return getElementName(arg0);
89      }
90  
91      /* (non-Javadoc)
92       * @see org.jaxen.Navigator#getElementStringValue(java.lang.Object)
93       */
94      public String getElementStringValue(Object arg0) {
95          // TODO Auto-generated method stub
96          return null;
97      }
98  
99      /* (non-Javadoc)
100      * @see org.jaxen.Navigator#getNamespacePrefix(java.lang.Object)
101      */
102     public String getNamespacePrefix(Object arg0) {
103         // TODO Auto-generated method stub
104         return null;
105     }
106 
107     /* (non-Javadoc)
108      * @see org.jaxen.Navigator#getNamespaceStringValue(java.lang.Object)
109      */
110     public String getNamespaceStringValue(Object arg0) {
111         // TODO Auto-generated method stub
112         return null;
113     }
114 
115     /* (non-Javadoc)
116      * @see org.jaxen.Navigator#getTextStringValue(java.lang.Object)
117      */
118     public String getTextStringValue(Object arg0) {
119         // TODO Auto-generated method stub
120         return null;
121     }
122 
123     /* (non-Javadoc)
124      * @see org.jaxen.Navigator#isAttribute(java.lang.Object)
125      */
126     public boolean isAttribute(Object arg0) {
127         // TODO Auto-generated method stub
128         return arg0 instanceof Attribute;
129     }
130 
131     /* (non-Javadoc)
132      * @see org.jaxen.Navigator#isComment(java.lang.Object)
133      */
134     public boolean isComment(Object arg0) {
135         // TODO Auto-generated method stub
136         return false;
137     }
138 
139     /* (non-Javadoc)
140      * @see org.jaxen.Navigator#isDocument(java.lang.Object)
141      */
142     public boolean isDocument(Object arg0) {
143         // TODO Auto-generated method stub
144         return arg0 instanceof ASTCompilationUnit;
145     }
146 
147     /* (non-Javadoc)
148      * @see org.jaxen.Navigator#isElement(java.lang.Object)
149      */
150     public boolean isElement(Object arg0) {
151         // TODO Auto-generated method stub
152         return arg0 instanceof Node;
153     }
154 
155     /* (non-Javadoc)
156      * @see org.jaxen.Navigator#isNamespace(java.lang.Object)
157      */
158     public boolean isNamespace(Object arg0) {
159         // TODO Auto-generated method stub
160         return false;
161     }
162 
163     /* (non-Javadoc)
164      * @see org.jaxen.Navigator#isProcessingInstruction(java.lang.Object)
165      */
166     public boolean isProcessingInstruction(Object arg0) {
167         // TODO Auto-generated method stub
168         return false;
169     }
170 
171     /* (non-Javadoc)
172      * @see org.jaxen.Navigator#isText(java.lang.Object)
173      */
174     public boolean isText(Object arg0) {
175         // TODO Auto-generated method stub
176         return false;
177     }
178 
179     /* (non-Javadoc)
180      * @see org.jaxen.Navigator#parseXPath(java.lang.String)
181      */
182     public XPath parseXPath(String arg0) throws SAXPathException {
183         // TODO Auto-generated method stub
184         return null;
185     }
186 
187     /* (non-Javadoc)
188      * @see org.jaxen.Navigator#getParentNode(java.lang.Object)
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     /* (non-Javadoc)
199      * @see org.jaxen.Navigator#getAttributeAxisIterator(java.lang.Object)
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     /* (non-Javadoc)
352      * @see org.jaxen.Navigator#getDocumentNode(java.lang.Object)
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 }