Example #1
0
 /**
  * Following the relevant RFC, construct a valid URL based on the passed in string.
  *
  * @param url A string which represents either a relative or absolute URL.
  * @return A URL when the passed in string can be interpreted according to the RFC. <code>null
  *     </code> otherwise.
  * @exception ParseException Thrown when we are unable to construct a proper URL from the passed
  *     in string.
  */
 private URL parseURL(String url) throws ParseException {
   URL u = null;
   try {
     if (url.indexOf(':') <= 1) {
       // we were passed in a relative URL or an absolute URL on a
       // win32 machine
       u = ParseUtil.fileToEncodedURL(new File(System.getProperty("user.dir"), url));
     } else {
       if (url.startsWith("file:") && url.replace(File.separatorChar, '/').indexOf('/') == -1) {
         // We were passed in a relative "file" URL, like this:
         //     "file:index.html".
         // Prepend current directory location.
         String fname = url.substring("file:".length());
         if (fname.length() > 0) {
           u = ParseUtil.fileToEncodedURL(new File(System.getProperty("user.dir"), fname));
         } else {
           u = new URL(url);
         }
       } else {
         u = new URL(url);
       }
     }
   } catch (MalformedURLException e) {
     throw new ParseException(lookup("main.err.badurl", url, e.getMessage()));
   }
   return u;
 }
  /**
   * Convert class path specification into an array of file URLs.
   *
   * <p>The path of the file is encoded before conversion into URL form so that reserved characters
   * can safely appear in the path.
   */
  public static URL[] pathToURLs(String path) {
    StringTokenizer st = new StringTokenizer(path, File.pathSeparator);
    URL[] urls = new URL[st.countTokens()];
    int count = 0;
    while (st.hasMoreTokens()) {
      File f = new File(st.nextToken());
      try {
        f = new File(f.getCanonicalPath());
      } catch (IOException x) {
        // use the non-canonicalized filename
      }
      try {
        urls[count++] = ParseUtil.fileToEncodedURL(f);
      } catch (IOException x) {
      }
    }

    if (urls.length != count) {
      URL[] tmp = new URL[count];
      System.arraycopy(urls, 0, tmp, 0, count);
      urls = tmp;
    }
    return urls;
  }