KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > process > PatternAutomaton


1 /*
2  * Copyright (C) Chaperon. All rights reserved.
3  * -------------------------------------------------------------------------
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8
9 package net.sourceforge.chaperon.process;
10
11 import net.sourceforge.chaperon.common.Decoder;
12
13 import java.io.Serializable JavaDoc;
14
15 /**
16  * This class represents automaton to recognized text pattern.
17  *
18  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
19  * @version CVS $Id: PatternAutomaton.java,v 1.8 2004/01/08 11:30:52 benedikta Exp $
20  */

21 public class PatternAutomaton implements Serializable JavaDoc
22 {
23   /** A simple transition without matching */
24   public static final int TYPE_NOMATCH = 0;
25
26   /** If the character should match */
27   public static final int TYPE_MATCH = 1;
28
29   /** If the characher shouldn't match */
30   public static final int TYPE_EXMATCH = 2;
31
32   /** Match all characters */
33   public static final int TYPE_MATCHANY = 3;
34
35   /** Begin of line */
36   public static final int TYPE_BOL = 4;
37
38   /** End of line */
39   public static final int TYPE_EOL = 5;
40
41   /** End of file */
42
43   // public final static int TYPE_EOF = 6;
44

45   /** Start of group */
46   public static final int TYPE_GROUPSTART = 6;
47
48   /** End of group */
49   public static final int TYPE_GROUPEND = 7;
50   private int[] types = new int[0];
51   private char[] intervalbegin = new char[0];
52   private char[] intervalend = new char[0];
53   private int[] groupindices = new int[0];
54   private int[][] transitions = new int[0][0];
55
56   // Count of states
57
private int statecount = 0;
58
59   // Initial state
60
private int firststate = -1;
61
62   // Accepted state
63
private int finalstate = -1;
64   private int groupcount = 0;
65   private static final long serialVersionUID = 1246342009422283917L;
66
67   /**
68    * Create a new pattern automaton.
69    *
70    * @param statecount Count of states.
71    */

72   public PatternAutomaton(int statecount)
73   {
74     if (statecount<=0)
75       throw new IllegalArgumentException JavaDoc("Count of states is invalid");
76
77     this.statecount = statecount;
78
79     types = new int[statecount];
80     intervalbegin = new char[statecount];
81     intervalend = new char[statecount];
82     groupindices = new int[statecount];
83     transitions = new int[statecount][0];
84
85     for (int state = 0; state<statecount; state++)
86     {
87       types[state] = TYPE_NOMATCH;
88       intervalbegin[state] = '\u0000';
89       intervalend[state] = '\u0000';
90       groupindices[state] = 0;
91     }
92   }
93
94   /**
95    * Set the of of transition.
96    *
97    * @param state Index of state.
98    * @param type Type of transition.
99    */

100   public void setType(int state, int type)
101   {
102     if ((type<TYPE_NOMATCH) || (type>TYPE_GROUPEND))
103       throw new IndexOutOfBoundsException JavaDoc();
104
105     this.types[state] = type;
106   }
107
108   /**
109    * Return the type of transition.
110    *
111    * @param state Index of state.
112    *
113    * @return Type of transition.
114    */

115   public int getType(int state)
116   {
117     return types[state];
118   }
119
120   /**
121    * Set the character interval, which the processor should matches against.
122    *
123    * @param state Index of state.
124    * @param begin Begin of the character interval.
125    * @param end End of the character interval.
126    */

127   public void setInterval(int state, char begin, char end)
128   {
129     this.intervalbegin[state] = begin;
130     this.intervalend[state] = end;
131   }
132
133   /**
134    * Return the begin of the character interval.
135    *
136    * @param state Index of state.
137    *
138    * @return Begin of the character interval.
139    */

140   public char getIntervalBegin(int state)
141   {
142     return intervalbegin[state];
143   }
144
145   /**
146    * Return the end of the character interval.
147    *
148    * @param state Index of state.
149    *
150    * @return End of the character interval.
151    */

152   public char getIntervalEnd(int state)
153   {
154     return intervalend[state];
155   }
156
157   /**
158    * Set the group index for a transition.
159    *
160    * @param state Index of state.
161    * @param groupindex Index of group.
162    */

163   public void setGroupIndex(int state, int groupindex)
164   {
165     groupindices[state] = groupindex;
166   }
167
168   /**
169    * Return the index of a group for a transition.
170    *
171    * @param state Index of state.
172    *
173    * @return Index of group.
174    */

175   public int getGroupIndex(int state)
176   {
177     return groupindices[state];
178   }
179
180   /**
181    * Set the count of groups.
182    *
183    * @param groupcount Count of groups.
184    */

185   public void setGroupCount(int groupcount)
186   {
187     this.groupcount = groupcount;
188   }
189
190   /**
191    * Return the count of groups.
192    *
193    * @return Count of groups.
194    */

195   public int getGroupCount()
196   {
197     return groupcount;
198   }
199
200   /**
201    * Set the destination states for a transition.
202    *
203    * @param state Index of state.
204    * @param transitions Destination states.
205    */

206   public void setTransitions(int state, int[] transitions)
207   {
208     this.transitions[state] = transitions;
209   }
210
211   /**
212    * Returns the destinations of the transition.
213    *
214    * @param state Index of the state
215    *
216    * @return Destinations of the transition.
217    */

218   public int[] getTransitions(int state)
219   {
220     return transitions[state];
221   }
222
223   /**
224    * Add a state as destination to the transition.
225    *
226    * @param state Index of transition.
227    * @param nextstate Destination state.
228    */

229   public void addTransition(int state, int nextstate)
230   {
231     // Prevent multiple entries
232
for (int i = 0; i<transitions[state].length; i++)
233       if (transitions[state][i]==nextstate)
234         return;
235
236     int[] newtransitions = new int[transitions[state].length+1];
237
238     System.arraycopy(transitions[state], 0, newtransitions, 0, transitions[state].length);
239     newtransitions[transitions[state].length] = nextstate;
240     transitions[state] = newtransitions;
241   }
242
243   /**
244    * Set the first state of the automaton.
245    *
246    * @param firststate First state of the automaton.
247    */

248   public void setFirstState(int firststate)
249   {
250     if ((firststate<0) || (firststate>statecount))
251       throw new IllegalArgumentException JavaDoc();
252
253     this.firststate = firststate;
254   }
255
256   /**
257    * Return the first state of the automaton.
258    *
259    * @return First state of the automaton.
260    */

261   public int getFirstState()
262   {
263     return firststate;
264   }
265
266   /**
267    * Set the final state. If the automaton reaches this state, the automate was successful
268    *
269    * @param finalstate Final state
270    */

271   public void setFinalState(int finalstate)
272   {
273     if ((finalstate<0) || (finalstate>statecount))
274       throw new IllegalArgumentException JavaDoc();
275
276     this.finalstate = finalstate;
277   }
278
279   /**
280    * Returns the index of the final state
281    *
282    * @return Index of the final state
283    */

284   public int getFinalState()
285   {
286     return finalstate;
287   }
288
289   /**
290    * Test if the state is the final state
291    *
292    * @param state Index of the state
293    *
294    * @return True, if the state is the final state
295    */

296   public boolean isFinalState(int state)
297   {
298     return finalstate==state;
299   }
300
301   /**
302    * Returns the count of states
303    *
304    * @return Count of states
305    */

306   public int getStateCount()
307   {
308     return statecount;
309   }
310
311   /** Creates empty string for indenting */
312   private static final String JavaDoc spaces =
313     " ";
314
315   /**
316    * Return a string representation of the automaton.
317    *
318    * @return String representation of the automaton.
319    */

320   public String JavaDoc toString()
321   {
322     String JavaDoc[] chars = new String JavaDoc[statecount];
323     int i;
324
325     for (i = 0; i<statecount; i++)
326       switch (types[i])
327       {
328         case TYPE_NOMATCH:
329           chars[i] = " ";
330           break;
331         case TYPE_MATCH:
332           chars[i] = Decoder.toClass(intervalbegin[i], intervalend[i]);
333           break;
334         case TYPE_EXMATCH:
335           chars[i] = Decoder.toNegativeClass(intervalbegin[i], intervalend[i]);
336           break;
337         case TYPE_MATCHANY:
338           chars[i] = ".";
339           break;
340         case TYPE_BOL:
341           chars[i] = "^";
342           break;
343         case TYPE_EOL:
344           chars[i] = "$";
345           break;
346         case TYPE_GROUPSTART:
347           chars[i] = "(["+groupindices[i]+"]";
348           break;
349         case TYPE_GROUPEND:
350           chars[i] = ")["+groupindices[i]+"]";
351           break;
352       }
353
354     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
355     String JavaDoc dummy;
356
357     for (i = 0; i<statecount; i++)
358     {
359       dummy = String.valueOf(i);
360       buffer.append(" ");
361       buffer.append(dummy);
362       buffer.append(spaces.substring(0, Math.max(0, chars[i].length()-dummy.length()+1)));
363     }
364
365     buffer.append("\n");
366
367     for (i = 0; i<statecount; i++)
368     {
369       buffer.append(" ");
370       buffer.append(String.valueOf(chars[i]));
371       buffer.append(" ");
372     }
373
374     buffer.append("\n");
375
376     int maxtransitions = 0;
377
378     for (i = 0; i<statecount; i++)
379       maxtransitions = Math.max(maxtransitions, transitions[i].length);
380
381     for (int j = 0; j<maxtransitions; j++)
382     {
383       for (i = 0; i<statecount; i++)
384         if (j<transitions[i].length)
385         {
386           dummy = String.valueOf(transitions[i][j]);
387
388           buffer.append(" ");
389           buffer.append(dummy);
390           buffer.append(spaces.substring(0, Math.max(0, chars[i].length()-dummy.length()+1)));
391         }
392         else
393         {
394           buffer.append(" ");
395           buffer.append(spaces.substring(0, Math.max(0, chars[i].length()+1)));
396         }
397
398       buffer.append("\n");
399     }
400
401     buffer.append("First state = ");
402     buffer.append(String.valueOf(firststate));
403     buffer.append("\n");
404     buffer.append("Final state = ");
405     buffer.append(String.valueOf(finalstate));
406     buffer.append("\n");
407     buffer.append("State count = ");
408     buffer.append(String.valueOf(statecount));
409     buffer.append("\n");
410     buffer.append("Group count = ");
411     buffer.append(String.valueOf(groupcount));
412
413     return buffer.toString();
414   }
415 }
416
Popular Tags