1 package net.sf.saxon.pattern; 2 import net.sf.saxon.om.FingerprintedNode; 3 import net.sf.saxon.om.NamePool; 4 import net.sf.saxon.om.NodeInfo; 5 import net.sf.saxon.type.Type; 6 import net.sf.saxon.type.ItemType; 7 import net.sf.saxon.tinytree.TinyTree; 8 9 import java.util.HashSet ; 10 import java.util.Set ; 11 12 19 20 public class NameTest extends NodeTest { 21 22 private int nodeKind; 23 private int fingerprint; 24 private NamePool namePool; 25 private String uri = null; 26 private String localName = null; 27 28 public NameTest(int nodeType, int nameCode, NamePool namePool) { 29 this.nodeKind = nodeType; 30 this.fingerprint = nameCode & 0xfffff; 31 this.namePool = namePool; 32 } 33 34 37 38 public NameTest(NodeInfo node) { 39 this.nodeKind = node.getNodeKind(); 40 this.fingerprint = node.getFingerprint(); 41 this.namePool = node.getNamePool(); 42 } 43 44 49 50 public boolean matches(int nodeKind, int nameCode, int annotation) { 51 return ((nameCode&0xfffff) == this.fingerprint && nodeKind == this.nodeKind); 54 } 56 57 67 68 public boolean matches(TinyTree tree, int nodeNr) { 69 return ((tree.getNameCode(nodeNr)&0xfffff) == this.fingerprint && tree.getNodeKind(nodeNr) == this.nodeKind); 70 } 71 72 78 79 public boolean matches(NodeInfo node) { 80 if (node.getNodeKind() != nodeKind) { 81 return false; 82 } 83 84 89 if (node instanceof FingerprintedNode) { 90 return node.getFingerprint() == fingerprint; 91 } else { 92 if (uri == null) { 93 uri = namePool.getURI(fingerprint); 94 } 95 if (localName == null) { 96 localName = namePool.getLocalName(fingerprint); 97 } 98 return localName.equals(node.getLocalPart()) && uri.equals(node.getURI()); 99 } 100 } 101 102 105 106 public final double getDefaultPriority() { 107 return 0.0; 108 } 109 110 113 114 public int getFingerprint() { 115 return fingerprint; 116 } 117 118 123 124 public int getPrimitiveType() { 125 return nodeKind; 126 } 127 128 140 141 public ItemType getSuperType() { 142 return NodeKindTest.makeNodeKindTest(nodeKind); 143 } 144 145 149 150 public int getNodeKindMask() { 151 return 1<<nodeKind; 152 } 153 154 159 160 public Set getRequiredNodeNames() { 161 HashSet s = new HashSet (1); 162 s.add(new Integer (fingerprint)); 163 return s; 164 } 165 166 public String toString() { 167 return toString(namePool); 168 } 169 170 public String toString(NamePool pool) { 171 switch (nodeKind) { 172 case Type.ELEMENT: 173 return "element(" + pool.getDisplayName(fingerprint) + ')'; 174 case Type.ATTRIBUTE: 175 return "attribute(" + pool.getDisplayName(fingerprint) + ')'; 176 case Type.PROCESSING_INSTRUCTION: 177 return "processing-instruction(" + pool.getDisplayName(fingerprint) + ')'; 178 case Type.NAMESPACE: 179 return "namespace(" + pool.getDisplayName(fingerprint) + ')'; 180 } 181 return pool.getDisplayName(fingerprint); 182 } 183 184 187 188 public int hashCode() { 189 return nodeKind<<20 ^ fingerprint; 190 } 191 192 193 } 194 195 | Popular Tags |