1 23 package com.tc.jrexx.regex; 24 25 import com.tc.jrexx.set.ISet_char; 26 27 import java.util.*; 28 29 30 import com.tc.jrexx.automaton.*; 31 32 import java.lang.ref.SoftReference ; 33 34 44 public class Pattern implements Cloneable { 45 46 protected static final HashMap AUTOMATON_MAP = new HashMap(); 47 48 protected static final Automaton_Pattern get(String regEx,boolean cache) { 49 if (cache) { 50 synchronized(AUTOMATON_MAP) { 51 final SoftReference reference = (SoftReference )AUTOMATON_MAP.get(regEx); 52 53 if (reference!=null) { 54 Automaton_Pattern automaton = (Automaton_Pattern)reference.get(); 55 if (automaton!=null) return automaton; 56 } 57 58 final Automaton_Pattern automaton = new Automaton_Pattern(regEx); 59 60 final Automaton.LinkedSet_State states = automaton.getStates(); 61 for (Automaton.Wrapper_State w = states.elements; w!=null; w=w.next) { 62 for (Automaton.State.Transition trans=w.state.eTransitions; trans!=null; trans=trans.next) { 63 trans.properties = null; 64 } 65 for (Automaton.State.Transition trans=w.state.transitions; trans!=null; trans=trans.next) { 66 trans.properties = null; 67 } 68 } 69 70 automaton.minimize(); 71 72 AUTOMATON_MAP.put(regEx,new SoftReference (automaton)); 73 return automaton; 74 } 75 } else { 76 SoftReference reference = null; 77 synchronized(AUTOMATON_MAP) { 78 reference = (SoftReference )AUTOMATON_MAP.get(regEx); 79 } 80 81 if (reference!=null) { 82 Automaton_Pattern automaton = (Automaton_Pattern)reference.get(); 83 if (automaton!=null) return automaton; 84 } 85 86 final Automaton_Pattern automaton = new Automaton_Pattern(regEx); 87 88 final Automaton.LinkedSet_State states = automaton.getStates(); 89 for (Automaton.Wrapper_State w = states.elements; w!=null; w=w.next) { 90 for (Automaton.State.Transition trans=w.state.eTransitions; trans!=null; trans=trans.next) { 91 trans.properties = null; 92 } 93 for (Automaton.State.Transition trans=w.state.transitions; trans!=null; trans=trans.next) { 94 trans.properties = null; 95 } 96 } 97 98 automaton.minimize(); 99 100 return automaton; 101 } 102 } 103 104 105 protected Automaton_Pattern automaton; 106 111 protected Pattern(Automaton_Pattern automaton) { 112 this.automaton = automaton; 113 } 114 115 protected Pattern(ISet_char fullSet) { 116 this.automaton = new Automaton_Pattern(fullSet); 117 } 118 119 122 public Pattern(String regEx) { 123 this(Pattern.get(regEx,true)); 124 } 125 126 public boolean contains(String s) { 127 return this.contains(s,0,s.length()); 128 } 129 130 public boolean contains(String s,int offset) { 131 return this.contains(s,offset,s.length()-offset); 132 } 133 134 135 public boolean contains(String s,int offset,int length) { 136 Automaton.State state = ((Automaton_Pattern)this.automaton).getStartState(); 137 138 if (state==null) return false; 143 144 Automaton.LinkedSet_State states = this.automaton.newLinkedSet_State(state); 145 Automaton.LinkedSet_State newStates = this.automaton.newLinkedSet_State(); 146 147 loop: for (;length>0; ++offset, --length) { 148 for (Automaton.State.Transition trans=state.transitions; trans!=null; trans=trans.next) { 149 if (trans.charSet.contains(s.charAt(offset))) { 150 state = trans.toState; 151 continue loop; 152 } 153 } 154 return false; 155 } 156 157 return ((Automaton_Pattern.PState)state).isFinal(); 158 168 171 175 } 176 177 public boolean contains(char[] chars) { 178 return this.contains(chars,0,chars.length); 179 } 180 181 public boolean contains(char[] chars,int offset) { 182 return this.contains(chars,offset,chars.length-offset); 183 } 184 185 186 public boolean contains(char[] chars,int offset,int length) { 187 Automaton.State state = ((Automaton_Pattern)this.automaton).getStartState(); 188 if (state==null) return false; 189 190 loop: for (;length>0; ++offset, --length) { 191 for (Automaton.State.Transition trans=state.transitions; trans!=null; trans=trans.next) { 192 if (trans.charSet.contains(chars[offset])) { 193 state = trans.toState; 194 continue loop; 195 } 196 } 197 return false; 198 } 199 200 return ((Automaton_Pattern.PState)state).isFinal; 201 } 202 203 public boolean contains(java.io.Reader in) throws java.io.IOException { 204 Automaton.State state = ((Automaton_Pattern)this.automaton).getStartState(); 205 if (state==null) return false; 206 207 loop: for (int ch=in.read(); ch!=-1; ch=in.read()) { 208 for (Automaton.State.Transition trans=state.transitions; trans!=null; trans=trans.next) { 209 if (trans.charSet.contains((char)ch)) { 210 state = trans.toState; 211 continue loop; 212 } 213 } 214 return false; 215 } 216 return ((Automaton_Pattern.PState)state).isFinal; 217 } 218 219 220 public String getRegEx() { 221 return this.automaton.regEx; 222 } 223 224 public String toString() { 225 return this.getRegEx(); 226 } 227 228 251 252 public Object clone() { 253 try { 254 Pattern clone = (Pattern)super.clone(); 255 clone.automaton = (Automaton_Pattern)clone.automaton.clone(); 256 return clone; 257 } catch(CloneNotSupportedException e) { 258 throw new Error ("should never happen"); 259 } 260 } 261 262 319 } 320 321 322 | Popular Tags |