nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Source.java
changeset 39351 a049c96353ae
parent 36517 41a1c20eb619
child 45725 083026107a26
equal deleted inserted replaced
39184:3aa52182b3ad 39351:a049c96353ae
    34 import java.io.InputStream;
    34 import java.io.InputStream;
    35 import java.io.PrintWriter;
    35 import java.io.PrintWriter;
    36 import java.io.Reader;
    36 import java.io.Reader;
    37 import java.lang.ref.WeakReference;
    37 import java.lang.ref.WeakReference;
    38 import java.net.MalformedURLException;
    38 import java.net.MalformedURLException;
       
    39 import java.net.URI;
    39 import java.net.URISyntaxException;
    40 import java.net.URISyntaxException;
    40 import java.net.URL;
    41 import java.net.URL;
    41 import java.net.URLConnection;
    42 import java.net.URLConnection;
    42 import java.nio.charset.Charset;
    43 import java.nio.charset.Charset;
    43 import java.nio.charset.StandardCharsets;
    44 import java.nio.charset.StandardCharsets;
    73      * Used to implement __FILE__. Also used for SourceFile in .class for debugger usage.
    74      * Used to implement __FILE__. Also used for SourceFile in .class for debugger usage.
    74      */
    75      */
    75     private final String name;
    76     private final String name;
    76 
    77 
    77     /**
    78     /**
    78      * Base directory the File or base part of the URL. Used to implement __DIR__.
    79      * Base path or URL of this source. Used to implement __DIR__, which can be
    79      * Used to load scripts relative to the 'directory' or 'base' URL of current script.
    80      * used to load scripts relative to the location of the current script.
    80      * This will be null when it can't be computed.
    81      * This will be null when it can't be computed.
    81      */
    82      */
    82     private final String base;
    83     private final String base;
    83 
    84 
    84     /** Source content */
    85     /** Source content */
   873         }
   874         }
   874         return ldigest;
   875         return ldigest;
   875     }
   876     }
   876 
   877 
   877     /**
   878     /**
   878      * Get the base url. This is currently used for testing only
   879      * Returns the base directory or URL for the given URL. Used to implement __DIR__.
   879      * @param url a URL
   880      * @param url a URL
   880      * @return base URL for url
   881      * @return base path or URL, or null if argument is not a hierarchical URL
   881      */
   882      */
   882     public static String baseURL(final URL url) {
   883     public static String baseURL(final URL url) {
   883         if (url.getProtocol().equals("file")) {
   884         try {
   884             try {
   885             final URI uri = url.toURI();
   885                 final Path path = Paths.get(url.toURI());
   886 
       
   887             if (uri.getScheme().equals("file")) {
       
   888                 final Path path = Paths.get(uri);
   886                 final Path parent = path.getParent();
   889                 final Path parent = path.getParent();
   887                 return (parent != null) ? (parent + File.separator) : null;
   890                 return (parent != null) ? (parent + File.separator) : null;
   888             } catch (final SecurityException | URISyntaxException | IOError e) {
   891             }
       
   892             if (uri.isOpaque() || uri.getPath() == null || uri.getPath().isEmpty()) {
   889                 return null;
   893                 return null;
   890             }
   894             }
   891         }
   895             return uri.resolve("").toString();
   892 
   896 
   893         // FIXME: is there a better way to find 'base' URL of a given URL?
   897         } catch (final SecurityException | URISyntaxException | IOError e) {
   894         String path = url.getPath();
       
   895         if (path.isEmpty()) {
       
   896             return null;
       
   897         }
       
   898         path = path.substring(0, path.lastIndexOf('/') + 1);
       
   899         final int port = url.getPort();
       
   900         try {
       
   901             return new URL(url.getProtocol(), url.getHost(), port, path).toString();
       
   902         } catch (final MalformedURLException e) {
       
   903             return null;
   898             return null;
   904         }
   899         }
   905     }
   900     }
   906 
   901 
   907     private static String dirName(final File file, final String DEFAULT_BASE_NAME) {
   902     private static String dirName(final File file, final String DEFAULT_BASE_NAME) {