1 28 29 package com.caucho.xpath.pattern; 30 31 import com.caucho.util.CharBuffer; 32 import com.caucho.xpath.Env; 33 import com.caucho.xpath.ExprEnvironment; 34 35 import org.w3c.dom.Node ; 36 37 40 public class FromContext extends Axis { 41 int _ancestorCount; 43 44 public FromContext() 45 { 46 super(null); 47 } 48 49 56 public FromContext(int ancestorCount) 57 { 58 super(null); 59 60 _ancestorCount = ancestorCount; 61 } 62 63 public int getCount() 64 { 65 return _ancestorCount; 66 } 67 68 76 public boolean match(Node node, ExprEnvironment env) 77 { 78 Node context = env.getContextNode(); 79 80 for (int i = 0; i < _ancestorCount && context != null; i++) 81 context = context.getParentNode(); 82 83 return (node == context); 84 } 85 86 89 public boolean isStrictlyAscending() 90 { 91 return true; 92 } 93 94 97 boolean isSingleSelect() 98 { 99 return true; 100 } 101 102 111 public int position(Node node, Env env, AbstractPattern pattern) 112 { 113 return 1; 114 } 115 116 125 public int count(Node node, Env env, AbstractPattern pattern) 126 { 127 return 1; 128 } 129 130 138 public Node firstNode(Node node, ExprEnvironment env) 139 { 140 for (int i = 0; node != null && i < _ancestorCount; i++) 141 node = node.getParentNode(); 142 143 return node; 144 } 145 146 154 public Node nextNode(Node node, Node lastNode) 155 { 156 return null; 157 } 158 159 162 public boolean equals(Object b) 163 { 164 return (b instanceof FromContext && 165 _ancestorCount == ((FromContext) b)._ancestorCount); 166 } 167 168 171 public String toString() 172 { 173 if (_ancestorCount == 0) 174 return "context()"; 175 176 CharBuffer cb = CharBuffer.allocate(); 177 178 cb.append(".."); 179 180 for (int i = 1; i < _ancestorCount; i++) { 181 cb.append("/.."); 182 } 183 184 return cb.close(); 185 } 186 } 187 | Popular Tags |