Changeset 15441
- Timestamp:
- 10/29/20 16:54:35 (3 years ago)
- Location:
- trunk/abcl
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/abcl/src/org/armedbear/lisp/URLPathname.java
r15413 r15441 48 48 extends Pathname 49 49 { 50 static final Symbol SCHEME = internKeyword("SCHEME");51 static final Symbol AUTHORITY = internKeyword("AUTHORITY");52 static final Symbol QUERY = internKeyword("QUERY");53 static final Symbol FRAGMENT = internKeyword("FRAGMENT");50 static public final Symbol SCHEME = internKeyword("SCHEME"); 51 static public final Symbol AUTHORITY = internKeyword("AUTHORITY"); 52 static public final Symbol QUERY = internKeyword("QUERY"); 53 static public final Symbol FRAGMENT = internKeyword("FRAGMENT"); 54 54 55 55 protected URLPathname() {} … … 77 77 } 78 78 79 static public final LispObject FILE = new SimpleString("file"); 79 80 public static URLPathname createFromFile(Pathname p) { 80 String ns = "file:" + p.getNamestring(); 81 return create(ns); 81 URLPathname result = new URLPathname(); 82 result.copyFrom(p); 83 LispObject scheme = NIL; 84 scheme = scheme.push(FILE).push(SCHEME); 85 result.setHost(scheme); 86 return result; 82 87 } 83 88 … … 126 131 } 127 132 final Pathname p = (Pathname)Pathname.create(path); 133 LispObject host = NIL.push(FILE).push(SCHEME); 128 134 result 129 .setHost( p.getHost())135 .setHost(host) 130 136 .setDevice(p.getDevice()) 131 137 .setDirectory(p.getDirectory()) … … 181 187 } 182 188 189 public URI toURI() { 190 String uriString = getNamestringAsURL(); 191 try { 192 URI uri = new URI(uriString); 193 return uri; 194 } catch (URISyntaxException eo) { 195 return null; 196 } 197 } 198 199 public URL toURL() { 200 URI uri = toURI(); 201 try { 202 if (uri != null) { 203 return uri.toURL(); 204 } 205 } catch (MalformedURLException e) { 206 } 207 return null; 208 } 209 210 public File getFile() { 211 if (!hasExplicitFile(this)) { 212 return null; // TODO signal that this is not possible? 213 } 214 URI uri = toURI(); 215 if (uri == null) { 216 return null; 217 } 218 File result = new File(uri); 219 return result; 220 } 221 222 static public boolean isFile(Pathname p) { 223 LispObject scheme = Symbol.GETF.execute(p.getHost(), SCHEME, NIL); 224 if (scheme.equals(NIL) 225 || hasExplicitFile(p)) { 226 return true; 227 } 228 return false; 229 } 230 231 static public boolean hasExplicitFile(Pathname p) { 232 LispObject scheme = Symbol.GETF.execute(p.getHost(), SCHEME, NIL); 233 return scheme.equalp(FILE); 234 } 235 183 236 public String getNamestring() { 184 237 StringBuilder sb = new StringBuilder(); … … 193 246 // as part of the usual namestring. getNamestringAsURI() should 194 247 // emit the 'file:' string 195 if (!scheme.equals(NIL)) { 248 boolean percentEncode = true; 249 if (scheme.equals(NIL)) { 250 percentEncode = false; 251 } else { 196 252 sb.append(scheme.getStringValue()); 197 253 sb.append(":"); … … 199 255 sb.append("//"); 200 256 sb.append(authority.getStringValue()); 257 } else if (scheme.equalp(FILE)) { 258 sb.append("//"); 201 259 } 202 260 } … … 207 265 } 208 266 String directoryNamestring = getDirectoryNamestring(); 267 if (percentEncode) { 268 directoryNamestring = uriEncode(directoryNamestring); 269 } 209 270 sb.append(directoryNamestring); 210 271 … … 216 277 .setDirectory(NIL); 217 278 String nameTypeVersion = p.getNamestring(); 279 if (percentEncode) { 280 nameTypeVersion = uriEncode(nameTypeVersion); 281 } 218 282 sb.append(nameTypeVersion); 219 283 … … 221 285 if (o != NIL) { 222 286 sb.append("?") 223 .append( o.getStringValue());287 .append(uriEncode(o.getStringValue())); 224 288 } 225 289 o = Symbol.GETF.execute(getHost(), FRAGMENT, NIL); 226 290 if (o != NIL) { 227 291 sb.append("#") 228 .append( o.getStringValue());292 .append(uriEncode(o.getStringValue())); 229 293 } 230 294 … … 232 296 } 233 297 234 // TODO URIEncode path components? 298 // We need our "own" rules for outputting a URL 299 // 1. For DOS drive letters 300 // 2. For relative "file" schemas (??) 235 301 public String getNamestringAsURL() { 236 302 LispObject schemeProperty = Symbol.GETF.execute(getHost(), SCHEME, NIL); … … 240 306 241 307 String scheme; 242 String authority ;308 String authority = null; 243 309 if (!schemeProperty.equals(NIL)) { 244 310 scheme = schemeProperty.getStringValue(); 245 authority = authorityProperty.getStringValue(); 311 if (!authorityProperty.equals(NIL)) { 312 authority = authorityProperty.getStringValue(); 313 } 246 314 } else { 247 315 scheme = "file"; 248 authority = "";249 316 } 250 317 251 318 String directory = getDirectoryNamestring(); 252 String file = Symbol.FILE_NAMESTRING.execute(this).getStringValue(); 319 String file = ""; 320 LispObject fileNamestring = Symbol.FILE_NAMESTRING.execute(this); 321 if (!fileNamestring.equals(NIL)) { 322 file = fileNamestring.getStringValue(); 323 } 253 324 String path = ""; 254 325 … … 264 335 } 265 336 337 path = uriEncode(path); 338 266 339 String query = null; 267 340 if (!queryProperty.equals(NIL)) { … … 311 384 public static LispObject truename(URLPathname p, boolean errorIfDoesNotExist) { 312 385 if (p.getHost().equals(NIL) 313 || Symbol.GETF.execute(p.getHost(), URLPathname.SCHEME, NIL) 314 .equals("file")) { 386 || hasExplicitFile(p)) { 315 387 LispObject fileTruename = Pathname.truename(p, errorIfDoesNotExist); 316 388 if (fileTruename.equals(NIL)) { … … 318 390 } 319 391 if (!(fileTruename instanceof URLPathname)) { 320 URLPathname urlTruename = URLPathname.create ((Pathname)fileTruename);392 URLPathname urlTruename = URLPathname.createFromFile((Pathname)fileTruename); 321 393 return urlTruename; 322 394 } … … 339 411 return Pathname.doTruenameExit(p, errorIfDoesNotExist); 340 412 } 341 413 342 414 public InputStream getInputStream() { 343 415 InputStream result = null; 416 417 if (URLPathname.isFile(this)) { 418 Pathname p = new Pathname(); 419 p.copyFrom(this) 420 .setHost(NIL); 421 return p.getInputStream(); 422 } 423 424 if (URLPathname.isFile(this)) { 425 Pathname p = new Pathname(); 426 p.copyFrom(this) 427 .setHost(NIL); 428 return p.getInputStream(); 429 } 344 430 345 431 URL url = this.toURL(); -
trunk/abcl/test/lisp/abcl/jar-pathname.lisp
r15437 r15441 195 195 t) 196 196 197 #+(or) ;; URI encodings in namestring are not currently interpolated 197 198 (deftest jar-pathname.load.14 198 (load-from-jar *tmp-jar-path-whitespace* "a/b/bar.abcl") 199 t) 200 199 (with-jar-file-init 200 (load-from-jar *tmp-jar-path-whitespace* "a/b/bar.abcl")) 201 t) 202 #+(or) ;; URI encodings in namestring are not currently interpolated 201 203 (deftest jar-pathname.load.15 202 (signals-error 203 (load-from-jar *tmp-jar-path-whitespace* "a/b/foo bar.abcl") 204 'error) 204 (load-from-jar *tmp-jar-path-whitespace* "a/b/foo bar.abcl") 205 205 t) 206 206 -
trunk/abcl/test/src/org/armedbear/lisp/URLPathnameTest.java
r15408 r15441 16 16 public void roundTrips() { 17 17 String namestrings[] = { 18 "https://www.youtube.com/user/BlackHatOfficialYT" 18 "https://www.youtube.com/user/BlackHatOfficialYT", 19 "file:///a%20path%20/with/whitespace.lisp" 19 20 }; 20 21 21 22 for (String namestring : namestrings) { 22 Pathname result = (Pathname)Pathname.create(namestring);23 URLPathname result = URLPathname.create(namestring); 23 24 String resultingNamestring = result.getNamestring(); 24 25 String message = MessageFormat.format("Namestring \"{0}\" failed to roundtrip", namestring);
Note: See TracChangeset
for help on using the changeset viewer.