JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / bin / jalview / util / UrlLink.js
1 Clazz.declarePackage ("jalview.util");\r
2 Clazz.load (null, "jalview.util.UrlLink", ["jalview.jsdev.RegExp", "java.util.Vector"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.url_suffix = null;\r
5 this.url_prefix = null;\r
6 this.target = null;\r
7 this.label = null;\r
8 this.regexReplace = null;\r
9 this.dynamic = false;\r
10 this.invalidMessage = null;\r
11 Clazz.instantialize (this, arguments);\r
12 }, jalview.util, "UrlLink");\r
13 Clazz.makeConstructor (c$, \r
14 function (link) {\r
15 var sep = link.indexOf ("|");\r
16 var psqid = link.indexOf ("$SEQUENCE_ID");\r
17 if (psqid > -1) {\r
18 this.dynamic = true;\r
19 var p = sep;\r
20 do {\r
21 sep = p;\r
22 p = link.indexOf ("|", sep + 1);\r
23 } while (p > sep && p < psqid);\r
24 this.label = link.substring (0, sep);\r
25 if (this.label.indexOf ("|") > -1) {\r
26 this.target = this.label.substring (0, this.label.indexOf ("|"));\r
27 } else if (this.label.indexOf (" ") > 2) {\r
28 this.target = this.label.substring (0, this.label.indexOf (" "));\r
29 } else {\r
30 this.target = this.label;\r
31 }this.url_prefix = link.substring (sep + 1, psqid);\r
32 if (link.indexOf ("$SEQUENCE_ID=/") == psqid && (p = link.indexOf ("/=$", psqid + 14)) > psqid + 14) {\r
33 this.url_suffix = link.substring (p + 3);\r
34 this.regexReplace = link.substring (psqid + 14, p);\r
35 try {\r
36 var rg = jalview.jsdev.RegExp.perlCode ("/" + this.regexReplace + "/");\r
37 if (rg == null) {\r
38 this.invalidMessage = "Invalid Regular Expression : '" + this.regexReplace + "'\n";\r
39 }} catch (e) {\r
40 if (Clazz.exceptionOf (e, Exception)) {\r
41 this.invalidMessage = "Invalid Regular Expression : '" + this.regexReplace + "'\n";\r
42 } else {\r
43 throw e;\r
44 }\r
45 }\r
46 } else {\r
47 this.regexReplace = null;\r
48 if (link.indexOf ("$SEQUENCE_ID$") == psqid) {\r
49 this.url_suffix = link.substring (psqid + 13);\r
50 this.regexReplace = null;\r
51 } else {\r
52 this.invalidMessage = "Warning: invalid regex structure for URL link : " + link;\r
53 }}} else {\r
54 this.target = link.substring (0, sep);\r
55 this.label = link.substring (0, sep = link.lastIndexOf ("|"));\r
56 this.url_prefix = link.substring (sep + 1);\r
57 this.regexReplace = null;\r
58 this.url_suffix = null;\r
59 }}, "~S");\r
60 Clazz.defineMethod (c$, "getUrl_suffix", \r
61 function () {\r
62 return this.url_suffix;\r
63 });\r
64 Clazz.defineMethod (c$, "getUrl_prefix", \r
65 function () {\r
66 return this.url_prefix;\r
67 });\r
68 Clazz.defineMethod (c$, "getTarget", \r
69 function () {\r
70 return this.target;\r
71 });\r
72 Clazz.defineMethod (c$, "getLabel", \r
73 function () {\r
74 return this.label;\r
75 });\r
76 Clazz.defineMethod (c$, "getRegexReplace", \r
77 function () {\r
78 return this.regexReplace;\r
79 });\r
80 Clazz.defineMethod (c$, "getInvalidMessage", \r
81 function () {\r
82 return this.invalidMessage;\r
83 });\r
84 Clazz.defineMethod (c$, "isValid", \r
85 function () {\r
86 return this.invalidMessage == null;\r
87 });\r
88 Clazz.defineMethod (c$, "makeUrls", \r
89 function (idstring, onlyIfMatches) {\r
90 if (this.dynamic) {\r
91 if (this.regexReplace != null) {\r
92 var rg = jalview.jsdev.RegExp.perlCode ("/" + this.regexReplace + "/");\r
93 if (rg.search (idstring)) {\r
94 var ns = rg.numSubs ();\r
95 if (ns == 0) {\r
96 return  Clazz.newArray (-1, [rg.stringMatchedI (ns), this.url_prefix + rg.stringMatched () + this.url_suffix]);\r
97 } else {\r
98 for (var s = 0; s <= rg.numSubs (); s++) {\r
99 System.err.println ("Sub " + s + " : " + rg.matchedFromI (s) + " : " + rg.matchedToI (s) + " : '" + rg.stringMatchedI (s) + "'");\r
100 }\r
101 var subs =  new java.util.Vector ();\r
102 var s = 0;\r
103 while (s <= ns) {\r
104 if (s + 1 <= ns && rg.matchedToI (s) > -1 && rg.matchedToI (s + 1) > -1 && rg.matchedToI (s + 1) < rg.matchedToI (s)) {\r
105 var r = s + 1;\r
106 var mtch = "";\r
107 while (r <= ns && rg.matchedToI (r) <= rg.matchedToI (s)) {\r
108 if (rg.matchedFromI (r) > -1) {\r
109 mtch += rg.stringMatchedI (r);\r
110 }r++;\r
111 }\r
112 if (mtch.length > 0) {\r
113 subs.addElement (mtch);\r
114 subs.addElement (this.url_prefix + mtch + this.url_suffix);\r
115 }s = r;\r
116 } else {\r
117 if (rg.matchedFromI (s) > -1) {\r
118 subs.addElement (rg.stringMatchedI (s));\r
119 subs.addElement (this.url_prefix + rg.stringMatchedI (s) + this.url_suffix);\r
120 }s++;\r
121 }}\r
122 var res =  new Array (subs.size ());\r
123 for (var r = 0, rs = subs.size (); r < rs; r++) {\r
124 res[r] = subs.elementAt (r);\r
125 }\r
126 subs.removeAllElements ();\r
127 return res;\r
128 }}if (onlyIfMatches) {\r
129 return null;\r
130 }}if (idstring.indexOf ("|") > -1) {\r
131 idstring = idstring.substring (idstring.lastIndexOf ("|") + 1);\r
132 }return  Clazz.newArray (-1, [idstring, this.url_prefix + idstring + this.url_suffix]);\r
133 } else {\r
134 return  Clazz.newArray (-1, ["", this.url_prefix]);\r
135 }}, "~S,~B");\r
136 Clazz.overrideMethod (c$, "toString", \r
137 function () {\r
138 return this.label + "|" + this.url_prefix + (this.dynamic ? ("$SEQUENCE_ID" + ((this.regexReplace != null) ? "=" + this.regexReplace + "=$" : "$")) : "") + ((this.url_suffix == null) ? "" : this.url_suffix);\r
139 });\r
140 Clazz.defineMethod (c$, "isDynamic", \r
141 function () {\r
142 return this.dynamic;\r
143 });\r
144 Clazz.defineMethod (c$, "setLabel", \r
145 function (newlabel) {\r
146 this.label = newlabel;\r
147 }, "~S");\r
148 });\r