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