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