Changeset 15441


Ignore:
Timestamp:
10/29/20 16:54:35 (3 years ago)
Author:
Mark Evenson
Message:

Able to load from directories with whitespace

Location:
trunk/abcl
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/abcl/src/org/armedbear/lisp/URLPathname.java

    r15413 r15441  
    4848  extends Pathname
    4949{
    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");
    5454
    5555  protected URLPathname() {}
     
    7777  }
    7878
     79  static public final LispObject FILE = new SimpleString("file");
    7980  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;
    8287  }
    8388
     
    126131      }
    127132      final Pathname p = (Pathname)Pathname.create(path);
     133      LispObject host = NIL.push(FILE).push(SCHEME);
    128134      result
    129         .setHost(p.getHost())
     135        .setHost(host)
    130136        .setDevice(p.getDevice())
    131137        .setDirectory(p.getDirectory())
     
    181187  }
    182188
     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
    183236  public String getNamestring() {
    184237    StringBuilder sb = new StringBuilder();
     
    193246    // as part of the usual namestring.  getNamestringAsURI() should
    194247    // emit the 'file:' string
    195     if (!scheme.equals(NIL)) {
     248    boolean percentEncode = true;
     249    if (scheme.equals(NIL)) {
     250      percentEncode = false;
     251    } else {
    196252      sb.append(scheme.getStringValue());
    197253      sb.append(":");
     
    199255        sb.append("//");
    200256        sb.append(authority.getStringValue());
     257      } else if (scheme.equalp(FILE)) {
     258        sb.append("//");
    201259      }
    202260    }
     
    207265    }
    208266    String directoryNamestring = getDirectoryNamestring();
     267    if (percentEncode) {
     268      directoryNamestring = uriEncode(directoryNamestring);
     269    }
    209270    sb.append(directoryNamestring);
    210271
     
    216277      .setDirectory(NIL);
    217278    String nameTypeVersion = p.getNamestring();
     279    if (percentEncode) {
     280      nameTypeVersion = uriEncode(nameTypeVersion);
     281    }     
    218282    sb.append(nameTypeVersion);
    219283
     
    221285    if (o != NIL) {
    222286      sb.append("?")
    223         .append(o.getStringValue());
     287        .append(uriEncode(o.getStringValue()));
    224288    }
    225289    o = Symbol.GETF.execute(getHost(), FRAGMENT, NIL);
    226290    if (o != NIL) {
    227291      sb.append("#")
    228         .append(o.getStringValue());
     292        .append(uriEncode(o.getStringValue()));
    229293    }
    230294
     
    232296  }
    233297
    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 (??)
    235301  public String getNamestringAsURL() {
    236302    LispObject schemeProperty = Symbol.GETF.execute(getHost(), SCHEME, NIL);
     
    240306
    241307    String scheme;
    242     String authority;
     308    String authority = null;
    243309    if (!schemeProperty.equals(NIL)) {
    244310      scheme = schemeProperty.getStringValue();
    245       authority =  authorityProperty.getStringValue();
     311      if (!authorityProperty.equals(NIL)) {
     312        authority =  authorityProperty.getStringValue();
     313      }
    246314    } else {
    247315      scheme = "file";
    248       authority = "";
    249316    }
    250317
    251318    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    }
    253324    String path = "";
    254325
     
    264335    }
    265336
     337    path = uriEncode(path);
     338
    266339    String query = null;
    267340    if (!queryProperty.equals(NIL)) {
     
    311384  public static LispObject truename(URLPathname p, boolean errorIfDoesNotExist) {
    312385    if (p.getHost().equals(NIL)
    313      || Symbol.GETF.execute(p.getHost(), URLPathname.SCHEME, NIL)
    314           .equals("file")) {
     386        || hasExplicitFile(p)) {
    315387      LispObject fileTruename = Pathname.truename(p, errorIfDoesNotExist);
    316388      if (fileTruename.equals(NIL)) {
     
    318390      }
    319391      if (!(fileTruename instanceof URLPathname)) {
    320         URLPathname urlTruename = URLPathname.create((Pathname)fileTruename);
     392        URLPathname urlTruename = URLPathname.createFromFile((Pathname)fileTruename);
    321393        return urlTruename;
    322394      }
     
    339411    return Pathname.doTruenameExit(p, errorIfDoesNotExist);
    340412  }
    341 
     413 
    342414  public InputStream getInputStream() {
    343415    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    }
    344430
    345431    URL url = this.toURL();
  • trunk/abcl/test/lisp/abcl/jar-pathname.lisp

    r15437 r15441  
    195195  t)
    196196
     197#+(or) ;; URI encodings in namestring are not currently interpolated
    197198(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
    201203(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") 
    205205  t)
    206206
  • trunk/abcl/test/src/org/armedbear/lisp/URLPathnameTest.java

    r15408 r15441  
    1616  public void roundTrips() {
    1717    String namestrings[] = {
    18       "https://www.youtube.com/user/BlackHatOfficialYT"
     18      "https://www.youtube.com/user/BlackHatOfficialYT",
     19      "file:///a%20path%20/with/whitespace.lisp"
    1920    };
    2021
    2122    for (String namestring  : namestrings) {
    22       Pathname result = (Pathname) Pathname.create(namestring);
     23      URLPathname result = URLPathname.create(namestring);
    2324      String resultingNamestring = result.getNamestring();
    2425      String message = MessageFormat.format("Namestring \"{0}\" failed to roundtrip", namestring);
Note: See TracChangeset for help on using the changeset viewer.