|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| package net.sourceforge.pmd.jaxen; |
|
5 |
| |
|
6 |
| import net.sourceforge.pmd.ast.CompilationUnit; |
|
7 |
| import net.sourceforge.pmd.ast.Node; |
|
8 |
| import org.jaxen.DefaultNavigator; |
|
9 |
| import org.jaxen.XPath; |
|
10 |
| import org.jaxen.util.SingleObjectIterator; |
|
11 |
| |
|
12 |
| import java.util.ArrayList; |
|
13 |
| import java.util.Iterator; |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| public class DocumentNavigator extends DefaultNavigator { |
|
19 |
| |
|
20 |
| private final static Iterator EMPTY_ITERATOR = new ArrayList().iterator(); |
|
21 |
| |
|
22 |
9799
| public String getAttributeName(Object arg0) {
|
|
23 |
9799
| return ((Attribute) arg0).getName();
|
|
24 |
| } |
|
25 |
| |
|
26 |
9799
| public String getAttributeNamespaceUri(Object arg0) {
|
|
27 |
9799
| return "";
|
|
28 |
| } |
|
29 |
| |
|
30 |
0
| public String getAttributeQName(Object arg0) {
|
|
31 |
0
| return ((Attribute) arg0).getName();
|
|
32 |
| } |
|
33 |
| |
|
34 |
896
| public String getAttributeStringValue(Object arg0) {
|
|
35 |
896
| return ((Attribute) arg0).getValue();
|
|
36 |
| } |
|
37 |
| |
|
38 |
0
| public String getCommentStringValue(Object arg0) {
|
|
39 |
0
| return "";
|
|
40 |
| } |
|
41 |
| |
|
42 |
8918
| public String getElementName(Object node) {
|
|
43 |
8918
| return node.toString();
|
|
44 |
| } |
|
45 |
| |
|
46 |
8918
| public String getElementNamespaceUri(Object arg0) {
|
|
47 |
8918
| return "";
|
|
48 |
| } |
|
49 |
| |
|
50 |
0
| public String getElementQName(Object arg0) {
|
|
51 |
0
| return getElementName(arg0);
|
|
52 |
| } |
|
53 |
| |
|
54 |
0
| public String getElementStringValue(Object arg0) {
|
|
55 |
0
| return "";
|
|
56 |
| } |
|
57 |
| |
|
58 |
0
| public String getNamespacePrefix(Object arg0) {
|
|
59 |
0
| return "";
|
|
60 |
| } |
|
61 |
| |
|
62 |
0
| public String getNamespaceStringValue(Object arg0) {
|
|
63 |
0
| return "";
|
|
64 |
| } |
|
65 |
| |
|
66 |
0
| public String getTextStringValue(Object arg0) {
|
|
67 |
0
| return "";
|
|
68 |
| } |
|
69 |
| |
|
70 |
11915
| public boolean isAttribute(Object arg0) {
|
|
71 |
11915
| return arg0 instanceof Attribute;
|
|
72 |
| } |
|
73 |
| |
|
74 |
926
| public boolean isComment(Object arg0) {
|
|
75 |
926
| return false;
|
|
76 |
| } |
|
77 |
| |
|
78 |
1285
| public boolean isDocument(Object arg0) {
|
|
79 |
1285
| return arg0 instanceof CompilationUnit;
|
|
80 |
| } |
|
81 |
| |
|
82 |
20548
| public boolean isElement(Object arg0) {
|
|
83 |
20548
| return arg0 instanceof Node;
|
|
84 |
| } |
|
85 |
| |
|
86 |
969
| public boolean isNamespace(Object arg0) {
|
|
87 |
969
| return false;
|
|
88 |
| } |
|
89 |
| |
|
90 |
926
| public boolean isProcessingInstruction(Object arg0) {
|
|
91 |
926
| return false;
|
|
92 |
| } |
|
93 |
| |
|
94 |
12553
| public boolean isText(Object arg0) {
|
|
95 |
12553
| return false;
|
|
96 |
| } |
|
97 |
| |
|
98 |
0
| public XPath parseXPath(String arg0) {
|
|
99 |
0
| return null;
|
|
100 |
| } |
|
101 |
| |
|
102 |
2353
| public Object getParentNode(Object arg0) {
|
|
103 |
2353
| if (arg0 instanceof Node) {
|
|
104 |
2349
| return ((Node) arg0).jjtGetParent();
|
|
105 |
| } |
|
106 |
4
| return ((Attribute) arg0).getParent();
|
|
107 |
| } |
|
108 |
| |
|
109 |
919
| public Iterator getAttributeAxisIterator(Object arg0) {
|
|
110 |
919
| return new AttributeAxisIterator((Node) arg0);
|
|
111 |
| } |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
12306
| public Iterator getChildAxisIterator(Object contextNode) {
|
|
120 |
12306
| return new NodeIterator((Node) contextNode) {
|
|
121 |
12306
| protected Node getFirstNode(Node node) {
|
|
122 |
12306
| return getFirstChild(node);
|
|
123 |
| } |
|
124 |
| |
|
125 |
12643
| protected Node getNextNode(Node node) {
|
|
126 |
12643
| return getNextSibling(node);
|
|
127 |
| } |
|
128 |
| }; |
|
129 |
| } |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
238
| public Iterator getParentAxisIterator(Object contextNode) {
|
|
138 |
238
| if (isAttribute(contextNode)) {
|
|
139 |
0
| return new SingleObjectIterator(((Attribute) contextNode).getParent());
|
|
140 |
| } |
|
141 |
238
| Node parent = ((Node) contextNode).jjtGetParent();
|
|
142 |
238
| if (parent != null) {
|
|
143 |
237
| return new SingleObjectIterator(parent);
|
|
144 |
| } else { |
|
145 |
1
| return EMPTY_ITERATOR;
|
|
146 |
| } |
|
147 |
| } |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
59
| public Iterator getFollowingSiblingAxisIterator(Object contextNode) {
|
|
156 |
59
| return new NodeIterator((Node) contextNode) {
|
|
157 |
59
| protected Node getFirstNode(Node node) {
|
|
158 |
59
| return getNextNode(node);
|
|
159 |
| } |
|
160 |
| |
|
161 |
114
| protected Node getNextNode(Node node) {
|
|
162 |
114
| return getNextSibling(node);
|
|
163 |
| } |
|
164 |
| }; |
|
165 |
| } |
|
166 |
| |
|
167 |
| |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
5
| public Iterator getPrecedingSiblingAxisIterator(Object contextNode) {
|
|
174 |
5
| return new NodeIterator((Node) contextNode) {
|
|
175 |
5
| protected Node getFirstNode(Node node) {
|
|
176 |
5
| return getNextNode(node);
|
|
177 |
| } |
|
178 |
| |
|
179 |
9
| protected Node getNextNode(Node node) {
|
|
180 |
9
| return getPreviousSibling(node);
|
|
181 |
| } |
|
182 |
| }; |
|
183 |
| } |
|
184 |
| |
|
185 |
| |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
| |
|
190 |
| |
|
191 |
0
| public Iterator getFollowingAxisIterator(Object contextNode) {
|
|
192 |
0
| return new NodeIterator((Node) contextNode) {
|
|
193 |
0
| protected Node getFirstNode(Node node) {
|
|
194 |
0
| if (node == null)
|
|
195 |
0
| return null;
|
|
196 |
| else { |
|
197 |
0
| Node sibling = getNextSibling(node);
|
|
198 |
0
| if (sibling == null)
|
|
199 |
0
| return getFirstNode(node.jjtGetParent());
|
|
200 |
| else |
|
201 |
0
| return sibling;
|
|
202 |
| } |
|
203 |
| } |
|
204 |
| |
|
205 |
0
| protected Node getNextNode(Node node) {
|
|
206 |
0
| if (node == null)
|
|
207 |
0
| return null;
|
|
208 |
| else { |
|
209 |
0
| Node n = getFirstChild(node);
|
|
210 |
0
| if (n == null)
|
|
211 |
0
| n = getNextSibling(node);
|
|
212 |
0
| if (n == null)
|
|
213 |
0
| return getFirstNode(node.jjtGetParent());
|
|
214 |
| else |
|
215 |
0
| return n;
|
|
216 |
| } |
|
217 |
| } |
|
218 |
| }; |
|
219 |
| } |
|
220 |
| |
|
221 |
| |
|
222 |
| |
|
223 |
| |
|
224 |
| |
|
225 |
| |
|
226 |
| |
|
227 |
3
| public Iterator getPrecedingAxisIterator(Object contextNode) {
|
|
228 |
3
| return new NodeIterator((Node) contextNode) {
|
|
229 |
14
| protected Node getFirstNode(Node node) {
|
|
230 |
14
| if (node == null)
|
|
231 |
3
| return null;
|
|
232 |
| else { |
|
233 |
11
| Node sibling = getPreviousSibling(node);
|
|
234 |
11
| if (sibling == null)
|
|
235 |
9
| return getFirstNode(node.jjtGetParent());
|
|
236 |
| else |
|
237 |
2
| return sibling;
|
|
238 |
| } |
|
239 |
| } |
|
240 |
| |
|
241 |
4
| protected Node getNextNode(Node node) {
|
|
242 |
4
| if (node == null)
|
|
243 |
0
| return null;
|
|
244 |
| else { |
|
245 |
4
| Node n = getLastChild(node);
|
|
246 |
4
| if (n == null)
|
|
247 |
2
| n = getPreviousSibling(node);
|
|
248 |
4
| if (n == null)
|
|
249 |
2
| return getFirstNode(node.jjtGetParent());
|
|
250 |
| else |
|
251 |
2
| return n;
|
|
252 |
| } |
|
253 |
| } |
|
254 |
| }; |
|
255 |
| } |
|
256 |
| |
|
257 |
359
| public Object getDocumentNode(Object contextNode) {
|
|
258 |
359
| if (isDocument(contextNode)) {
|
|
259 |
58
| return contextNode;
|
|
260 |
| } |
|
261 |
301
| return getDocumentNode(getParentNode(contextNode));
|
|
262 |
| } |
|
263 |
| } |