1 28 29 package com.caucho.xpath; 30 31 import com.caucho.log.Log; 32 import com.caucho.xpath.pattern.AbstractPattern; 33 import com.caucho.xpath.pattern.NodeIterator; 34 35 import org.w3c.dom.Node ; 36 37 import java.util.Iterator ; 38 import java.util.logging.Logger ; 39 40 51 public class Pattern { 52 protected final static Logger log = Log.open(Pattern.class); 53 54 private AbstractPattern pattern; 55 56 Pattern(AbstractPattern pattern) 57 { 58 this.pattern = pattern; 59 } 60 61 69 public Node find(Node node) 70 throws XPathException 71 { 72 if (node == null) 73 throw new NullPointerException (); 74 75 Env env = XPath.createEnv(); 76 80 Iterator iter = pattern.select(node, env); 81 82 Node value = null; 83 if (iter.hasNext()) 84 value = (Node ) iter.next(); 85 86 XPath.freeEnv(env); 87 88 return value; 89 } 90 91 100 public Node find(Node node, ExprEnvironment env) 101 throws XPathException 102 { 103 if (node == null) 104 throw new NullPointerException (); 105 106 if (env instanceof Env) { 107 Env globalEnv = (Env) env; 108 109 } 113 114 Iterator iter = pattern.select(node, env); 115 116 Node value = null; 117 if (iter.hasNext()) 118 value = (Node ) iter.next(); 119 120 return value; 121 } 122 123 131 public NodeIterator select(Node node) 132 throws XPathException 133 { 134 if (node == null) 135 throw new NullPointerException (); 136 137 Env env = XPath.createEnv(); 138 139 env.setCurrentNode(node); 140 141 NodeIterator iter = pattern.select(node, env); 142 143 XPath.freeEnv(env); 144 145 return iter; 146 } 147 148 157 public NodeIterator select(Node node, ExprEnvironment env) 158 throws XPathException 159 { 160 if (node == null) 161 throw new NullPointerException (); 162 163 if (env instanceof Env) { 164 Env globalEnv = (Env) env; 165 globalEnv.setCurrentNode(node); 166 globalEnv.setContextNode(node); 167 } 168 169 return pattern.select(node, env); 170 } 171 172 180 public boolean isMatch(Node node) 181 throws XPathException 182 { 183 Env env = XPath.createEnv(); 184 185 189 boolean value = pattern.match(node, env); 190 191 XPath.freeEnv(env); 192 193 return value; 194 } 195 196 205 public boolean isMatch(Node node, ExprEnvironment env) 206 throws XPathException 207 { 208 return pattern.match(node, env); 209 } 210 211 214 public AbstractPattern getPattern() 215 { 216 return pattern; 217 } 218 219 public String toString() 220 { 221 return pattern.toString(); 222 } 223 } 224 | Popular Tags |