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