1 8 9 package net.sourceforge.chaperon.process; 10 11 import net.sourceforge.chaperon.common.Decoder; 12 13 import java.io.Serializable ; 14 15 21 public class PatternAutomaton implements Serializable 22 { 23 24 public static final int TYPE_NOMATCH = 0; 25 26 27 public static final int TYPE_MATCH = 1; 28 29 30 public static final int TYPE_EXMATCH = 2; 31 32 33 public static final int TYPE_MATCHANY = 3; 34 35 36 public static final int TYPE_BOL = 4; 37 38 39 public static final int TYPE_EOL = 5; 40 41 42 43 45 46 public static final int TYPE_GROUPSTART = 6; 47 48 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 private int statecount = 0; 58 59 private int firststate = -1; 61 62 private int finalstate = -1; 64 private int groupcount = 0; 65 private static final long serialVersionUID = 1246342009422283917L; 66 67 72 public PatternAutomaton(int statecount) 73 { 74 if (statecount<=0) 75 throw new IllegalArgumentException ("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 100 public void setType(int state, int type) 101 { 102 if ((type<TYPE_NOMATCH) || (type>TYPE_GROUPEND)) 103 throw new IndexOutOfBoundsException (); 104 105 this.types[state] = type; 106 } 107 108 115 public int getType(int state) 116 { 117 return types[state]; 118 } 119 120 127 public void setInterval(int state, char begin, char end) 128 { 129 this.intervalbegin[state] = begin; 130 this.intervalend[state] = end; 131 } 132 133 140 public char getIntervalBegin(int state) 141 { 142 return intervalbegin[state]; 143 } 144 145 152 public char getIntervalEnd(int state) 153 { 154 return intervalend[state]; 155 } 156 157 163 public void setGroupIndex(int state, int groupindex) 164 { 165 groupindices[state] = groupindex; 166 } 167 168 175 public int getGroupIndex(int state) 176 { 177 return groupindices[state]; 178 } 179 180 185 public void setGroupCount(int groupcount) 186 { 187 this.groupcount = groupcount; 188 } 189 190 195 public int getGroupCount() 196 { 197 return groupcount; 198 } 199 200 206 public void setTransitions(int state, int[] transitions) 207 { 208 this.transitions[state] = transitions; 209 } 210 211 218 public int[] getTransitions(int state) 219 { 220 return transitions[state]; 221 } 222 223 229 public void addTransition(int state, int nextstate) 230 { 231 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 248 public void setFirstState(int firststate) 249 { 250 if ((firststate<0) || (firststate>statecount)) 251 throw new IllegalArgumentException (); 252 253 this.firststate = firststate; 254 } 255 256 261 public int getFirstState() 262 { 263 return firststate; 264 } 265 266 271 public void setFinalState(int finalstate) 272 { 273 if ((finalstate<0) || (finalstate>statecount)) 274 throw new IllegalArgumentException (); 275 276 this.finalstate = finalstate; 277 } 278 279 284 public int getFinalState() 285 { 286 return finalstate; 287 } 288 289 296 public boolean isFinalState(int state) 297 { 298 return finalstate==state; 299 } 300 301 306 public int getStateCount() 307 { 308 return statecount; 309 } 310 311 312 private static final String spaces = 313 " "; 314 315 320 public String toString() 321 { 322 String [] chars = new String [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 buffer = new StringBuffer (); 355 String 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 |