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