KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * 01/07/2003 - 15:19:32
3 *
4 * Pattern.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 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 JavaDoc;
33
34 /**
35  * Regular expression based on a minimized deterministic automaton (DFA) and designed as an immutable set of strings.
36  * <br>Use this class to create a regular expression and match strings against it.
37  * <br>for example:
38  * <br>to check whether a given string is a number try<br>
39  * <br><code>new Pattern("[0-9]+").contains(s)</code>
40  *
41  * @author Ralf Meyer
42  * @version 1.1
43  */

44 public class Pattern implements Cloneable JavaDoc {
45
46   protected static final HashMap AUTOMATON_MAP = new HashMap();
47
48   protected static final Automaton_Pattern get(String JavaDoc regEx,boolean cache) {
49     if (cache) {
50       synchronized(AUTOMATON_MAP) {
51         final SoftReference JavaDoc reference = (SoftReference JavaDoc)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 JavaDoc(automaton));
73         return automaton;
74       }
75     } else {
76       SoftReference JavaDoc reference = null;
77       synchronized(AUTOMATON_MAP) {
78         reference = (SoftReference JavaDoc)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 /*
107   protected Pattern() {
108     this(new Automaton_Pattern());
109   }
110 */

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   /**
120    * creates a minimized deterministic automaton (DFA) from the given regEx pattern.
121    */

122   public Pattern(String JavaDoc regEx) {
123     this(Pattern.get(regEx,true));
124   }
125
126   public boolean contains(String JavaDoc s) {
127     return this.contains(s,0,s.length());
128   }
129
130   public boolean contains(String JavaDoc s,int offset) {
131     return this.contains(s,offset,s.length()-offset);
132   }
133
134
135   public boolean contains(String JavaDoc s,int offset,int length) {
136     Automaton.State state = ((Automaton_Pattern)this.automaton).getStartState();
137
138 //int _offset = offset;
139
//int _length = length;
140
//long start = System.currentTimeMillis();
141
//try {
142
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 //} finally {
159
// long end = System.currentTimeMillis();
160
// System.out.println("Pattern.contains: "+(end-start));
161
// if (length>0) {
162
// System.out.println(this.automaton);
163
// s = s.substring(_offset,_offset+_length);
164
// offset = offset-_offset;
165
// if (offset<=100) System.out.println(" can start with: "+s.substring(0,offset)+"\"");
166
// else System.out.println(" can start with: \""+s.substring(0,100)+"...\""+s.length());
167

168 // if (s.length()-offset<=100) System.out.println(" stopped for : "+s.substring(offset)+"\"");
169
// else System.out.println(" stopped for : "+s.substring(offset,offset+100)+"...\""+(s.length()-offset));
170

171 // System.out.println("currentState: "+state);
172
// }
173
//}
174

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 JavaDoc in) throws java.io.IOException JavaDoc {
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 JavaDoc getRegEx() {
221     return this.automaton.regEx;
222   }
223
224   public String JavaDoc toString() {
225     return this.getRegEx();
226   }
227
228 /*
229   private void writeObject(java.io.OutputStream out) throws IOException {
230     out.writeObject(this.toAutomatonData());
231   }
232   private void readObject(java.io.InputStream in) throws IOException,ClassNotFoundException {
233     SAutomatonData a = (SAutomatonData)in.readObject();
234     this.init(a);
235   }
236
237
238   public SAutomatonData toAutomatonData() {
239     return new PAutomaton(this.automaton).toData();
240   }
241
242   public void toAutomatonData(OutputStream automatonDataStream) throws IOException {
243     new OutputStream(automatonDataStream).writeObject(this);
244   }
245
246
247   protected void init(SAutomatonData a) {
248     this.automaton = (Automaton_Pattern)new PAutomaton(a).getAutomaton();
249   }
250 */

251
252   public Object JavaDoc clone() {
253     try {
254       Pattern clone = (Pattern)super.clone();
255       clone.automaton = (Automaton_Pattern)clone.automaton.clone();
256       return clone;
257     } catch(CloneNotSupportedException JavaDoc e) {
258       throw new Error JavaDoc("should never happen");
259     }
260   }
261
262 /*
263   public Iterator iterator() {
264     return new Iterator() {
265       HashMap fromStates = null;
266       HashMap toStates = new HashMap();
267       Iterator it_states = null;
268       ISet_char.Iterator it_charSet = null;
269       {
270         Object startState = Pattern.this.automaton.getStartState();
271         if (startState!=null) {
272           LinkedList lists = new LinkedList(); lists.add(new LinkedList());
273           toStates.put(startState,lists);
274         }
275
276
277       }
278
279       public boolean hasNext() {
280         return false;
281       }
282       public Object next() {
283         it_charSet.next();
284         //if (this.it_states==null) it_states = fromStates.keySet().iterator();
285         if (it_states.hasNext()==false) {
286           this.fromStates = toStates;
287           this.toStates = new HashMap();
288           this.it_states
289         }
290
291         IStatePro.ITransition[] trans = state.getTransitions();
292
293         Iterator it_lists = lists.iterator();
294         while (it_lists.hasNext()) {
295           LinkedList elements = (LinkedList)it_lists.next();
296
297           for (int i=0; i<trans.length; ++i) {
298             LinkedList toLists = (LinkedList)toStates.get(trans[i].getToState());
299             if (toLists==null) {
300               toLists = new LinkedList();
301               toStates.put(trans[i].getToState(),toLists);
302             }
303
304             LinkedList toElements = new LinkedList(elements);
305             toElements.add(trans[i].getCharSet());
306             toLists.add(toElements);
307           }
308         }
309
310         return null;
311       }
312
313       public void remove() {
314         throw new UnsupportedOperationException();
315       }
316     };
317   }
318 */

319 }
320
321
322
Popular Tags