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