1 package net.sf.saxon.pattern; 2 import net.sf.saxon.om.Axis; 3 import net.sf.saxon.om.AxisIterator; 4 import net.sf.saxon.om.NodeInfo; 5 import net.sf.saxon.om.NamePool; 6 import net.sf.saxon.type.Type; 7 import net.sf.saxon.tinytree.TinyTree; 8 9 12 13 17 public class DocumentNodeTest extends NodeTest { 18 19 20 private NodeTest elementTest; 21 22 public DocumentNodeTest(NodeTest elementTest) { 23 this.elementTest = elementTest; 24 } 25 26 31 32 public boolean matches(int nodeKind, int fingerprint, int annotation) { 33 throw new UnsupportedOperationException ("DocumentNodeTest doesn't support this method"); 34 } 35 36 47 48 public boolean matches(TinyTree tree, int nodeNr) { 49 if (tree.getNodeKind(nodeNr) != Type.DOCUMENT) { 50 return false; 51 } 52 return matches(tree.getNode(nodeNr)); 53 } 54 55 61 62 public boolean matches(NodeInfo node) { 63 if (node.getNodeKind() != Type.DOCUMENT) { 64 return false; 65 } 66 AxisIterator iter = node.iterateAxis(Axis.CHILD); 67 boolean found = false; 70 while (true) { 71 NodeInfo n = (NodeInfo)iter.next(); 72 if (n==null) { 73 return found; 74 } 75 int kind = n.getNodeKind(); 76 if (kind==Type.TEXT) { 77 return false; 78 } else if (kind==Type.ELEMENT) { 79 if (found) { 80 return false; 81 } 82 if (elementTest.matchesItem(n)) { 83 found = true; 84 } else { 85 return false; 86 } 87 } 88 } 89 } 90 91 94 95 public final double getDefaultPriority() { 96 return elementTest.getDefaultPriority(); 97 } 98 99 103 104 public int getPrimitiveType() { 105 return Type.DOCUMENT; 106 } 107 108 112 113 public int getNodeKindMask() { 114 return 1<<Type.DOCUMENT; 115 } 116 117 121 122 public NodeTest getElementTest() { 123 return elementTest; 124 } 125 126 public String toString(NamePool pool) { 127 return "document-node(" + elementTest.toString(pool) + ')'; 128 } 129 130 public String toString() { 131 return "document-node(" + elementTest.toString() + ')'; 132 } 133 134 137 138 public int hashCode() { 139 return elementTest.hashCode()^12345; 140 } 141 142 } 143 144 | Popular Tags |