001: /**
002: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
003: */package net.sourceforge.pmd.jaxen;
004:
005: import net.sourceforge.pmd.ast.CompilationUnit;
006: import net.sourceforge.pmd.ast.Node;
007: import org.jaxen.DefaultNavigator;
008: import org.jaxen.XPath;
009: import org.jaxen.util.SingleObjectIterator;
010:
011: import java.util.ArrayList;
012: import java.util.Iterator;
013:
014: /**
015: * @author daniels
016: */
017: public class DocumentNavigator extends DefaultNavigator {
018:
019: private final static Iterator EMPTY_ITERATOR = new ArrayList()
020: .iterator();
021:
022: public String getAttributeName(Object arg0) {
023: return ((Attribute) arg0).getName();
024: }
025:
026: public String getAttributeNamespaceUri(Object arg0) {
027: return "";
028: }
029:
030: public String getAttributeQName(Object arg0) {
031: return ((Attribute) arg0).getName();
032: }
033:
034: public String getAttributeStringValue(Object arg0) {
035: return ((Attribute) arg0).getValue();
036: }
037:
038: public String getCommentStringValue(Object arg0) {
039: return "";
040: }
041:
042: public String getElementName(Object node) {
043: return node.toString();
044: }
045:
046: public String getElementNamespaceUri(Object arg0) {
047: return "";
048: }
049:
050: public String getElementQName(Object arg0) {
051: return getElementName(arg0);
052: }
053:
054: public String getElementStringValue(Object arg0) {
055: return "";
056: }
057:
058: public String getNamespacePrefix(Object arg0) {
059: return "";
060: }
061:
062: public String getNamespaceStringValue(Object arg0) {
063: return "";
064: }
065:
066: public String getTextStringValue(Object arg0) {
067: return "";
068: }
069:
070: public boolean isAttribute(Object arg0) {
071: return arg0 instanceof Attribute;
072: }
073:
074: public boolean isComment(Object arg0) {
075: return false;
076: }
077:
078: public boolean isDocument(Object arg0) {
079: return arg0 instanceof CompilationUnit;
080: }
081:
082: public boolean isElement(Object arg0) {
083: return arg0 instanceof Node;
084: }
085:
086: public boolean isNamespace(Object arg0) {
087: return false;
088: }
089:
090: public boolean isProcessingInstruction(Object arg0) {
091: return false;
092: }
093:
094: public boolean isText(Object arg0) {
095: return false;
096: }
097:
098: public XPath parseXPath(String arg0) {
099: return null;
100: }
101:
102: public Object getParentNode(Object arg0) {
103: if (arg0 instanceof Node) {
104: return ((Node) arg0).jjtGetParent();
105: }
106: return ((Attribute) arg0).getParent();
107: }
108:
109: public Iterator<Attribute> getAttributeAxisIterator(Object arg0) {
110: return new AttributeAxisIterator((Node) arg0);
111: }
112:
113: /**
114: * Get an iterator over all of this node's children.
115: *
116: * @param contextNode The context node for the child axis.
117: * @return A possibly-empty iterator (not null).
118: */
119: public Iterator<Node> getChildAxisIterator(Object contextNode) {
120: return new NodeIterator((Node) contextNode) {
121: protected Node getFirstNode(Node node) {
122: return getFirstChild(node);
123: }
124:
125: protected Node getNextNode(Node node) {
126: return getNextSibling(node);
127: }
128: };
129: }
130:
131: /**
132: * Get a (single-member) iterator over this node's parent.
133: *
134: * @param contextNode the context node for the parent axis.
135: * @return A possibly-empty iterator (not null).
136: */
137: public Iterator getParentAxisIterator(Object contextNode) {
138: if (isAttribute(contextNode)) {
139: return new SingleObjectIterator(((Attribute) contextNode)
140: .getParent());
141: }
142: Node parent = ((Node) contextNode).jjtGetParent();
143: if (parent != null) {
144: return new SingleObjectIterator(parent);
145: } else {
146: return EMPTY_ITERATOR;
147: }
148: }
149:
150: /**
151: * Get an iterator over all following siblings.
152: *
153: * @param contextNode the context node for the sibling iterator.
154: * @return A possibly-empty iterator (not null).
155: */
156: public Iterator<Node> getFollowingSiblingAxisIterator(
157: Object contextNode) {
158: return new NodeIterator((Node) contextNode) {
159: protected Node getFirstNode(Node node) {
160: return getNextNode(node);
161: }
162:
163: protected Node getNextNode(Node node) {
164: return getNextSibling(node);
165: }
166: };
167: }
168:
169: /**
170: * Get an iterator over all preceding siblings.
171: *
172: * @param contextNode The context node for the preceding sibling axis.
173: * @return A possibly-empty iterator (not null).
174: */
175: public Iterator<Node> getPrecedingSiblingAxisIterator(
176: Object contextNode) {
177: return new NodeIterator((Node) contextNode) {
178: protected Node getFirstNode(Node node) {
179: return getNextNode(node);
180: }
181:
182: protected Node getNextNode(Node node) {
183: return getPreviousSibling(node);
184: }
185: };
186: }
187:
188: /**
189: * Get an iterator over all following nodes, depth-first.
190: *
191: * @param contextNode The context node for the following axis.
192: * @return A possibly-empty iterator (not null).
193: */
194: public Iterator<Node> getFollowingAxisIterator(Object contextNode) {
195: return new NodeIterator((Node) contextNode) {
196: protected Node getFirstNode(Node node) {
197: if (node == null)
198: return null;
199: else {
200: Node sibling = getNextSibling(node);
201: if (sibling == null)
202: return getFirstNode(node.jjtGetParent());
203: else
204: return sibling;
205: }
206: }
207:
208: protected Node getNextNode(Node node) {
209: if (node == null)
210: return null;
211: else {
212: Node n = getFirstChild(node);
213: if (n == null)
214: n = getNextSibling(node);
215: if (n == null)
216: return getFirstNode(node.jjtGetParent());
217: else
218: return n;
219: }
220: }
221: };
222: }
223:
224: /**
225: * Get an iterator over all preceding nodes, depth-first.
226: *
227: * @param contextNode The context node for the preceding axis.
228: * @return A possibly-empty iterator (not null).
229: */
230: public Iterator<Node> getPrecedingAxisIterator(Object contextNode) {
231: return new NodeIterator((Node) contextNode) {
232: protected Node getFirstNode(Node node) {
233: if (node == null)
234: return null;
235: else {
236: Node sibling = getPreviousSibling(node);
237: if (sibling == null)
238: return getFirstNode(node.jjtGetParent());
239: else
240: return sibling;
241: }
242: }
243:
244: protected Node getNextNode(Node node) {
245: if (node == null)
246: return null;
247: else {
248: Node n = getLastChild(node);
249: if (n == null)
250: n = getPreviousSibling(node);
251: if (n == null)
252: return getFirstNode(node.jjtGetParent());
253: else
254: return n;
255: }
256: }
257: };
258: }
259:
260: public Object getDocumentNode(Object contextNode) {
261: if (isDocument(contextNode)) {
262: return contextNode;
263: }
264: return getDocumentNode(getParentNode(contextNode));
265: }
266: }
|