001: /*
002: * Created on 15/03/2003
003: *
004: * To change this generated comment go to
005: * Window>Preferences>Java>Code Generation>Code Template
006: */
007: package org.acm.seguin.pmd.jaxen;
008:
009: import net.sourceforge.jrefactory.ast.ASTCompilationUnit;
010: import net.sourceforge.jrefactory.ast.Node;
011: import org.jaxen.DefaultNavigator;
012: import org.jaxen.XPath;
013: import org.jaxen.util.SingleObjectIterator;
014: import org.saxpath.SAXPathException;
015:
016: import java.util.ArrayList;
017: import java.util.Iterator;
018:
019: /**
020: * @author daniels
021: *
022: * To change this generated comment go to
023: * Window>Preferences>Java>Code Generation>Code Template
024: */
025: public class DocumentNavigator extends DefaultNavigator {
026:
027: /**
028: * Constant: empty iterator.
029: */
030: private final static Iterator EMPTY_ITERATOR = new ArrayList()
031: .iterator();
032:
033: /* (non-Javadoc)
034: * @see org.jaxen.Navigator#getAttributeName(java.lang.Object)
035: */
036: public String getAttributeName(Object arg0) {
037: // TODO Auto-generated method stub
038: return ((Attribute) arg0).getName();
039: }
040:
041: /* (non-Javadoc)
042: * @see org.jaxen.Navigator#getAttributeNamespaceUri(java.lang.Object)
043: */
044: public String getAttributeNamespaceUri(Object arg0) {
045: // TODO Auto-generated method stub
046: return null;
047: }
048:
049: /* (non-Javadoc)
050: * @see org.jaxen.Navigator#getAttributeQName(java.lang.Object)
051: */
052: public String getAttributeQName(Object arg0) {
053: // TODO Auto-generated method stub
054: return ((Attribute) arg0).getName();
055: }
056:
057: /* (non-Javadoc)
058: * @see org.jaxen.Navigator#getAttributeStringValue(java.lang.Object)
059: */
060: public String getAttributeStringValue(Object arg0) {
061: // TODO Auto-generated method stub
062: return ((Attribute) arg0).getValue();
063: }
064:
065: /* (non-Javadoc)
066: * @see org.jaxen.Navigator#getCommentStringValue(java.lang.Object)
067: */
068: public String getCommentStringValue(Object arg0) {
069: // TODO Auto-generated method stub
070: return null;
071: }
072:
073: /* (non-Javadoc)
074: * @see org.jaxen.Navigator#getElementName(java.lang.Object)
075: */
076: public String getElementName(Object node) {
077: //System.err.println("getElementName("+node+") -> "+node.toString());
078: return node.toString();
079: }
080:
081: /* (non-Javadoc)
082: * @see org.jaxen.Navigator#getElementNamespaceUri(java.lang.Object)
083: */
084: public String getElementNamespaceUri(Object arg0) {
085: // TODO Auto-generated method stub
086: return null;
087: }
088:
089: /* (non-Javadoc)
090: * @see org.jaxen.Navigator#getElementQName(java.lang.Object)
091: */
092: public String getElementQName(Object arg0) {
093: //System.err.println("getElementQName("+arg0+") -> "+getElementName(arg0));
094: return getElementName(arg0);
095: }
096:
097: /* (non-Javadoc)
098: * @see org.jaxen.Navigator#getElementStringValue(java.lang.Object)
099: */
100: public String getElementStringValue(Object arg0) {
101: // TODO Auto-generated method stub
102: return null;
103: }
104:
105: /* (non-Javadoc)
106: * @see org.jaxen.Navigator#getNamespacePrefix(java.lang.Object)
107: */
108: public String getNamespacePrefix(Object arg0) {
109: // TODO Auto-generated method stub
110: return null;
111: }
112:
113: /* (non-Javadoc)
114: * @see org.jaxen.Navigator#getNamespaceStringValue(java.lang.Object)
115: */
116: public String getNamespaceStringValue(Object arg0) {
117: // TODO Auto-generated method stub
118: return null;
119: }
120:
121: /* (non-Javadoc)
122: * @see org.jaxen.Navigator#getTextStringValue(java.lang.Object)
123: */
124: public String getTextStringValue(Object arg0) {
125: // TODO Auto-generated method stub
126: return null;
127: }
128:
129: /* (non-Javadoc)
130: * @see org.jaxen.Navigator#isAttribute(java.lang.Object)
131: */
132: public boolean isAttribute(Object arg0) {
133: // TODO Auto-generated method stub
134: //System.err.println("isAttribute("+arg0+") -> "+(arg0 instanceof Attribute));
135: return arg0 instanceof Attribute;
136: }
137:
138: /* (non-Javadoc)
139: * @see org.jaxen.Navigator#isComment(java.lang.Object)
140: */
141: public boolean isComment(Object arg0) {
142: // TODO Auto-generated method stub
143: return false;
144: }
145:
146: /* (non-Javadoc)
147: * @see org.jaxen.Navigator#isDocument(java.lang.Object)
148: */
149: public boolean isDocument(Object arg0) {
150: // TODO Auto-generated method stub
151: //System.err.println("isDocument("+arg0+") -> "+(arg0 instanceof ASTCompilationUnit));
152: //if (arg0 instanceof ASTCompilationUnit) {
153: // System.err.println(((ASTCompilationUnit)arg0).dumpString("\r\n"));
154: //}
155: return arg0 instanceof ASTCompilationUnit;
156: }
157:
158: /* (non-Javadoc)
159: * @see org.jaxen.Navigator#isElement(java.lang.Object)
160: */
161: public boolean isElement(Object arg0) {
162: // TODO Auto-generated method stub
163: //System.err.println("isElement("+arg0+") -> "+(arg0 instanceof Node));
164: return arg0 instanceof Node;
165: }
166:
167: /* (non-Javadoc)
168: * @see org.jaxen.Navigator#isNamespace(java.lang.Object)
169: */
170: public boolean isNamespace(Object arg0) {
171: // TODO Auto-generated method stub
172: return false;
173: }
174:
175: /* (non-Javadoc)
176: * @see org.jaxen.Navigator#isProcessingInstruction(java.lang.Object)
177: */
178: public boolean isProcessingInstruction(Object arg0) {
179: // TODO Auto-generated method stub
180: return false;
181: }
182:
183: /* (non-Javadoc)
184: * @see org.jaxen.Navigator#isText(java.lang.Object)
185: */
186: public boolean isText(Object arg0) {
187: // TODO Auto-generated method stub
188: return false;
189: }
190:
191: /* (non-Javadoc)
192: * @see org.jaxen.Navigator#parseXPath(java.lang.String)
193: */
194: public XPath parseXPath(String arg0) {
195: // TODO Auto-generated method stub
196: return null;
197: }
198:
199: /* (non-Javadoc)
200: * @see org.jaxen.Navigator#getParentNode(java.lang.Object)
201: */
202: public Object getParentNode(Object arg0) {
203: if (arg0 instanceof Node) {
204: //System.err.println("getParentNode("+arg0+") -> "+(((Node) arg0).jjtGetParent()));
205: return ((Node) arg0).jjtGetParent();
206: } else {
207: //System.err.println("getParentNode("+arg0+") -> "+(((Attribute) arg0).getParent()));
208: return ((Attribute) arg0).getParent();
209: }
210: }
211:
212: /* (non-Javadoc)
213: * @see org.jaxen.Navigator#getAttributeAxisIterator(java.lang.Object)
214: */
215: public Iterator getAttributeAxisIterator(Object arg0) {
216: Node contextNode = (Node) arg0;
217: return new AttributeAxisIterator(contextNode);
218: }
219:
220: /**
221: * Get an iterator over all of this node's children.
222: *
223: * @param contextNode The context node for the child axis.
224: * @return A possibly-empty iterator (not null).
225: */
226: public Iterator getChildAxisIterator(final Object contextNode) {
227: return new NodeIterator((Node) contextNode) {
228: final private Node theParent = (Node) contextNode;
229: private int nextPosition = 0;
230:
231: protected Node getFirstNode(Node node) {
232: //System.err.println("getChildAxisIterator("+node+") children="+node.jjtGetNumChildren());
233: Node fc = getFirstChild(node);
234: //System.err.println("gcai.getFirstChild("+node+") -> "+fc);
235: return fc;
236: }
237:
238: protected Node getNextNode(Node node) {
239: Node ns = getNextSibling(node);
240: //System.err.println("gcai.getNextSibling("+node+") -> "+ns);
241: return ns;
242: }
243:
244: protected Node getNextSibling(Node contextNode) {
245: nextPosition++;
246: if (nextPosition < theParent.jjtGetNumChildren()) {
247: return theParent.jjtGetChild(nextPosition);
248: }
249: return null;
250: }
251:
252: };
253: }
254:
255: /**
256: * Get a (single-member) iterator over this node's parent.
257: *
258: * @param contextNode the context node for the parent axis.
259: * @return A possibly-empty iterator (not null).
260: */
261: public Iterator getParentAxisIterator(Object contextNode) {
262: if (isAttribute(contextNode)) {
263: return new SingleObjectIterator(((Attribute) contextNode)
264: .getParent());
265: } else {
266: Node parent = ((Node) contextNode).jjtGetParent();
267: if (parent != null) {
268: return new SingleObjectIterator(parent);
269: } else {
270: return EMPTY_ITERATOR;
271: }
272: }
273: }
274:
275: /**
276: * Get an iterator over all following siblings.
277: *
278: * @param contextNode the context node for the sibling iterator.
279: * @return A possibly-empty iterator (not null).
280: */
281: public Iterator getFollowingSiblingAxisIterator(Object contextNode) {
282: return new NodeIterator((Node) contextNode) {
283: protected Node getFirstNode(Node node) {
284: Node nn = getNextNode(node);
285: //System.err.println("gfsai.getFirstNode("+node+") -> "+nn);
286: return nn;
287: }
288:
289: protected Node getNextNode(Node node) {
290: Node ns = getNextSibling(node);
291: //System.err.println("gfsai.getNextNode("+node+") -> "+ns);
292: return ns;
293: }
294: };
295: }
296:
297: /**
298: * Get an iterator over all preceding siblings.
299: *
300: * @param contextNode The context node for the preceding sibling axis.
301: * @return A possibly-empty iterator (not null).
302: */
303: public Iterator getPrecedingSiblingAxisIterator(Object contextNode) {
304: return new NodeIterator((Node) contextNode) {
305: protected Node getFirstNode(Node node) {
306: return getNextNode(node);
307: }
308:
309: protected Node getNextNode(Node node) {
310: return getPreviousSibling(node);
311: }
312: };
313: }
314:
315: /**
316: * Get an iterator over all following nodes, depth-first.
317: *
318: * @param contextNode The context node for the following axis.
319: * @return A possibly-empty iterator (not null).
320: */
321: public Iterator getFollowingAxisIterator(Object contextNode) {
322: return new NodeIterator((Node) contextNode) {
323: protected Node getFirstNode(Node node) {
324: if (node == null) {
325: //System.err.println("gfai.getFirstNode("+node+") -> "+null);
326: return null;
327: } else {
328: Node sibling = getNextSibling(node);
329: if (sibling == null) {
330: Node nxt = getFirstNode(node.jjtGetParent());
331: //System.err.println("gfai.getFirstNode("+node+") -> "+nxt);
332: return nxt;
333: } else {
334: //System.err.println("gfai.getFirstNode("+node+") -> "+sibling);
335: return sibling;
336: }
337: }
338: }
339:
340: protected Node getNextNode(Node node) {
341: if (node == null) {
342: //System.err.println("gfai.getNextNode("+node+") -> "+null);
343: return null;
344: } else {
345: Node n = getFirstChild(node);
346: //System.err.print("gfai.getNextNode("+node+") -> "+n);
347: if (n == null) {
348: n = getNextSibling(node);
349: //System.err.print(" -> "+n);
350: }
351: if (n == null) {
352: Node nn = getFirstNode(node.jjtGetParent());
353: //System.err.println(" -> "+nn);
354: return nn;
355: } else {
356: //System.err.println();
357: return n;
358: }
359: }
360: }
361: };
362: }
363:
364: /**
365: * Get an iterator over all preceding nodes, depth-first.
366: *
367: * @param contextNode The context node for the preceding axis.
368: * @return A possibly-empty iterator (not null).
369: */
370: public Iterator getPrecedingAxisIterator(Object contextNode) {
371: return new NodeIterator((Node) contextNode) {
372: protected Node getFirstNode(Node node) {
373: if (node == null)
374: return null;
375: else {
376: Node sibling = getPreviousSibling(node);
377: if (sibling == null)
378: return getFirstNode(node.jjtGetParent());
379: else
380: return sibling;
381: }
382: }
383:
384: protected Node getNextNode(Node node) {
385: if (node == null)
386: return null;
387: else {
388: Node n = getLastChild(node);
389: if (n == null)
390: n = getPreviousSibling(node);
391: if (n == null)
392: return getFirstNode(node.jjtGetParent());
393: else
394: return n;
395: }
396: }
397: };
398: }
399:
400: /* (non-Javadoc)
401: * @see org.jaxen.Navigator#getDocumentNode(java.lang.Object)
402: */
403: public Object getDocumentNode(Object contextNode) {
404: if (isDocument(contextNode)) {
405: return contextNode;
406: } else {
407: return getDocumentNode(getParentNode(contextNode));
408: }
409: }
410:
411: }
|