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$ ();
7 this.url_prefix = null;
10 this.url_suffix = null;
11 this.separators = null;
12 this.regexReplace = null;
13 this.invalidMessage = null;
16 Clazz.instantialize (this, arguments);
17 }, jalview.util, "GroupUrlLink");
18 Clazz.makeConstructor (c$,
20 var sep = link.indexOf ("|");
21 this.segs = Clazz.newIntArray (jalview.util.GroupUrlLink.tokens.length, 0);
23 for (var i = 0; i < this.segs.length; i++) {
24 if ((this.segs[i] = link.indexOf ("$" + jalview.util.GroupUrlLink.tokens[i])) > -1) {
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=/]$'";
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];
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];
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 (" "));
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 '/=$' ?";
68 }this.url_suffix[pass] = link.substring (p + 3, ptok[pass + 1]);
69 this.regexReplace[pass] = link.substring (ptok[pass] + mlength, p);
71 var rg = jalview.jsdev.RegExp.perlCode ("/" + this.regexReplace[pass] + "/");
73 this.invalidMessage = "Invalid Regular Expression : '" + this.regexReplace[pass] + "'\n";
75 if (Clazz.exceptionOf (e, Exception)) {
76 this.invalidMessage = "Invalid Regular Expression : '" + this.regexReplace[pass] + "'\n";
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]);
86 this.invalidMessage = "Warning: invalid regex structure (after '" + this.mtch[0] + "') for URL link : " + link;
89 this.separators = new Array (this.url_suffix.length);
90 var suffices = this.url_suffix[this.url_suffix.length - 1];
92 while ((p = suffices.indexOf ('|')) > -1) {
93 this.separators[pass] = suffices.substring (p + 1);
95 this.url_suffix[this.url_suffix.length - 1] = suffices.substring (0, p);
97 lastsep = (this.separators[pass - 1] = this.separators[pass - 1].substring (0, p));
98 }suffices = this.separators[pass];
102 lastsep = this.separators[pass - 1];
103 }while (pass < this.separators.length) {
104 this.separators[pass++] = lastsep;
107 Clazz.defineMethod (c$, "getUrl_suffix",
109 return this.url_suffix[this.url_suffix.length - 1];
111 Clazz.defineMethod (c$, "getUrl_prefix",
113 return this.url_prefix;
115 Clazz.defineMethod (c$, "getTarget",
119 Clazz.defineMethod (c$, "getLabel",
123 Clazz.defineMethod (c$, "getIDRegexReplace",
125 return this._replaceFor (jalview.util.GroupUrlLink.tokens[0]);
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];
134 }, $fz.isPrivate = true, $fz), "~S");
135 Clazz.defineMethod (c$, "getSeqRegexReplace",
137 return this._replaceFor (jalview.util.GroupUrlLink.tokens[1]);
139 Clazz.defineMethod (c$, "getInvalidMessage",
141 return this.invalidMessage;
143 Clazz.defineMethod (c$, "isValid",
145 return this.invalidMessage == null;
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);
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"));
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);
171 return Clazz.newArray (-1, [stubs[0], stubs[1], rstrings, Clazz.newBooleanArray (-1, [b])]);
174 Clazz.defineMethod (c$, "constructFrom",
176 var results = this.makeUrlsIf (true, stubs[2], (stubs[3])[0]);
177 return (results[3])[0];
179 Clazz.defineMethod (c$, "makeUrlsIf",
180 function (createFullUrl, repstrings, onlyIfMatches) {
182 var idseq = new Array (this.mtch.length);
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) {
191 maxs = idseq[i].length;
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 ()])));
196 throw new Error (jalview.util.MessageManager.getString ("error.cannot_have_zero_length_vector_replacement_strings"));
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] + "/");
207 var urllength = this.url_prefix.length;
208 for (pass = 0; pass < matched.length; pass++) {
209 urllength += this.url_suffix[pass].length;
211 var thismatched = Clazz.newBooleanArray (maxs, false);
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) {
220 }if (rgxs[pass] != null) {
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) {
231 }var ns = rg.numSubs ();
233 thematches[pass].append (rg.stringMatched ());
235 var subs = new StringBuffer ();
238 if (s + 1 <= ns && rg.matchedToI (s) > -1 && rg.matchedToI (s + 1) > -1 && rg.matchedToI (s + 1) < rg.matchedToI (s)) {
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));
246 if (rmtch.length () > 0) {
250 if (rg.matchedFromI (s) > -1) {
251 subs.append (rg.stringMatchedI (s));
254 thematches[pass].append (subs);
257 if (!onlyIfMatches) {
258 thismatched[sq] = new Boolean (thismatched[sq] | true).valueOf ();
259 urllength += idseq[pass][sq].length;
261 thematches[pass] = new StringBuffer (idseq[pass][sq]);
263 if (thismatched[sq]) {
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]);
272 if (seqsmatched == 0 || (createFullUrl && matched[0].length () == 0)) {
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]);
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",
289 return (urlstub[0])[0];
291 Clazz.defineMethod (c$, "getGroupURLType",
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])) {
302 Clazz.overrideMethod (c$, "toString",
304 var result = new StringBuffer ();
305 result.append (this.label + "|" + this.url_prefix);
307 for (r = 0; r < this.url_suffix.length; r++) {
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]);
317 for (r = 0; r < this.separators.length; r++) {
319 result.append (this.separators[r]);
321 return result.toString ();
323 c$.formStrings = Clazz.defineMethod (c$, "formStrings",
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 ();
332 Clazz.defineMethod (c$, "setLabel",
333 function (newlabel) {
334 this.label = newlabel;
336 c$.$GroupUrlLink$UrlStringTooLongException$ = function () {
338 c$ = Clazz.decorateAsClass (function () {
339 Clazz.prepareCallback (this, arguments);
341 Clazz.instantialize (this, arguments);
342 }, jalview.util.GroupUrlLink, "UrlStringTooLongException", Exception);
343 Clazz.makeConstructor (c$,
345 Clazz.superConstructor (this, jalview.util.GroupUrlLink.UrlStringTooLongException, []);
348 Clazz.overrideMethod (c$, "toString",
350 return "Generated url is estimated to be too long (" + this.urlLength + ")";
354 Clazz.defineStatics (c$,
357 if (jalview.util.GroupUrlLink.tokens == null) {
358 jalview.util.GroupUrlLink.tokens = Clazz.newArray (-1, ["SEQUENCEIDS", "SEQUENCES", "DATASETID"]);
359 }}Clazz.defineStatics (c$,