annotate src/howtouse/CharReader.java @ 17:e30f2714686b draft

fix CharReader.java
author one
date Thu, 23 Aug 2012 17:36:46 +0900
parents bf7863a55cd6
children 2c3a10047ec6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
1 package howtouse;
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
2
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
3
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
4 public class CharReader {
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
5
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
6 final static char EOFchar = (char) 0;
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
7
9
d655c8fef734 fix WikiLinkParser.java
one
parents: 8
diff changeset
8 private String text;
d655c8fef734 fix WikiLinkParser.java
one
parents: 8
diff changeset
9 private int textLength;
8
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
10 private int index;
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
11
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
12 private final char LBRANK = '[';
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
13 private final char RBRANK = ']';
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
14 private final char VERBAR = '|';
17
e30f2714686b fix CharReader.java
one
parents: 12
diff changeset
15 private final char COLON= ':';
8
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
16
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
17
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
18 CharReader() {
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
19 }
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
20
9
d655c8fef734 fix WikiLinkParser.java
one
parents: 8
diff changeset
21 public void setText(String str) {
d655c8fef734 fix WikiLinkParser.java
one
parents: 8
diff changeset
22 text = str;
d655c8fef734 fix WikiLinkParser.java
one
parents: 8
diff changeset
23 textLength = text.length();
d655c8fef734 fix WikiLinkParser.java
one
parents: 8
diff changeset
24 index = 0;
d655c8fef734 fix WikiLinkParser.java
one
parents: 8
diff changeset
25 }
8
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
26
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
27
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
28 char nextChar() {
9
d655c8fef734 fix WikiLinkParser.java
one
parents: 8
diff changeset
29 if (index < textLength)
d655c8fef734 fix WikiLinkParser.java
one
parents: 8
diff changeset
30 return text.charAt(index++);
8
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
31
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
32 return EOFchar;
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
33
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
34 }
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
35
9
d655c8fef734 fix WikiLinkParser.java
one
parents: 8
diff changeset
36 String getToken() {
8
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
37
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
38 int nextState = 1;
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
39
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
40 StringBuffer buf = new StringBuffer(256);
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
41 char ch;
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
42 int index = -1;
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
43 while (true) {
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
44 ch = nextChar();
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
45 if (ch == EOFchar) return null;
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
46 switch (nextState) {
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
47 case 1:
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
48 if (ch == LBRANK)
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
49 nextState = 2;
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
50 break;
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
51 case 2:
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
52 if (ch == LBRANK)
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
53 nextState = 3;
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
54 else
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
55 nextState = 1;
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
56 break;
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
57 case 3:
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
58 if (ch == RBRANK) {
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
59 nextState = 4;
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
60 } else if (ch == VERBAR) {
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
61 index = buf.length();
17
e30f2714686b fix CharReader.java
one
parents: 12
diff changeset
62 buf.append(ch);
e30f2714686b fix CharReader.java
one
parents: 12
diff changeset
63 return buf.substring(0,index);
e30f2714686b fix CharReader.java
one
parents: 12
diff changeset
64 } else if (ch == COLON) {
e30f2714686b fix CharReader.java
one
parents: 12
diff changeset
65 index = 0;
e30f2714686b fix CharReader.java
one
parents: 12
diff changeset
66 buf.delete(0,buf.length());
8
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
67 } else {
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
68 buf.append(ch);
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
69 }
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
70 break;
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
71 case 4:
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
72 if (ch == RBRANK) {
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
73 if (index == -1) {
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
74 return buf.toString();
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
75 } else{
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
76 return buf.substring(0,index);
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
77 }
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
78 } else {
9
d655c8fef734 fix WikiLinkParser.java
one
parents: 8
diff changeset
79 return null;
8
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
80 }
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
81 }
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
82
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
83 }
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
84
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
85 }
d8d0855bcdfd add CharReader.java , WikiLinkParser.java
one
parents:
diff changeset
86 }