Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / java / net / URL.js
1 Clazz.declarePackage ("java.net");
2 Clazz.load (["java.util.Hashtable"], "java.net.URL", ["java.io.IOException", "java.lang.Character", "$.Error", "java.net.MalformedURLException"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.protocol = null;
5 this.host = null;
6 this.port = -1;
7 this.file = null;
8 this.query = null;
9 this.authority = null;
10 this.path = null;
11 this.userInfo = null;
12 this.ref = null;
13 this.handler = null;
14 this.$hashCode = -1;
15 Clazz.instantialize (this, arguments);
16 }, java.net, "URL", null, java.io.Serializable);
17 Clazz.makeConstructor (c$, 
18 function (context, spec, handler) {
19 {
20 switch (arguments.length) {
21 case 1:
22 spec = context;context = handler = null;
23 break;
24 case 2:
25 handler = null;
26 break;
27 case 3:
28 if (context == null || Clazz.instanceOf(context, java.net.URL))
29 break;
30 default:
31 alert("java.net.URL constructor format not supported");
32 break;
33 }
34 context && context.valueOf && context.valueOf() == null && (context = null);
35 }var original = spec;
36 var i;
37 var limit;
38 var c;
39 var start = 0;
40 var newProtocol = null;
41 var aRef = false;
42 var isRelative = false;
43 try {
44 limit = spec.length;
45 while ((limit > 0) && (spec.charAt (limit - 1) <= ' ')) {
46 limit--;
47 }
48 while ((start < limit) && (spec.charAt (start) <= ' ')) {
49 start++;
50 }
51 if (spec.regionMatches (true, start, "url:", 0, 4)) {
52 start += 4;
53 }if (start < spec.length && spec.charAt (start) == '#') {
54 aRef = true;
55 }for (i = start; !aRef && (i < limit) && ((c = spec.charCodeAt (i)) != 47); i++) {
56 if (c == 58) {
57 var s = spec.substring (start, i).toLowerCase ();
58 if (this.isValidProtocol (s)) {
59 newProtocol = s;
60 start = i + 1;
61 }break;
62 }}
63 this.protocol = newProtocol;
64 if ((context != null) && ((newProtocol == null) || newProtocol.equalsIgnoreCase (context.protocol))) {
65 if (handler == null) {
66 handler = context.handler;
67 }if (context.path != null && context.path.startsWith ("/")) newProtocol = null;
68 if (newProtocol == null) {
69 this.protocol = context.protocol;
70 this.authority = context.authority;
71 this.userInfo = context.userInfo;
72 this.host = context.host;
73 this.port = context.port;
74 this.file = context.file;
75 this.path = context.path;
76 isRelative = true;
77 }}if (this.protocol == null) {
78 throw  new java.net.MalformedURLException ("no protocol: " + original);
79 }if (handler == null && (handler = java.net.URL.getURLStreamHandler (this.protocol)) == null) {
80 throw  new java.net.MalformedURLException ("unknown protocol: " + this.protocol);
81 }this.handler = handler;
82 i = spec.indexOf ('#', start);
83 if (i >= 0) {
84 this.ref = spec.substring (i + 1, limit);
85 limit = i;
86 }if (isRelative && start == limit) {
87 this.query = context.query;
88 if (this.ref == null) {
89 this.ref = context.ref;
90 }}handler.parseURL (this, spec, start, limit);
91 } catch (e$$) {
92 if (Clazz.exceptionOf (e$$, java.net.MalformedURLException)) {
93 var e = e$$;
94 {
95 throw e;
96 }
97 } else if (Clazz.exceptionOf (e$$, Exception)) {
98 var e = e$$;
99 {
100 var exception =  new java.net.MalformedURLException (e.getMessage ());
101 exception.initCause (e);
102 throw exception;
103 }
104 } else {
105 throw e$$;
106 }
107 }
108 }, "java.net.URL,~S,java.net.URLStreamHandler");
109 Clazz.defineMethod (c$, "isValidProtocol", 
110  function (protocol) {
111 var len = protocol.length;
112 if (len < 1) return false;
113 var c = protocol.charAt (0);
114 if (!Character.isLetter (c)) return false;
115 for (var i = 1; i < len; i++) {
116 c = protocol.charAt (i);
117 if (!Character.isLetterOrDigit (c) && c != '.' && c != '+' && c != '-') {
118 return false;
119 }}
120 return true;
121 }, "~S");
122 Clazz.defineMethod (c$, "set5", 
123 function (protocol, host, port, file, ref) {
124 {
125 this.protocol = protocol;
126 this.host = host;
127 this.authority = port == -1 ? host : host + ":" + port;
128 this.port = port;
129 this.file = file;
130 this.ref = ref;
131 this.$hashCode = -1;
132 var q = file.lastIndexOf ('?');
133 if (q != -1) {
134 this.query = file.substring (q + 1);
135 this.path = file.substring (0, q);
136 } else this.path = file;
137 }}, "~S,~S,~N,~S,~S");
138 Clazz.defineMethod (c$, "set", 
139 function (protocol, host, port, authority, userInfo, path, query, ref) {
140 {
141 this.protocol = protocol;
142 this.host = host;
143 this.port = port;
144 this.file = query == null ? path : path + "?" + query;
145 this.userInfo = userInfo;
146 this.path = path;
147 this.ref = ref;
148 this.$hashCode = -1;
149 this.query = query;
150 this.authority = authority;
151 }}, "~S,~S,~N,~S,~S,~S,~S,~S");
152 Clazz.defineMethod (c$, "getQuery", 
153 function () {
154 return this.query;
155 });
156 Clazz.defineMethod (c$, "getPath", 
157 function () {
158 return this.path;
159 });
160 Clazz.defineMethod (c$, "getUserInfo", 
161 function () {
162 return this.userInfo;
163 });
164 Clazz.defineMethod (c$, "getAuthority", 
165 function () {
166 return this.authority;
167 });
168 Clazz.defineMethod (c$, "getPort", 
169 function () {
170 return this.port;
171 });
172 Clazz.defineMethod (c$, "getDefaultPort", 
173 function () {
174 return this.handler.getDefaultPort ();
175 });
176 Clazz.defineMethod (c$, "getProtocol", 
177 function () {
178 return this.protocol;
179 });
180 Clazz.defineMethod (c$, "getHost", 
181 function () {
182 return this.host;
183 });
184 Clazz.defineMethod (c$, "getFile", 
185 function () {
186 return this.file;
187 });
188 Clazz.defineMethod (c$, "getRef", 
189 function () {
190 return this.ref;
191 });
192 Clazz.overrideMethod (c$, "equals", 
193 function (obj) {
194 if (!(Clazz.instanceOf (obj, java.net.URL))) return false;
195 var u2 = obj;
196 return this.handler.equals2 (this, u2);
197 }, "~O");
198 Clazz.overrideMethod (c$, "hashCode", 
199 function () {
200 if (this.$hashCode != -1) return this.$hashCode;
201 this.$hashCode = this.handler.hashCode (this);
202 return this.$hashCode;
203 });
204 Clazz.defineMethod (c$, "sameFile", 
205 function (other) {
206 return this.handler.sameFile (this, other);
207 }, "java.net.URL");
208 Clazz.overrideMethod (c$, "toString", 
209 function () {
210 return this.toExternalForm ();
211 });
212 Clazz.defineMethod (c$, "toExternalForm", 
213 function () {
214 return this.handler.toExternalForm (this);
215 });
216 Clazz.defineMethod (c$, "openConnection", 
217 function () {
218 return this.handler.openConnection (this);
219 });
220 Clazz.defineMethod (c$, "openStream", 
221 function () {
222 return this.openConnection ().getInputStream ();
223 });
224 Clazz.defineMethod (c$, "getContent", 
225 function () {
226 return this.openConnection ().getInputStream ();
227 });
228 c$.setURLStreamHandlerFactory = Clazz.defineMethod (c$, "setURLStreamHandlerFactory", 
229 function (fac) {
230 {
231 if (java.net.URL.factory != null) {
232 throw  new Error ("factory already defined");
233 }var security = System.getSecurityManager ();
234 if (security != null) {
235 security.checkSetFactory ();
236 }java.net.URL.handlers.clear ();
237 java.net.URL.factory = fac;
238 }}, "java.net.URLStreamHandlerFactory");
239 c$.getURLStreamHandler = Clazz.defineMethod (c$, "getURLStreamHandler", 
240 function (protocol) {
241 var handler = java.net.URL.handlers.get (protocol);
242 if (handler == null) {
243 if (java.net.URL.factory != null) {
244 handler = java.net.URL.factory.createURLStreamHandler (protocol);
245 }}return handler;
246 }, "~S");
247 Clazz.defineStatics (c$,
248 "factory", null);
249 c$.handlers = c$.prototype.handlers =  new java.util.Hashtable ();
250 c$.streamHandlerLock = c$.prototype.streamHandlerLock =  new Clazz._O ();
251 });