JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / bin / com / stevesoft / pat / RegexWriter.js
1 Clazz.declarePackage ("com.stevesoft.pat");\r
2 Clazz.load (["java.io.Writer", "com.stevesoft.pat.PartialBuffer", "java.lang.StringBuffer"], "com.stevesoft.pat.RegexWriter", ["com.stevesoft.pat.StringBufferLike", "com.stevesoft.pat.wrap.WriterWrap"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.repr = null;\r
5 this.w = null;\r
6 this.ww = null;\r
7 this.sb = null;\r
8 this.wrap = null;\r
9 this.pos = 0;\r
10 this.epos = 0;\r
11 this.interval = 128;\r
12 this.bufferSize = 2048;\r
13 this.EOLchar = '\n';\r
14 this.max_lines = 2;\r
15 Clazz.instantialize (this, arguments);\r
16 }, com.stevesoft.pat, "RegexWriter", java.io.Writer);\r
17 Clazz.prepareFields (c$, function () {\r
18 this.sb =  new StringBuffer ();\r
19 this.wrap =  new com.stevesoft.pat.PartialBuffer (this.sb);\r
20 });\r
21 Clazz.makeConstructor (c$, \r
22 function (t, w) {\r
23 Clazz.superConstructor (this, com.stevesoft.pat.RegexWriter, []);\r
24 this.w = w;\r
25 this.ww =  new com.stevesoft.pat.wrap.WriterWrap (w);\r
26 this.repr = t.getReplacer ();\r
27 this.repr.setBuffer ( new com.stevesoft.pat.StringBufferLike (this.ww));\r
28 this.repr.setSource (this.wrap);\r
29 }, "com.stevesoft.pat.Transformer,java.io.Writer");\r
30 Clazz.makeConstructor (c$, \r
31 function (r, w) {\r
32 Clazz.superConstructor (this, com.stevesoft.pat.RegexWriter, []);\r
33 this.w = w;\r
34 this.ww =  new com.stevesoft.pat.wrap.WriterWrap (w);\r
35 this.repr = r.getReplacer ();\r
36 this.repr.setBuffer ( new com.stevesoft.pat.StringBufferLike (this.ww));\r
37 this.repr.setSource (this.wrap);\r
38 }, "com.stevesoft.pat.Regex,java.io.Writer");\r
39 Clazz.defineMethod (c$, "getEOLchar", \r
40 function () {\r
41 return this.EOLchar;\r
42 });\r
43 Clazz.defineMethod (c$, "setEOLchar", \r
44 function (c) {\r
45 this.EOLchar = c;\r
46 }, "~S");\r
47 Clazz.defineMethod (c$, "getMaxLines", \r
48 function () {\r
49 return this.max_lines;\r
50 });\r
51 Clazz.defineMethod (c$, "setMaxLines", \r
52 function (ml) {\r
53 this.max_lines = ml;\r
54 }, "~N");\r
55 Clazz.defineMethod (c$, "write", \r
56 function () {\r
57 var rex = this.repr.getRegex ();\r
58 var eposOld = this.epos;\r
59 if (rex.matchAtLike (this.wrap, this.epos) && !this.wrap.overRun) {\r
60 while (this.pos < this.epos) {\r
61 this.w.write (this.sb.charCodeAt (this.pos++));\r
62 }\r
63 var to = rex.matchedTo ();\r
64 this.repr.setPos (to);\r
65 this.repr.apply (rex, rex.getReplaceRule ());\r
66 this.epos = this.pos = to;\r
67 if (this.epos == eposOld && this.epos < this.sb.length ()) {\r
68 this.epos++;\r
69 }} else if (!this.wrap.overRun && this.epos < this.sb.length ()) {\r
70 this.epos++;\r
71 }while (this.pos < this.epos) {\r
72 this.w.write (this.sb.charCodeAt (this.pos++));\r
73 }\r
74 if (this.epos == this.sb.length ()) {\r
75 this.sb.setLength (1);\r
76 this.pos = this.epos = 1;\r
77 } else if (this.pos > this.bufferSize) {\r
78 for (var i = this.bufferSize; i < this.sb.length (); i++) {\r
79 this.sb.setCharAt (i - this.bufferSize, this.sb.charAt (i));\r
80 }\r
81 this.pos -= this.bufferSize;\r
82 this.epos -= this.bufferSize;\r
83 this.sb.setLength (this.sb.length () - this.bufferSize);\r
84 }});\r
85 Clazz.defineMethod (c$, "write", \r
86 function (ca, b, n) {\r
87 var m = b + n;\r
88 for (var i = b; i < m; i++) {\r
89 this.sb.append (ca[i]);\r
90 if (this.sb.length () % this.interval == this.interval - 1) {\r
91 this.wrap.overRun = false;\r
92 while (this.epos + this.interval < this.sb.length () && !this.wrap.overRun) {\r
93 this.write ();\r
94 }\r
95 }}\r
96 }, "~A,~N,~N");\r
97 Clazz.overrideMethod (c$, "flush", \r
98 function () {\r
99 });\r
100 Clazz.defineMethod (c$, "close", \r
101 function () {\r
102 this.wrap.allowOverRun = false;\r
103 this.wrap.overRun = false;\r
104 while (this.epos < this.sb.length ()) {\r
105 this.write ();\r
106 }\r
107 this.write ();\r
108 this.w.close ();\r
109 });\r
110 Clazz.defineMethod (c$, "length", \r
111 function () {\r
112 return this.sb.length ();\r
113 });\r
114 Clazz.defineMethod (c$, "charAt", \r
115 function (i) {\r
116 return this.sb.charAt (i);\r
117 }, "~N");\r
118 Clazz.defineMethod (c$, "setInterval", \r
119 function (i) {\r
120 this.interval = i;\r
121 }, "~N");\r
122 Clazz.defineMethod (c$, "getInterval", \r
123 function () {\r
124 return this.interval;\r
125 });\r
126 Clazz.defineMethod (c$, "getBufferSize", \r
127 function () {\r
128 return this.bufferSize;\r
129 });\r
130 Clazz.defineMethod (c$, "setBufferSize", \r
131 function (i) {\r
132 this.bufferSize = i;\r
133 }, "~N");\r
134 });\r