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.NodeInfo; 8 import net.sf.saxon.trans.XPathException; 9 import net.sf.saxon.type.ItemType; 10 import net.sf.saxon.type.Type; 11 import net.sf.saxon.value.AtomicValue; 12 13 import java.util.StringTokenizer ; 14 import java.util.Iterator ; 15 16 19 20 public final class IDPattern extends Pattern { 21 22 private Expression idExpression; 23 24 28 public IDPattern(Expression id) { 29 idExpression = id; 30 } 31 32 38 39 public Pattern analyze(StaticContext env, ItemType contextItemType) throws XPathException { 40 idExpression = idExpression.typeCheck(env, contextItemType); 41 return this; 42 } 43 44 48 49 public int getDependencies() { 50 return idExpression.getDependencies(); 51 } 52 53 56 57 public Iterator iterateSubExpressions() { 58 return idExpression.iterateSubExpressions(); 59 } 60 61 79 80 public void promote(PromotionOffer offer) throws XPathException { 81 idExpression = idExpression.promote(offer); 82 } 83 84 89 90 public boolean matches(NodeInfo e, XPathContext context) throws XPathException { 91 if (e.getNodeKind() != Type.ELEMENT) { 92 return false; 93 } 94 DocumentInfo doc = e.getDocumentRoot(); 95 if (doc==null) { 96 return false; 97 } 98 AtomicValue idValue = (AtomicValue)idExpression.evaluateItem(context); 99 if (idValue == null) { 100 return false; 101 } 102 String ids = idValue.getStringValue(); 103 if (ids.indexOf(' ') < 0 && 104 ids.indexOf(0x09) < 0 && 105 ids.indexOf(0x0a) < 0 && 106 ids.indexOf(0x0c) < 0) { 107 NodeInfo element = doc.selectID(ids); 108 if (element==null) return false; 109 return (element.isSameNodeInfo(e)); 110 } else { 111 StringTokenizer tokenizer = new StringTokenizer (ids); 112 while (tokenizer.hasMoreElements()) { 113 String id = (String )tokenizer.nextElement(); 114 NodeInfo element = doc.selectID(id); 115 if (element != null && e.isSameNodeInfo(element)) { 116 return true; 117 } 118 } 119 return false; 120 } 121 122 } 123 124 128 129 public int getNodeKind() { 130 return Type.ELEMENT; 131 } 132 133 136 137 public NodeTest getNodeTest() { 138 return NodeKindTest.ELEMENT; 139 } 140 141 } 142 143 | Popular Tags |