KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > jrexx > regex > PatternPro


1 /*
2 * 01/07/2003 - 15:19:32
3 *
4 * PatternPro.java -
5 * Copyright (C) 2003 Buero fuer Softwarearchitektur GbR
6 * ralf.meyer@karneim.com
7 * http://jrexx.sf.net
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */

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 /**
30  * Regular expression based on a minimized deterministic automaton (FSA) and designed as a set of strings.
31  * <br>Use this class to manipulate a reular expression through set oprations or automaton methods
32  * <br>PatternPro differs from Pattern that the contributed set of strings is mutable through the methods
33  * addAll, removeAll and retainAll.
34  * <br>Further PaternPro provides access to its PAutomaton through the getAutomaton method.
35  * So it is possible to inspect the automaton's states through PAutomaton's methods.
36  * @author Ralf Meyer
37  * @version 1.0
38  */

39 public class PatternPro extends Pattern {
40
41   WeakReference automatonWrapper = null;
42 // WeakReference patternWrapper = null;
43

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   /**
61    * creates a PatternPro with the given automaton. The automaton will not be cloned:
62    * two PatternPro can use the same automaton.
63    */

64   public PatternPro(PAutomaton automaton) {
65     super((Automaton_Pattern)automaton.getAutomaton());
66     this.automatonWrapper = new WeakReference(automaton);
67   }
68
69   /**
70    * copy constructor
71    */

72   public PatternPro(Pattern p) {
73     super((Automaton_Pattern)p.automaton.clone());
74   }
75
76   public PatternPro(String JavaDoc regEx) {
77     super(Pattern.get(regEx,false));
78   }
79
80
81
82   public void setRegEx(String JavaDoc regEx) {
83     this.automaton = Pattern.get(regEx,false);
84   }
85
86
87   /**
88    * if p is an instance of PatternPro
89    * use setAutomaton(p.getAutomaton());
90    * else setAutomaton(new PatternPro(p).getAutomaton())
91    *
92    * @deprecated
93    */

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   /**
107    * don't needed: you have a PatternPro which extends Pattern.
108    * (Pattern)this.clone() has the same effect
109    * @deprecated
110    */

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 JavaDoc 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 //int _offset = offset;
136
//int _length = length;
137
//long start = System.currentTimeMillis();
138
//try {
139
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 //} finally {
183
// long end = System.currentTimeMillis();
184
// System.out.println("Pattern.contains: "+(end-start));
185
// if (length>0) {
186
// System.out.println(this.automaton);
187
// s = s.substring(_offset,_offset+_length);
188
// offset = offset-_offset;
189
// if (offset<=100) System.out.println(" can start with: "+s.substring(0,offset)+"\"");
190
// else System.out.println(" can start with: \""+s.substring(0,100)+"...\""+s.length());
191
//
192
// if (s.length()-offset<=100) System.out.println(" stopped for : "+s.substring(offset)+"\"");
193
// else System.out.println(" stopped for : "+s.substring(offset,offset+100)+"...\""+(s.length()-offset));
194
//
195
// System.out.println("currentState: "+state);
196
// }
197
//}
198
}
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 JavaDoc in) throws java.io.IOException JavaDoc {
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 JavaDoc regEx) {
300     this.automaton.addAll(regEx);
301     this.automaton.minimize();
302   }
303
304   public void retainAll(String JavaDoc regEx) {
305     this.automaton.retainAll(regEx);
306     this.automaton.minimize();
307   }
308
309   public void removeAll(String JavaDoc 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