JAL-1807 test
[jalviewjs.git] / bin / jalview / util / GroupUrlLink.js
1 Clazz.declarePackage ("jalview.util");
2 Clazz.load (["java.lang.Exception"], "jalview.util.GroupUrlLink", ["jalview.jsdev.RegExp", "jalview.util.MessageManager", "$.Platform", "$.QuickSort", "java.lang.Error", "$.StringBuffer", "java.util.Hashtable"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 if (!Clazz.isClassDefined ("jalview.util.GroupUrlLink.UrlStringTooLongException")) {
5 jalview.util.GroupUrlLink.$GroupUrlLink$UrlStringTooLongException$ ();
6 }
7 this.url_prefix = null;
8 this.target = null;
9 this.label = null;
10 this.url_suffix = null;
11 this.separators = null;
12 this.regexReplace = null;
13 this.invalidMessage = null;
14 this.segs = null;
15 this.mtch = null;
16 Clazz.instantialize (this, arguments);
17 }, jalview.util, "GroupUrlLink");
18 Clazz.makeConstructor (c$, 
19 function (link) {
20 var sep = link.indexOf ("|");
21 this.segs =  Clazz.newIntArray (jalview.util.GroupUrlLink.tokens.length, 0);
22 var ntoks = 0;
23 for (var i = 0; i < this.segs.length; i++) {
24 if ((this.segs[i] = link.indexOf ("$" + jalview.util.GroupUrlLink.tokens[i])) > -1) {
25 ntoks++;
26 }}
27 if (ntoks == 0) {
28 this.invalidMessage = "Group URL string must contain at least one of ";
29 for (var i = 0; i < this.segs.length; i++) {
30 this.invalidMessage += " '$" + jalview.util.GroupUrlLink.tokens[i] + "[=/regex=/]$'";
31 }
32 return;
33 }var ptok =  Clazz.newIntArray (ntoks + 1, 0);
34 var tmtch =  new Array (ntoks + 1);
35 this.mtch =  new Array (ntoks);
36 for (var i = 0, t = 0; i < this.segs.length; i++) {
37 if (this.segs[i] > -1) {
38 ptok[t] = this.segs[i];
39 tmtch[t++] = jalview.util.GroupUrlLink.tokens[i];
40 }}
41 ptok[ntoks] = link.length;
42 tmtch[ntoks] = "$$$$$$$$$";
43 jalview.util.QuickSort.sortInt (ptok, tmtch);
44 for (var i = 0; i < ntoks; i++) {
45 this.mtch[i] = tmtch[i];
46 }
47 var p = sep;
48 do {
49 sep = p;
50 p = link.indexOf ("|", sep + 1);
51 } while (p > sep && p < ptok[0]);
52 this.label = link.substring (0, sep);
53 if (this.label.indexOf ("|") > -1) {
54 this.target = this.label.substring (0, this.label.indexOf ("|"));
55 } else if (this.label.indexOf (" ") > 2) {
56 this.target = this.label.substring (0, this.label.indexOf (" "));
57 } else {
58 this.target = this.label;
59 }this.url_prefix = link.substring (sep + 1, ptok[0]);
60 this.url_suffix =  new Array (this.mtch.length);
61 this.regexReplace =  new Array (this.mtch.length);
62 for (var pass = 0; pass < this.mtch.length; pass++) {
63 var mlength = 3 + this.mtch[pass].length;
64 if (link.indexOf ("$" + this.mtch[pass] + "=/") == ptok[pass] && (p = link.indexOf ("/=$", ptok[pass] + mlength)) > ptok[pass] + mlength) {
65 if (ptok[pass + 1] < p + 3) {
66 this.invalidMessage = "Token regexes cannot contain other regexes (did you terminate the $" + this.mtch[pass] + " regex with a '/=$' ?";
67 return;
68 }this.url_suffix[pass] = link.substring (p + 3, ptok[pass + 1]);
69 this.regexReplace[pass] = link.substring (ptok[pass] + mlength, p);
70 try {
71 var rg = jalview.jsdev.RegExp.perlCode ("/" + this.regexReplace[pass] + "/");
72 if (rg == null) {
73 this.invalidMessage = "Invalid Regular Expression : '" + this.regexReplace[pass] + "'\n";
74 }} catch (e) {
75 if (Clazz.exceptionOf (e, Exception)) {
76 this.invalidMessage = "Invalid Regular Expression : '" + this.regexReplace[pass] + "'\n";
77 } else {
78 throw e;
79 }
80 }
81 } else {
82 this.regexReplace[pass] = null;
83 if ((p = link.indexOf ("$" + this.mtch[pass] + "$")) == ptok[pass]) {
84 this.url_suffix[pass] = link.substring (p + this.mtch[pass].length + 2, ptok[pass + 1]);
85 } else {
86 this.invalidMessage = "Warning: invalid regex structure (after '" + this.mtch[0] + "') for URL link : " + link;
87 }}}
88 var pass = 0;
89 this.separators =  new Array (this.url_suffix.length);
90 var suffices = this.url_suffix[this.url_suffix.length - 1];
91 var lastsep = ",";
92 while ((p = suffices.indexOf ('|')) > -1) {
93 this.separators[pass] = suffices.substring (p + 1);
94 if (pass == 0) {
95 this.url_suffix[this.url_suffix.length - 1] = suffices.substring (0, p);
96 } else {
97 lastsep = (this.separators[pass - 1] = this.separators[pass - 1].substring (0, p));
98 }suffices = this.separators[pass];
99 pass++;
100 }
101 if (pass > 0) {
102 lastsep = this.separators[pass - 1];
103 }while (pass < this.separators.length) {
104 this.separators[pass++] = lastsep;
105 }
106 }, "~S");
107 Clazz.defineMethod (c$, "getUrl_suffix", 
108 function () {
109 return this.url_suffix[this.url_suffix.length - 1];
110 });
111 Clazz.defineMethod (c$, "getUrl_prefix", 
112 function () {
113 return this.url_prefix;
114 });
115 Clazz.defineMethod (c$, "getTarget", 
116 function () {
117 return this.target;
118 });
119 Clazz.defineMethod (c$, "getLabel", 
120 function () {
121 return this.label;
122 });
123 Clazz.defineMethod (c$, "getIDRegexReplace", 
124 function () {
125 return this._replaceFor (jalview.util.GroupUrlLink.tokens[0]);
126 });
127 Clazz.defineMethod (c$, "_replaceFor", 
128 ($fz = function (token) {
129 for (var i = 0; i < this.mtch.length; i++) {
130 if (this.segs[i] > -1 && this.mtch[i].equals (token)) {
131 return this.regexReplace[i];
132 }}
133 return null;
134 }, $fz.isPrivate = true, $fz), "~S");
135 Clazz.defineMethod (c$, "getSeqRegexReplace", 
136 function () {
137 return this._replaceFor (jalview.util.GroupUrlLink.tokens[1]);
138 });
139 Clazz.defineMethod (c$, "getInvalidMessage", 
140 function () {
141 return this.invalidMessage;
142 });
143 Clazz.defineMethod (c$, "isValid", 
144 function () {
145 return this.invalidMessage == null;
146 });
147 Clazz.defineMethod (c$, "makeUrls", 
148 function (idstrings, seqstrings, dsstring, onlyIfMatches) {
149 var rstrings = this.replacementArgs (idstrings, seqstrings, dsstring);
150 return this.makeUrls (rstrings, onlyIfMatches);
151 }, "~A,~A,~S,~B");
152 Clazz.defineMethod (c$, "replacementArgs", 
153 ($fz = function (idstrings, seqstrings, dsstring) {
154 var rstrings =  new java.util.Hashtable ();
155 rstrings.put (jalview.util.GroupUrlLink.tokens[0], idstrings);
156 rstrings.put (jalview.util.GroupUrlLink.tokens[1], seqstrings);
157 rstrings.put (jalview.util.GroupUrlLink.tokens[2],  Clazz.newArray (-1, [dsstring]));
158 if (idstrings.length != seqstrings.length) {
159 throw  new Error (jalview.util.MessageManager.getString ("error.idstring_seqstrings_only_one_per_sequence"));
160 }return rstrings;
161 }, $fz.isPrivate = true, $fz), "~A,~A,~S");
162 Clazz.defineMethod (c$, "makeUrls", 
163 function (repstrings, onlyIfMatches) {
164 return this.makeUrlsIf (true, repstrings, onlyIfMatches);
165 }, "java.util.Hashtable,~B");
166 Clazz.defineMethod (c$, "makeUrlStubs", 
167 function (ids, seqstr, string, b) {
168 var rstrings = this.replacementArgs (ids, seqstr, string);
169 var stubs = this.makeUrlsIf (false, rstrings, b);
170 if (stubs != null) {
171 return  Clazz.newArray (-1, [stubs[0], stubs[1], rstrings,  Clazz.newBooleanArray (-1, [b])]);
172 }return null;
173 }, "~A,~A,~S,~B");
174 Clazz.defineMethod (c$, "constructFrom", 
175 function (stubs) {
176 var results = this.makeUrlsIf (true, stubs[2], (stubs[3])[0]);
177 return (results[3])[0];
178 }, "~A");
179 Clazz.defineMethod (c$, "makeUrlsIf", 
180 function (createFullUrl, repstrings, onlyIfMatches) {
181 var pass = 0;
182 var idseq =  new Array (this.mtch.length);
183 var mins = 0;
184 var maxs = 0;
185 for (var i = 0; i < this.mtch.length; i++) {
186 idseq[i] = repstrings.get (this.mtch[i]);
187 if (idseq[i].length >= 1) {
188 if (mins == 0 && idseq[i].length == 1) {
189 mins = 1;
190 }if (maxs < 2) {
191 maxs = idseq[i].length;
192 } else {
193 if (maxs != idseq[i].length) {
194 throw  new Error (jalview.util.MessageManager.formatMessage ("error.cannot_have_mixed_length_replacement_vectors",  Clazz.newArray (-1, [(this.mtch[i]), Integer.$valueOf (idseq[i].length).toString (), Integer.$valueOf (maxs).toString ()])));
195 }}} else {
196 throw  new Error (jalview.util.MessageManager.getString ("error.cannot_have_zero_length_vector_replacement_strings"));
197 }}
198 var matched =  new Array (idseq.length);
199 var rgxs =  new Array (matched.length);
200 for (pass = 0; pass < matched.length; pass++) {
201 matched[pass] =  new StringBuffer ();
202 if (this.regexReplace[pass] != null) {
203 rgxs[pass] = jalview.jsdev.RegExp.perlCode ("/" + this.regexReplace[pass] + "/");
204 } else {
205 rgxs[pass] = null;
206 }}
207 var urllength = this.url_prefix.length;
208 for (pass = 0; pass < matched.length; pass++) {
209 urllength += this.url_suffix[pass].length;
210 }
211 var thismatched =  Clazz.newBooleanArray (maxs, false);
212 var seqsmatched = 0;
213 for (var sq = 0; sq < maxs; sq++) {
214 thismatched[sq] = false;
215 var thematches =  new Array (rgxs.length);
216 for (pass = 0; pass < rgxs.length; pass++) {
217 thematches[pass] =  new StringBuffer ();
218 if (idseq[pass].length <= sq) {
219 continue;
220 }if (rgxs[pass] != null) {
221 var rg = rgxs[pass];
222 var rematchat = 0;
223 while (rg.searchFrom (idseq[pass][sq], rematchat)) {
224 rematchat = rg.matchedTo ();
225 thismatched[sq] = new Boolean (thismatched[sq] | true).valueOf ();
226 urllength += rg.charsMatched ();
227 if ((urllength + 32) > jalview.util.Platform.getMaxCommandLineLength ()) {
228 throw Clazz.innerTypeInstance (jalview.util.GroupUrlLink.UrlStringTooLongException, this, null, urllength);
229 }if (!createFullUrl) {
230 continue;
231 }var ns = rg.numSubs ();
232 if (ns == 0) {
233 thematches[pass].append (rg.stringMatched ());
234 } else {
235 var subs =  new StringBuffer ();
236 var s = 0;
237 while (s <= ns) {
238 if (s + 1 <= ns && rg.matchedToI (s) > -1 && rg.matchedToI (s + 1) > -1 && rg.matchedToI (s + 1) < rg.matchedToI (s)) {
239 var r = s + 1;
240 var rmtch =  new StringBuffer ();
241 while (r <= ns && rg.matchedToI (r) <= rg.matchedToI (s)) {
242 if (rg.matchedFromI (r) > -1) {
243 rmtch.append (rg.stringMatchedI (r));
244 }r++;
245 }
246 if (rmtch.length () > 0) {
247 subs.append (rmtch);
248 }s = r;
249 } else {
250 if (rg.matchedFromI (s) > -1) {
251 subs.append (rg.stringMatchedI (s));
252 }s++;
253 }}
254 thematches[pass].append (subs);
255 }}
256 } else {
257 if (!onlyIfMatches) {
258 thismatched[sq] = new Boolean (thismatched[sq] | true).valueOf ();
259 urllength += idseq[pass][sq].length;
260 if (createFullUrl) {
261 thematches[pass] =  new StringBuffer (idseq[pass][sq]);
262 }}}}
263 if (thismatched[sq]) {
264 if (createFullUrl) {
265 for (pass = 0; pass < matched.length; pass++) {
266 if (idseq[pass].length > 1 && matched[pass].length () > 0) {
267 matched[pass].append (this.separators[pass]);
268 }matched[pass].append (thematches[pass]);
269 }
270 }seqsmatched++;
271 }}
272 if (seqsmatched == 0 || (createFullUrl && matched[0].length () == 0)) {
273 return null;
274 }if ((urllength + 32) > jalview.util.Platform.getMaxCommandLineLength ()) {
275 throw Clazz.innerTypeInstance (jalview.util.GroupUrlLink.UrlStringTooLongException, this, null, urllength);
276 }if (!createFullUrl) {
277 return  Clazz.newArray (-1, [ Clazz.newIntArray (-1, [seqsmatched]), thismatched]);
278 }var submiturl =  new StringBuffer ();
279 submiturl.append (this.url_prefix);
280 for (pass = 0; pass < matched.length; pass++) {
281 submiturl.append (matched[pass]);
282 if (this.url_suffix[pass] != null) {
283 submiturl.append (this.url_suffix[pass]);
284 }}
285 return  Clazz.newArray (-1, [ Clazz.newIntArray (-1, [seqsmatched]), thismatched, matched,  Clazz.newArray (-1, [submiturl.toString ()])]);
286 }, "~B,java.util.Hashtable,~B");
287 Clazz.defineMethod (c$, "getNumberInvolved", 
288 function (urlstub) {
289 return (urlstub[0])[0];
290 }, "~A");
291 Clazz.defineMethod (c$, "getGroupURLType", 
292 function () {
293 var r = 0;
294 for (var pass = 0; pass < jalview.util.GroupUrlLink.tokens.length; pass++) {
295 for (var i = 0; i < this.mtch.length; i++) {
296 if (this.mtch[i].equals (jalview.util.GroupUrlLink.tokens[pass])) {
297 r += 1 << pass;
298 }}
299 }
300 return r;
301 });
302 Clazz.overrideMethod (c$, "toString", 
303 function () {
304 var result =  new StringBuffer ();
305 result.append (this.label + "|" + this.url_prefix);
306 var r;
307 for (r = 0; r < this.url_suffix.length; r++) {
308 result.append ("$");
309 result.append (this.mtch[r]);
310 if (this.regexReplace[r] != null) {
311 result.append ("=/");
312 result.append (this.regexReplace[r]);
313 result.append ("/=");
314 }result.append ("$");
315 result.append (this.url_suffix[r]);
316 }
317 for (r = 0; r < this.separators.length; r++) {
318 result.append ("|");
319 result.append (this.separators[r]);
320 }
321 return result.toString ();
322 });
323 c$.formStrings = Clazz.defineMethod (c$, "formStrings", 
324 function (seqs) {
325 var idset =  Clazz.newArray (2, seqs.length, null);
326 for (var i = 0; i < seqs.length; i++) {
327 idset[0][i] = seqs[i].getName ();
328 idset[1][i] = seqs[i].getSequenceAsString ();
329 }
330 return idset;
331 }, "~A");
332 Clazz.defineMethod (c$, "setLabel", 
333 function (newlabel) {
334 this.label = newlabel;
335 }, "~S");
336 c$.$GroupUrlLink$UrlStringTooLongException$ = function () {
337 Clazz.pu$h ();
338 c$ = Clazz.decorateAsClass (function () {
339 Clazz.prepareCallback (this, arguments);
340 this.urlLength = 0;
341 Clazz.instantialize (this, arguments);
342 }, jalview.util.GroupUrlLink, "UrlStringTooLongException", Exception);
343 Clazz.makeConstructor (c$, 
344 function (a) {
345 Clazz.superConstructor (this, jalview.util.GroupUrlLink.UrlStringTooLongException, []);
346 this.urlLength = a;
347 }, "~N");
348 Clazz.overrideMethod (c$, "toString", 
349 function () {
350 return "Generated url is estimated to be too long (" + this.urlLength + ")";
351 });
352 c$ = Clazz.p0p ();
353 };
354 Clazz.defineStatics (c$,
355 "tokens", null);
356 {
357 if (jalview.util.GroupUrlLink.tokens == null) {
358 jalview.util.GroupUrlLink.tokens =  Clazz.newArray (-1, ["SEQUENCEIDS", "SEQUENCES", "DATASETID"]);
359 }}Clazz.defineStatics (c$,
360 "SEQUENCEIDS", 1,
361 "SEQUENCES", 2,
362 "DATASETID", 4);
363 });