JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / bin / com / stevesoft / pat / Replacer.js
1 Clazz.declarePackage ("com.stevesoft.pat");\r
2 Clazz.load (null, ["com.stevesoft.pat.RegHolder", "$.Replacer", "$.CodeVal"], ["com.stevesoft.pat.AmpersandRule", "$.ChangeRule", "$.MessageManager", "$.PopRule", "$.PushRule", "$.RuleHolder", "$.SpecialRule", "$.StringBufferLike", "$.WantMoreTextReplaceRule", "com.stevesoft.pat.wrap.StringWrap", "java.lang.NullPointerException"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.me = null;\r
5 this.prev = null;\r
6 Clazz.instantialize (this, arguments);\r
7 }, com.stevesoft.pat, "RegHolder");\r
8 c$ = Clazz.decorateAsClass (function () {\r
9 this.pos = 0;\r
10 this.code = '\0';\r
11 Clazz.instantialize (this, arguments);\r
12 }, com.stevesoft.pat, "CodeVal");\r
13 Clazz.makeConstructor (c$, \r
14 function (p, c) {\r
15 this.pos = p;\r
16 this.code = c;\r
17 }, "~N,~S");\r
18 Clazz.overrideMethod (c$, "toString", \r
19 function () {\r
20 return "(" + this.pos + "," + this.code + ")";\r
21 });\r
22 c$ = Clazz.decorateAsClass (function () {\r
23 this.first = false;\r
24 this.rh = null;\r
25 this.sb = null;\r
26 this.src = null;\r
27 this.pos = 0;\r
28 this.want_more_text = false;\r
29 this.want_more_text_enable = false;\r
30 this.$lastMatchedTo = 0;\r
31 Clazz.instantialize (this, arguments);\r
32 }, com.stevesoft.pat, "Replacer");\r
33 Clazz.prepareFields (c$, function () {\r
34 this.rh =  new com.stevesoft.pat.RegHolder ();\r
35 });\r
36 Clazz.makeConstructor (c$, \r
37 function () {\r
38 });\r
39 Clazz.defineMethod (c$, "replaceFirstRegion", \r
40 function (s, r, start, end) {\r
41 return this.replaceFirstRegion ( new com.stevesoft.pat.wrap.StringWrap (s), r, start, end);\r
42 }, "~S,com.stevesoft.pat.Regex,~N,~N");\r
43 Clazz.defineMethod (c$, "replaceFirstRegion", \r
44 function (s, r, start, end) {\r
45 this.first = true;\r
46 this.rh.me = r;\r
47 this.rh.prev = null;\r
48 return this.dorep (s, start, end);\r
49 }, "com.stevesoft.pat.StringLike,com.stevesoft.pat.Regex,~N,~N");\r
50 Clazz.defineMethod (c$, "replaceFirst", \r
51 function (s) {\r
52 return this.replaceFirstRegion (s, 0, s.length ());\r
53 }, "com.stevesoft.pat.StringLike");\r
54 Clazz.defineMethod (c$, "replaceFirstFrom", \r
55 function (s, start) {\r
56 return this.replaceFirstRegion (s, start, s.length ());\r
57 }, "com.stevesoft.pat.StringLike,~N");\r
58 Clazz.defineMethod (c$, "replaceFirstRegion", \r
59 function (s, start, end) {\r
60 this.first = true;\r
61 return this.dorep (s, start, end);\r
62 }, "com.stevesoft.pat.StringLike,~N,~N");\r
63 Clazz.defineMethod (c$, "replaceAllRegion", \r
64 function (s, r, start, end) {\r
65 return this.replaceAllRegion ( new com.stevesoft.pat.wrap.StringWrap (s), r, start, end);\r
66 }, "~S,com.stevesoft.pat.Regex,~N,~N");\r
67 Clazz.defineMethod (c$, "replaceAllRegion", \r
68 function (s, r, start, end) {\r
69 this.first = false;\r
70 this.rh.me = r;\r
71 this.rh.prev = null;\r
72 return this.dorep (s, start, end);\r
73 }, "com.stevesoft.pat.StringLike,com.stevesoft.pat.Regex,~N,~N");\r
74 Clazz.defineMethod (c$, "replaceAll", \r
75 function (s) {\r
76 return this.replaceAllRegion (s, 0, s.length ());\r
77 }, "com.stevesoft.pat.StringLike");\r
78 Clazz.defineMethod (c$, "replaceAllFrom", \r
79 function (s, start) {\r
80 return this.replaceAllRegion (s, start, s.length ());\r
81 }, "com.stevesoft.pat.StringLike,~N");\r
82 Clazz.defineMethod (c$, "replaceAllRegion", \r
83 function (s, start, end) {\r
84 this.first = false;\r
85 return this.dorep (s, start, end);\r
86 }, "com.stevesoft.pat.StringLike,~N,~N");\r
87 Clazz.defineMethod (c$, "replaceAll", \r
88 function (s) {\r
89 return this.replaceAllRegion ( new com.stevesoft.pat.wrap.StringWrap (s), 0, s.length).toString ();\r
90 }, "~S");\r
91 Clazz.defineMethod (c$, "replaceAllFrom", \r
92 function (s, start) {\r
93 return this.replaceAllRegion ( new com.stevesoft.pat.wrap.StringWrap (s), start, s.length).toString ();\r
94 }, "~S,~N");\r
95 Clazz.defineMethod (c$, "replaceAllRegion", \r
96 function (s, start, end) {\r
97 this.first = false;\r
98 return this.dorep ( new com.stevesoft.pat.wrap.StringWrap (s), start, end).toString ();\r
99 }, "~S,~N,~N");\r
100 Clazz.defineMethod (c$, "isSpecial", \r
101 function (x) {\r
102 while (x != null) {\r
103 if (Clazz.instanceOf (x, com.stevesoft.pat.SpecialRule) || (Clazz.instanceOf (x, com.stevesoft.pat.RuleHolder) && Clazz.instanceOf ((x).held, com.stevesoft.pat.SpecialRule))) {\r
104 return true;\r
105 }x = x.next;\r
106 }\r
107 return false;\r
108 }, "com.stevesoft.pat.ReplaceRule");\r
109 Clazz.defineMethod (c$, "apply1", \r
110 function (rr) {\r
111 rr.charsMatched_++;\r
112 this.apply (rr, null);\r
113 rr.charsMatched_--;\r
114 }, "com.stevesoft.pat.RegRes");\r
115 Clazz.defineMethod (c$, "dorep", \r
116 function (s, start, end) {\r
117 var ret = s;\r
118 this.want_more_text = false;\r
119 this.$lastMatchedTo = 0;\r
120 if (this.rh.me == null) {\r
121 throw  new NullPointerException (com.stevesoft.pat.MessageManager.getString ("exception.replace_null_regex_pointer"));\r
122 }if (this.rh.me._searchLike (s, start, end)) {\r
123 var rmn = this.rh.me.matchedTo ();\r
124 if (this.rh.me.charsMatched () == 0 && !this.isSpecial (this.rh.me.getReplaceRule ())) {\r
125 this.apply1 (this.rh.me);\r
126 rmn++;\r
127 }this.apply (this.rh.me);\r
128 if (!this.first) {\r
129 for (var i = rmn; !this.want_more_text && this.rh.me._searchLike (s, i, end); i = rmn) {\r
130 rmn = this.rh.me.matchedTo ();\r
131 if (this.rh.me.charsMatched () == 0) {\r
132 if (!this.isSpecial (this.rh.me.getReplaceRule ())) {\r
133 this.apply1 (this.rh.me);\r
134 }rmn++;\r
135 }this.apply (this.rh.me);\r
136 }\r
137 }ret = this.finish ();\r
138 ret = ret == null ? s : ret;\r
139 }return ret;\r
140 }, "com.stevesoft.pat.StringLike,~N,~N");\r
141 Clazz.defineMethod (c$, "apply", \r
142 function (r, rp) {\r
143 if (rp == null || (rp.next == null && Clazz.instanceOf (rp, com.stevesoft.pat.AmpersandRule))) {\r
144 return;\r
145 }if (r.didMatch ()) {\r
146 if (this.src == null) {\r
147 this.src = r.getStringLike ();\r
148 }if (this.sb == null) {\r
149 this.sb =  new com.stevesoft.pat.StringBufferLike (this.src.newStringBufferLike ());\r
150 }var rmf = r.matchedFrom ();\r
151 for (var ii = this.pos; ii < rmf; ii++) {\r
152 this.sb.appendC (this.src.charAt (ii));\r
153 }\r
154 for (var x = rp; x != null; x = x.next) {\r
155 x.apply (this.sb, r);\r
156 if (Clazz.instanceOf (x, com.stevesoft.pat.SpecialRule)) {\r
157 if (Clazz.instanceOf (x, com.stevesoft.pat.WantMoreTextReplaceRule) && this.want_more_text_enable) {\r
158 this.want_more_text = true;\r
159 } else if (Clazz.instanceOf (x, com.stevesoft.pat.PushRule)) {\r
160 var rh2 =  new com.stevesoft.pat.RegHolder ();\r
161 rh2.me = (x).NewRule;\r
162 rh2.prev = this.rh;\r
163 this.rh = rh2;\r
164 } else if (Clazz.instanceOf (x, com.stevesoft.pat.PopRule)) {\r
165 if (this.rh.prev != null) {\r
166 this.rh = this.rh.prev;\r
167 }} else if (Clazz.instanceOf (x, com.stevesoft.pat.ChangeRule)) {\r
168 this.rh.me = (x).NewRule;\r
169 }}}\r
170 if (!this.want_more_text) {\r
171 this.pos = r.matchedTo ();\r
172 }}}, "com.stevesoft.pat.RegRes,com.stevesoft.pat.ReplaceRule");\r
173 Clazz.defineMethod (c$, "WantMoreText", \r
174 function () {\r
175 return this.want_more_text;\r
176 });\r
177 Clazz.defineMethod (c$, "apply", \r
178 function (r) {\r
179 this.apply (r, r.getReplaceRule ());\r
180 }, "com.stevesoft.pat.Regex");\r
181 Clazz.defineMethod (c$, "finish", \r
182 function () {\r
183 if (this.src == null) {\r
184 return null;\r
185 }var s_end = this.src.length ();\r
186 for (var ii = this.pos; ii < s_end; ii++) {\r
187 this.sb.appendC (this.src.charAt (ii));\r
188 }\r
189 this.src = null;\r
190 this.$lastMatchedTo = this.pos;\r
191 this.pos = 0;\r
192 var retstr = this.sb.toStringLike ();\r
193 this.sb = null;\r
194 return retstr;\r
195 });\r
196 Clazz.overrideMethod (c$, "clone", \r
197 function () {\r
198 var r =  new com.stevesoft.pat.Replacer ();\r
199 r.first = this.first;\r
200 r.src = this.src;\r
201 r.sb = this.sb;\r
202 r.pos = this.pos;\r
203 r.$lastMatchedTo = this.$lastMatchedTo;\r
204 r.want_more_text = this.want_more_text;\r
205 r.want_more_text_enable = this.want_more_text_enable;\r
206 r.rh.me = this.rh.me;\r
207 r.rh.prev = this.rh.prev;\r
208 return r;\r
209 });\r
210 Clazz.defineMethod (c$, "lastMatchedTo", \r
211 function () {\r
212 return this.$lastMatchedTo;\r
213 });\r
214 Clazz.defineMethod (c$, "getRegex", \r
215 function () {\r
216 return this.rh.me;\r
217 });\r
218 Clazz.defineMethod (c$, "setSource", \r
219 function (sl) {\r
220 this.src = sl;\r
221 }, "com.stevesoft.pat.StringLike");\r
222 Clazz.defineMethod (c$, "setBuffer", \r
223 function (sbl) {\r
224 this.sb = sbl;\r
225 }, "com.stevesoft.pat.StringBufferLike");\r
226 Clazz.defineMethod (c$, "setPos", \r
227 function (pos) {\r
228 this.pos = pos;\r
229 }, "~N");\r
230 });\r