JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / site / j2s / com / stevesoft / pat / RegexReader.js
1 Clazz.declarePackage ("com.stevesoft.pat");\r
2 Clazz.load (["java.io.Reader", "com.stevesoft.pat.PartialBuffer", "$.RBuffer", "java.lang.StringBuffer"], "com.stevesoft.pat.RegexReader", ["com.stevesoft.pat.StringBufferLike", "com.stevesoft.pat.wrap.StringBufferWrap"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.rb = null;\r
5 this.wrap = null;\r
6 this.moreToRead = true;\r
7 this.r = null;\r
8 this.rp = null;\r
9 this.nmax = 2048;\r
10 this.max_lines = 2;\r
11 this.EOLchar = '\n';\r
12 Clazz.instantialize (this, arguments);\r
13 }, com.stevesoft.pat, "RegexReader", java.io.Reader);\r
14 Clazz.prepareFields (c$, function () {\r
15 this.rb =  new com.stevesoft.pat.RBuffer ( new StringBuffer ());\r
16 this.wrap =  new com.stevesoft.pat.PartialBuffer (this.rb.sb);\r
17 });\r
18 Clazz.makeConstructor (c$, \r
19 function (rex, r) {\r
20 Clazz.superConstructor (this, com.stevesoft.pat.RegexReader, []);\r
21 this.r = r;\r
22 this.rp = rex.getReplacer ();\r
23 }, "com.stevesoft.pat.Regex,java.io.Reader");\r
24 Clazz.makeConstructor (c$, \r
25 function (tex, r) {\r
26 Clazz.superConstructor (this, com.stevesoft.pat.RegexReader, []);\r
27 this.r = r;\r
28 this.rp = tex.getReplacer ();\r
29 }, "com.stevesoft.pat.Transformer,java.io.Reader");\r
30 Clazz.defineMethod (c$, "reset", \r
31 function () {\r
32 this.r.reset ();\r
33 this.rb =  new com.stevesoft.pat.RBuffer ( new StringBuffer ());\r
34 this.wrap =  new com.stevesoft.pat.PartialBuffer (this.rb.sb);\r
35 this.moreToRead = true;\r
36 });\r
37 Clazz.defineMethod (c$, "readData", \r
38 function () {\r
39 var c;\r
40 var n = 0;\r
41 while ((c = this.r.read ()) != -1) {\r
42 this.rb.sb.append (String.fromCharCode (c));\r
43 if (n++ > this.nmax) {\r
44 break;\r
45 }}\r
46 if (c == -1 && n == 0) {\r
47 this.moreToRead = false;\r
48 this.wrap.allowOverRun = false;\r
49 }});\r
50 Clazz.defineMethod (c$, "getMoreData", \r
51 function () {\r
52 while (this.rb.pos >= this.rb.epos) {\r
53 this.wrap.overRun = false;\r
54 if (this.rb.next != null) {\r
55 this.rb = this.rb.next;\r
56 } else if (this.rb.done) {\r
57 break;\r
58 } else if (this.rb.epos >= this.rb.sb.length () && this.rb.epos > this.nmax) {\r
59 this.rb.pos = 1;\r
60 this.rb.epos = 1;\r
61 this.rb.sb.setLength (1);\r
62 this.readData ();\r
63 } else if (this.rb.epos >= this.rb.sb.length () && this.moreToRead) {\r
64 this.readData ();\r
65 } else if (this.rp.getRegex ().matchAtLike (this.wrap, this.rb.epos)) {\r
66 if (this.wrap.overRun) {\r
67 this.readData ();\r
68 } else {\r
69 var sbw =  new com.stevesoft.pat.wrap.StringBufferWrap ();\r
70 var sbl =  new com.stevesoft.pat.StringBufferLike (sbw);\r
71 var rex = this.rp.getRegex ();\r
72 var npos = rex.matchedTo ();\r
73 this.rp.setBuffer (sbl);\r
74 this.rp.setSource (this.wrap);\r
75 this.rp.setPos (npos);\r
76 this.rp.apply (rex, rex.getReplaceRule ());\r
77 var opos = this.rb.epos;\r
78 var rb2 =  new com.stevesoft.pat.RBuffer (sbw.unwrap ());\r
79 rb2.epos = rb2.sb.length ();\r
80 var rb3 =  new com.stevesoft.pat.RBuffer (this.rb.sb);\r
81 this.rb.next = rb2;\r
82 rb2.next = rb3;\r
83 if (npos == opos) {\r
84 rb3.epos = npos + 1;\r
85 if (rb3.epos > rb3.sb.length ()) {\r
86 if (this.rb.pos >= this.rb.epos) {\r
87 this.rb = this.rb.next;\r
88 }rb3.pos = rb3.epos = 0;\r
89 rb3.done = true;\r
90 }rb3.pos = npos;\r
91 } else {\r
92 rb3.pos = rb3.epos = npos;\r
93 }}} else {\r
94 if (this.wrap.overRun) {\r
95 this.readData ();\r
96 } else if (this.rb.epos < this.rb.sb.length ()) {\r
97 this.rb.epos++;\r
98 } else {\r
99 break;\r
100 }}}\r
101 });\r
102 Clazz.defineMethod (c$, "read", \r
103 function () {\r
104 if (this.rb.pos >= this.rb.epos) {\r
105 this.getMoreData ();\r
106 if (this.rb.pos >= this.rb.epos) {\r
107 return -1;\r
108 }}return this.rb.sb.charAt (this.rb.pos++);\r
109 });\r
110 Clazz.defineMethod (c$, "read", \r
111 function (buf, off, len) {\r
112 var c = -1;\r
113 var end = off + len;\r
114 for (var i = off; i < end; i++) {\r
115 c = this.read ();\r
116 if (c < 0) {\r
117 if (i == off) {\r
118 return -1;\r
119 }return i - off;\r
120 }buf[i] = String.fromCharCode (c);\r
121 }\r
122 return len;\r
123 }, "~A,~N,~N");\r
124 Clazz.defineMethod (c$, "close", \r
125 function () {\r
126 this.r.close ();\r
127 });\r
128 Clazz.overrideMethod (c$, "markSupported", \r
129 function () {\r
130 return false;\r
131 });\r
132 Clazz.defineMethod (c$, "getBufferSize", \r
133 function () {\r
134 return this.nmax;\r
135 });\r
136 Clazz.defineMethod (c$, "setBufferSize", \r
137 function (n) {\r
138 this.nmax = n;\r
139 }, "~N");\r
140 Clazz.defineMethod (c$, "getMaxLines", \r
141 function () {\r
142 return this.max_lines;\r
143 });\r
144 Clazz.defineMethod (c$, "setMaxLines", \r
145 function (ml) {\r
146 this.max_lines = ml;\r
147 }, "~N");\r
148 Clazz.defineMethod (c$, "getEOLchar", \r
149 function () {\r
150 return this.EOLchar;\r
151 });\r
152 Clazz.defineMethod (c$, "setEOLchar", \r
153 function (c) {\r
154 this.EOLchar = c;\r
155 }, "~S");\r
156 Clazz.overrideMethod (c$, "skip", \r
157 function (d) {\r
158 var n = 0;\r
159 while (n < d && this.read () != -1) {\r
160 n++;\r
161 }\r
162 return n;\r
163 }, "~N");\r
164 });\r