1 23 package com.tc.jrexx.regex; 24 25 import java.lang.ref.*; 26 import com.tc.jrexx.automaton.*; 27 import com.tc.jrexx.set.*; 28 29 39 public class PatternPro extends Pattern { 40 41 WeakReference automatonWrapper = null; 42 44 protected PatternPro(ISet_char fullSet) { 45 super(fullSet); 46 } 47 48 protected PatternPro(Automaton_Pattern automaton) { 49 super(automaton); 50 } 51 52 protected AutomatonSet_String getInnerAutomaton() { 53 return this.automaton; 54 } 55 56 public PatternPro() { 57 this(new Automaton_Pattern()); 58 } 59 60 64 public PatternPro(PAutomaton automaton) { 65 super((Automaton_Pattern)automaton.getAutomaton()); 66 this.automatonWrapper = new WeakReference(automaton); 67 } 68 69 72 public PatternPro(Pattern p) { 73 super((Automaton_Pattern)p.automaton.clone()); 74 } 75 76 public PatternPro(String regEx) { 77 super(Pattern.get(regEx,false)); 78 } 79 80 81 82 public void setRegEx(String regEx) { 83 this.automaton = Pattern.get(regEx,false); 84 } 85 86 87 94 public void setPattern(Pattern p) { 95 this.automaton = (Automaton_Pattern)p.automaton.clone(); 96 if (p.getClass()!=Pattern.class) this.automaton.minimize(); 97 } 98 99 100 101 public void setAutomaton(PAutomaton a) { 102 this.automatonWrapper = new WeakReference(a); 103 this.automaton = (Automaton_Pattern)a.getAutomaton(); 104 } 105 106 111 public Pattern getPattern() { 112 return new Pattern((Automaton_Pattern)this.automaton.clone()); 113 } 114 115 public PAutomaton getAutomaton() { 116 if (this.automatonWrapper==null) { 117 PAutomaton answer = new PAutomaton(this.automaton); 118 this.automatonWrapper = new WeakReference(answer); 119 return answer; 120 } 121 122 PAutomaton answer = (PAutomaton)this.automatonWrapper.get(); 123 if (answer!=null) return answer; 124 125 answer = new PAutomaton(this.automaton); 126 this.automatonWrapper = new WeakReference(answer); 127 return answer; 128 } 129 130 public boolean contains(String s,int offset,int length) { 131 if (this.automaton.isDeterministic()) return super.contains(s,offset,length); 132 133 Automaton.State state = ((Automaton_Pattern)this.automaton).getStartState(); 134 135 if (state==null) return false; 140 141 Automaton.IState istate = ((Automaton_Pattern.PState)state).getEClosure(); 142 Automaton.LinkedSet_State states = 143 (istate instanceof Automaton_Pattern.PState) 144 ? this.automaton.newLinkedSet_State((Automaton_Pattern.PState)istate) 145 : (Automaton.LinkedSet_State)istate; 146 147 Automaton.LinkedSet_State newStates = this.automaton.newLinkedSet_State(); 148 149 for (;length>0; ++offset, --length) { 150 loop: for (Automaton.Wrapper_State w=states.elements; w!=null; w=w.next) { 151 if (w.state.isDeterministic()) { 152 for (Automaton.State.Transition trans=w.state.transitions; trans!=null; trans=trans.next) { 153 if (trans.charSet.contains(s.charAt(offset))) { 154 newStates.add(trans.toState); 155 continue loop; 156 } 157 } 158 } else { 159 for (Automaton.State.Transition trans=w.state.transitions; trans!=null; trans=trans.next) { 160 if (trans.charSet.contains(s.charAt(offset))) { 161 newStates.add(trans.toState); 162 } 163 } 164 } 165 } 166 167 for (Automaton.Wrapper_State w=newStates.elements; w!=null; w=w.next) { 168 for (Automaton.State.Transition trans=w.state.eTransitions; trans!=null; trans=trans.next) { 169 newStates.add(trans.toState); 170 } 171 } 172 173 if (newStates.isEmpty()) return false; 174 175 Automaton.LinkedSet_State tmp = states; 176 states = newStates; 177 newStates = tmp; 178 newStates.clear(); 179 } 180 return ((Automaton_Pattern.LinkedSet_PState)states).isFinal(); 181 182 } 199 200 public boolean contains(char[] chars,int offset,int length) { 201 if (this.automaton.isDeterministic()) return super.contains(chars,offset,length); 202 203 Automaton.State state = ((Automaton_Pattern)this.automaton).getStartState(); 204 if (state==null) return false; 205 206 Automaton.IState istate = ((Automaton_Pattern.PState)state).getEClosure(); 207 Automaton.LinkedSet_State states = 208 (istate instanceof Automaton_Pattern.PState) 209 ? this.automaton.newLinkedSet_State((Automaton_Pattern.PState)istate) 210 : (Automaton.LinkedSet_State)istate; 211 212 Automaton.LinkedSet_State newStates = this.automaton.newLinkedSet_State(); 213 214 for (;length>0; ++offset, --length) { 215 loop: for (Automaton.Wrapper_State w=states.elements; w!=null; w=w.next) { 216 if (w.state.isDeterministic()) { 217 for (Automaton.State.Transition trans=w.state.transitions; trans!=null; trans=trans.next) { 218 if (trans.charSet.contains(chars[offset])) { 219 newStates.add(trans.toState); 220 continue loop; 221 } 222 } 223 } else { 224 for (Automaton.State.Transition trans=w.state.transitions; trans!=null; trans=trans.next) { 225 if (trans.charSet.contains(chars[offset])) { 226 newStates.add(trans.toState); 227 } 228 } 229 } 230 } 231 232 for (Automaton.Wrapper_State w=newStates.elements; w!=null; w=w.next) { 233 for (Automaton.State.Transition trans=w.state.eTransitions; trans!=null; trans=trans.next) { 234 newStates.add(trans.toState); 235 } 236 } 237 238 if (newStates.isEmpty()) return false; 239 240 Automaton.LinkedSet_State tmp = states; 241 states = newStates; 242 newStates = tmp; 243 newStates.clear(); 244 } 245 return ((Automaton_Pattern.LinkedSet_PState)states).isFinal(); 246 } 247 248 public boolean contains(java.io.Reader in) throws java.io.IOException { 249 if (this.automaton.isDeterministic()) return super.contains(in); 250 251 Automaton.State state = ((Automaton_Pattern)this.automaton).getStartState(); 252 if (state==null) return false; 253 254 Automaton.IState istate = ((Automaton_Pattern.PState)state).getEClosure(); 255 Automaton.LinkedSet_State states = 256 (istate instanceof Automaton_Pattern.PState) 257 ? this.automaton.newLinkedSet_State((Automaton_Pattern.PState)istate) 258 : (Automaton.LinkedSet_State)istate; 259 260 Automaton.LinkedSet_State newStates = this.automaton.newLinkedSet_State(); 261 262 for (int ch=in.read(); ch!=-1; ch=in.read()) { 263 loop: for (Automaton.Wrapper_State w=states.elements; w!=null; w=w.next) { 264 if (w.state.isDeterministic()) { 265 for (Automaton.State.Transition trans=w.state.transitions; trans!=null; trans=trans.next) { 266 if (trans.charSet.contains((char)ch)) { 267 newStates.add(trans.toState); 268 continue loop; 269 } 270 } 271 } else { 272 for (Automaton.State.Transition trans=w.state.transitions; trans!=null; trans=trans.next) { 273 if (trans.charSet.contains((char)ch)) { 274 newStates.add(trans.toState); 275 } 276 } 277 } 278 } 279 280 for (Automaton.Wrapper_State w=newStates.elements; w!=null; w=w.next) { 281 for (Automaton.State.Transition trans=w.state.eTransitions; trans!=null; trans=trans.next) { 282 newStates.add(trans.toState); 283 } 284 } 285 286 if (newStates.isEmpty()) return false; 287 288 Automaton.LinkedSet_State tmp = states; 289 states = newStates; 290 newStates = tmp; 291 newStates.clear(); 292 } 293 return ((Automaton_Pattern.LinkedSet_PState)states).isFinal(); } 294 295 public void complement() { 296 this.automaton.complement(); 297 } 298 299 public void addAll(String regEx) { 300 this.automaton.addAll(regEx); 301 this.automaton.minimize(); 302 } 303 304 public void retainAll(String regEx) { 305 this.automaton.retainAll(regEx); 306 this.automaton.minimize(); 307 } 308 309 public void removeAll(String regEx) { 310 this.automaton.removeAll(regEx); 311 this.automaton.minimize(); 312 } 313 314 public void addAll(Pattern pattern) { 315 this.automaton.addAll(pattern.automaton); 316 this.automaton.minimize(); 317 } 318 319 public void retainAll(Pattern pattern) { 320 this.automaton.retainAll(pattern.automaton); 321 this.automaton.minimize(); 322 } 323 324 public void removeAll(Pattern pattern) { 325 this.automaton.removeAll(pattern.automaton); 326 this.automaton.minimize(); 327 } 328 329 public void addAll(PAutomaton a) { 330 this.automaton.addAll((Automaton_Pattern)a.getAutomaton()); 331 this.automaton.minimize(); 332 } 333 334 public void retainAll(PAutomaton a) { 335 this.automaton.retainAll((Automaton_Pattern)a.getAutomaton()); 336 this.automaton.minimize(); 337 } 338 339 public void removeAll(PAutomaton a) { 340 this.automaton.removeAll((Automaton_Pattern)a.getAutomaton()); 341 this.automaton.minimize(); 342 } 343 344 public void clear() { 345 this.automaton.clear(); 346 } 347 348 } | Popular Tags |