1 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 18 public class DocumentNavigator extends DefaultNavigator { 19 20 private final static Iterator EMPTY_ITERATOR = new ArrayList ().iterator(); 21 22 public String getAttributeName(Object arg0) { 23 return ((Attribute) arg0).getName(); 24 } 25 26 public String getAttributeNamespaceUri(Object arg0) { 27 return ""; 28 } 29 30 public String getAttributeQName(Object arg0) { 31 return ((Attribute) arg0).getName(); 32 } 33 34 public String getAttributeStringValue(Object arg0) { 35 return ((Attribute) arg0).getValue(); 36 } 37 38 public String getCommentStringValue(Object arg0) { 39 return ""; 40 } 41 42 public String getElementName(Object node) { 43 return node.toString(); 44 } 45 46 public String getElementNamespaceUri(Object arg0) { 47 return ""; 48 } 49 50 public String getElementQName(Object arg0) { 51 return getElementName(arg0); 52 } 53 54 public String getElementStringValue(Object arg0) { 55 return ""; 56 } 57 58 public String getNamespacePrefix(Object arg0) { 59 return ""; 60 } 61 62 public String getNamespaceStringValue(Object arg0) { 63 return ""; 64 } 65 66 public String getTextStringValue(Object arg0) { 67 return ""; 68 } 69 70 public boolean isAttribute(Object arg0) { 71 return arg0 instanceof Attribute; 72 } 73 74 public boolean isComment(Object arg0) { 75 return false; 76 } 77 78 public boolean isDocument(Object arg0) { 79 return arg0 instanceof CompilationUnit; 80 } 81 82 public boolean isElement(Object arg0) { 83 return arg0 instanceof Node; 84 } 85 86 public boolean isNamespace(Object arg0) { 87 return false; 88 } 89 90 public boolean isProcessingInstruction(Object arg0) { 91 return false; 92 } 93 94 public boolean isText(Object arg0) { 95 return false; 96 } 97 98 public XPath parseXPath(String arg0) { 99 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 getAttributeAxisIterator(Object arg0) { 110 return new AttributeAxisIterator((Node) arg0); 111 } 112 113 119 public Iterator 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 137 public Iterator getParentAxisIterator(Object contextNode) { 138 if (isAttribute(contextNode)) { 139 return new SingleObjectIterator(((Attribute) contextNode).getParent()); 140 } 141 Node parent = ((Node) contextNode).jjtGetParent(); 142 if (parent != null) { 143 return new SingleObjectIterator(parent); 144 } else { 145 return EMPTY_ITERATOR; 146 } 147 } 148 149 155 public Iterator getFollowingSiblingAxisIterator(Object contextNode) { 156 return new NodeIterator((Node) contextNode) { 157 protected Node getFirstNode(Node node) { 158 return getNextNode(node); 159 } 160 161 protected Node getNextNode(Node node) { 162 return getNextSibling(node); 163 } 164 }; 165 } 166 167 173 public Iterator getPrecedingSiblingAxisIterator(Object contextNode) { 174 return new NodeIterator((Node) contextNode) { 175 protected Node getFirstNode(Node node) { 176 return getNextNode(node); 177 } 178 179 protected Node getNextNode(Node node) { 180 return getPreviousSibling(node); 181 } 182 }; 183 } 184 185 191 public Iterator getFollowingAxisIterator(Object contextNode) { 192 return new NodeIterator((Node) contextNode) { 193 protected Node getFirstNode(Node node) { 194 if (node == null) 195 return null; 196 else { 197 Node sibling = getNextSibling(node); 198 if (sibling == null) 199 return getFirstNode(node.jjtGetParent()); 200 else 201 return sibling; 202 } 203 } 204 205 protected Node getNextNode(Node node) { 206 if (node == null) 207 return null; 208 else { 209 Node n = getFirstChild(node); 210 if (n == null) 211 n = getNextSibling(node); 212 if (n == null) 213 return getFirstNode(node.jjtGetParent()); 214 else 215 return n; 216 } 217 } 218 }; 219 } 220 221 227 public Iterator getPrecedingAxisIterator(Object contextNode) { 228 return new NodeIterator((Node) contextNode) { 229 protected Node getFirstNode(Node node) { 230 if (node == null) 231 return null; 232 else { 233 Node sibling = getPreviousSibling(node); 234 if (sibling == null) 235 return getFirstNode(node.jjtGetParent()); 236 else 237 return sibling; 238 } 239 } 240 241 protected Node getNextNode(Node node) { 242 if (node == null) 243 return null; 244 else { 245 Node n = getLastChild(node); 246 if (n == null) 247 n = getPreviousSibling(node); 248 if (n == null) 249 return getFirstNode(node.jjtGetParent()); 250 else 251 return n; 252 } 253 } 254 }; 255 } 256 257 public Object getDocumentNode(Object contextNode) { 258 if (isDocument(contextNode)) { 259 return contextNode; 260 } 261 return getDocumentNode(getParentNode(contextNode)); 262 } 263 } 264 | Popular Tags |