1 package net.sf.saxon.pattern; 2 import net.sf.saxon.expr.Expression; 3 import net.sf.saxon.expr.StaticContext; 4 import net.sf.saxon.expr.XPathContext; 5 import net.sf.saxon.expr.PromotionOffer; 6 import net.sf.saxon.om.DocumentInfo; 7 import net.sf.saxon.om.Item; 8 import net.sf.saxon.om.NodeInfo; 9 import net.sf.saxon.om.SequenceIterator; 10 import net.sf.saxon.trans.KeyManager; 11 import net.sf.saxon.trans.XPathException; 12 import net.sf.saxon.type.ItemType; 13 import net.sf.saxon.value.AtomicValue; 14 15 import java.util.Iterator ; 16 17 20 21 public final class KeyPattern extends Pattern { 22 23 private int keyfingerprint; private Expression keyexp; 26 31 32 public KeyPattern(int namecode, Expression key) { 33 keyfingerprint = namecode & 0xfffff; 34 keyexp = key; 35 } 36 37 42 43 public Pattern analyze(StaticContext env, ItemType contextItemType) throws XPathException { 44 keyexp = keyexp.typeCheck(env, contextItemType); 45 return this; 46 } 47 48 52 53 public int getDependencies() { 54 return keyexp.getDependencies(); 55 } 56 57 60 61 public Iterator iterateSubExpressions() { 62 return keyexp.iterateSubExpressions(); 63 } 64 65 83 84 public void promote(PromotionOffer offer) throws XPathException { 85 keyexp = keyexp.promote(offer); 86 } 87 88 93 94 public boolean matches(NodeInfo e, XPathContext context) throws XPathException { 95 DocumentInfo doc = e.getDocumentRoot(); 96 if (doc==null) { 97 return false; 98 } 99 KeyManager km = context.getController().getKeyManager(); 100 SequenceIterator iter = keyexp.iterate(context); 101 while (true) { 102 Item it = iter.next(); 103 if (it == null) { 104 return false; 105 } 106 SequenceIterator nodes = km.selectByKey(keyfingerprint, doc, (AtomicValue)it, context); 107 while (true) { 108 NodeInfo n = (NodeInfo)nodes.next(); 109 if (n == null) { 110 break; 111 } 112 if (n.isSameNodeInfo(e)) { 113 return true; 114 } 115 } 116 } 117 } 118 119 122 123 public NodeTest getNodeTest() { 124 return AnyNodeTest.getInstance(); 125 } 126 127 } 128 129 | Popular Tags |