1 28 29 package com.caucho.xpath; 30 31 import com.caucho.loader.EnvironmentLocal; 32 import com.caucho.log.Log; 33 import com.caucho.util.LruCache; 34 import com.caucho.xpath.pattern.AbstractPattern; 35 import com.caucho.xpath.pattern.FromContext; 36 37 import org.w3c.dom.Node ; 38 39 import java.util.Iterator ; 40 import java.util.logging.Level ; 41 import java.util.logging.Logger ; 42 43 70 public class XPath { 71 private static final Logger log = Log.open(XPath.class); 72 73 private static EnvironmentLocal<LruCache<String ,Pattern>> _matchCache = 74 new EnvironmentLocal<LruCache<String ,Pattern>>(); 75 76 private static EnvironmentLocal<LruCache<String ,Pattern>> _selectCache = 77 new EnvironmentLocal<LruCache<String ,Pattern>>(); 78 79 private static EnvironmentLocal<LruCache<String ,Expr>> _exprCache = 80 new EnvironmentLocal<LruCache<String ,Expr>>(); 81 82 private XPath() 83 { 84 } 85 86 95 public static Node find(String query, Node node) 96 throws XPathException 97 { 98 Pattern pattern = parseSelect(query); 99 100 return (Node ) pattern.find(node); 101 } 102 103 110 public static Iterator select(String query, Node node) 111 throws XPathException 112 { 113 Pattern pattern = parseSelect(query); 114 115 return pattern.select(node); 116 } 117 118 125 public static Pattern parseSelect(String query) 126 throws XPathParseException 127 { 128 LruCache<String ,Pattern> cache = _selectCache.get(); 129 if (cache == null) 130 cache = new LruCache<String ,Pattern>(128); 131 132 Pattern pattern = cache.get(query); 133 134 if (pattern == null) { 135 pattern = parseSelect(query, null); 136 cache.put(query, pattern); 137 } 138 139 return pattern; 140 } 141 142 154 public static Pattern parseSelect(String query, NamespaceContext namespace) 155 throws XPathParseException 156 { 157 XPathParser parser = new XPathParser(query, namespace); 158 159 AbstractPattern pattern = parser.parseSelect(); 160 161 if (log.isLoggable(Level.FINER)) 162 log.finest("select: " + pattern); 163 164 return new Pattern(pattern); 165 } 166 167 176 public static Pattern parseMatch(String query) 177 throws XPathParseException 178 { 179 LruCache<String ,Pattern> cache = _matchCache.get(); 180 if (cache == null) 181 cache = new LruCache<String ,Pattern>(128); 182 183 Pattern pattern = cache.get(query); 184 185 if (pattern == null) { 186 pattern = parseMatch(query, null); 187 cache.put(query, pattern); 188 } 189 190 return pattern; 191 } 192 193 204 public static Pattern parseMatch(String query, NamespaceContext namespace) 205 throws XPathParseException 206 { 207 XPathParser parser = new XPathParser(query, namespace); 208 209 AbstractPattern pattern = parser.parseMatch(); 210 211 if (log.isLoggable(Level.FINER)) 212 log.finest("match: " + pattern); 213 214 return new Pattern(pattern); 215 } 216 217 232 public static String evalString(String query, Node node) 233 throws XPathException 234 { 235 Expr expr = parseExpr(query); 236 237 return expr.evalString(node); 238 } 239 240 248 public static double evalNumber(String query, Node node) 249 throws XPathException 250 { 251 Expr expr = parseExpr(query); 252 253 return expr.evalNumber(node); 254 } 255 256 264 public static boolean evalBoolean(String query, Node node) 265 throws XPathException 266 { 267 Expr expr = parseExpr(query); 268 269 return expr.evalBoolean(node); 270 } 271 272 280 public static Object evalObject(String query, Node node) 281 throws XPathException 282 { 283 Expr expr = parseExpr(query); 284 285 return expr.evalObject(node); 286 } 287 288 294 public static Expr parseExpr(String query) 295 throws XPathParseException 296 { 297 LruCache<String ,Expr> cache = _exprCache.get(); 298 if (cache == null) 299 cache = new LruCache<String ,Expr>(128); 300 301 Expr expr = cache.get(query); 302 303 if (expr == null) { 304 expr = parseExpr(query, null); 305 cache.put(query, expr); 306 } 307 308 return expr; 309 } 310 311 319 public static Expr parseExpr(String query, NamespaceContext namespace) 320 throws XPathParseException 321 { 322 XPathParser parser = new XPathParser(query, namespace); 323 324 Expr expr = parser.parseExpr(); 325 326 if (log.isLoggable(Level.FINER)) 327 log.finest("expr: " + expr); 328 329 return expr; 330 } 331 332 341 public static Expr parseExpr(String query, NamespaceContext namespace, 342 AbstractPattern nodeList) 343 throws XPathParseException 344 { 345 XPathParser parser = new XPathParser(query, namespace); 346 347 Expr expr = parser.parseExpr(new FromContext(), nodeList); 348 349 if (expr != null) 350 expr.setListContext(nodeList); 351 352 if (log.isLoggable(Level.FINER)) 353 log.finest("expr: " + expr); 354 355 return expr; 356 } 357 358 361 public static Env createEnv() 362 { 363 return Env.create(); 364 } 365 366 371 public static Env createEnv(Env global) 372 { 373 Env env = Env.create(); 374 375 env.init(global); 376 377 return env; 378 } 379 380 385 public static Env createCall(Env parent) 386 { 387 Env env = Env.create(); 388 389 env.initMacro(parent); 390 391 return env; 392 } 393 394 397 public static void freeEnv(Env env) 398 { 399 } 401 } 402 | Popular Tags |