Java Code Examples for java.net.URI.isOpaque()

The following are Jave code examples for showing how to use isOpaque() of the java.net.URI class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: incubator-netbeans   File: J2SEModularProjectProperties.java   View Source Code Vote up 6 votes
private void validateURL(final URL url, final File file) {
    try {
        final URI uri = url.toURI();
        if (!uri.isAbsolute()) {
            throw new IllegalArgumentException("URI is not absolute: " + uri.toString() + " File: " + file.getAbsolutePath());   //NOI18N
        }
        if (uri.isOpaque()) {
            throw new IllegalArgumentException("URI is not hierarchical: " + uri.toString() + " File: " + file.getAbsolutePath());   //NOI18N
        }
        if (!"file".equals(uri.getScheme())) {
            throw new IllegalArgumentException("URI scheme is not \"file\": " + uri.toString() + " File: " + file.getAbsolutePath());   //NOI18N
        }
    } catch (URISyntaxException use) {
        throw new IllegalArgumentException(use);
    }
}
 
Example 2
Project: incubator-netbeans   File: J2SEProjectProperties.java   View Source Code Vote up 6 votes
private void validateURL(final URL url, final File file) {
    try {
        final URI uri = url.toURI();
        if (!uri.isAbsolute()) {
            throw new IllegalArgumentException("URI is not absolute: " + uri.toString() + " File: " + file.getAbsolutePath());   //NOI18N
        }
        if (uri.isOpaque()) {
            throw new IllegalArgumentException("URI is not hierarchical: " + uri.toString() + " File: " + file.getAbsolutePath());   //NOI18N
        }
        if (!"file".equals(uri.getScheme())) {
            throw new IllegalArgumentException("URI scheme is not \"file\": " + uri.toString() + " File: " + file.getAbsolutePath());   //NOI18N
        }
    } catch (URISyntaxException use) {
        throw new IllegalArgumentException(use);
    }
}
 
Example 3
Project: elasticsearch_my   File: URIPattern.java   View Source Code Vote up 6 votes
private boolean matchNormalized(URI uri) {
    if(uriPattern.isOpaque()) {
        // This url only has scheme, scheme-specific part and fragment
        return uri.isOpaque() &&
                match(uriPattern.getScheme(), uri.getScheme()) &&
                match(uriPattern.getSchemeSpecificPart(), uri.getSchemeSpecificPart()) &&
                match(uriPattern.getFragment(), uri.getFragment());

    } else {
        return match(uriPattern.getScheme(), uri.getScheme()) &&
                match(uriPattern.getAuthority(), uri.getAuthority()) &&
                match(uriPattern.getQuery(), uri.getQuery()) &&
                match(uriPattern.getPath(), uri.getPath()) &&
                match(uriPattern.getFragment(), uri.getFragment());
    }
}
 
Example 4
Project: letv   File: jr.java   View Source Code Vote up 6 votes
public static String b(String str) {
    if (TextUtils.isEmpty(str)) {
        return str;
    }
    URI i = i(str);
    if (i == null) {
        return str;
    }
    i = i.normalize();
    if (i.isOpaque()) {
        return str;
    }
    i = a(i.getScheme(), i.getAuthority(), "/", null, null);
    if (i != null) {
        return i.toString();
    }
    return str;
}
 
Example 5
Project: letv   File: jr.java   View Source Code Vote up 6 votes
public static String c(String str) {
    if (TextUtils.isEmpty(str)) {
        return str;
    }
    URI i = i(str);
    if (i == null) {
        return str;
    }
    i = i.normalize();
    if (i.isOpaque()) {
        return str;
    }
    i = URIUtils.resolve(i, "./");
    if (i != null) {
        return i.toString();
    }
    return str;
}
 
Example 6
Project: FirefoxData-android   File: URIUtils.java   View Source Code Vote up 6 votes
/**
 * A convenience method that creates a new {@link URI} whose scheme, host, port, path,
 * query are taken from the existing URI, dropping any fragment or user-information.
 * The path is set to "/" if not explicitly specified. The existing URI is returned
 * unmodified if it has no fragment or user-information and has a path.
 *
 * @param uri
 *            original URI.
 * @throws URISyntaxException
 *             If the resulting URI is invalid.
 */
public static URI rewriteURI(final URI uri) throws URISyntaxException {
    Args.notNull(uri, "URI");
    if (uri.isOpaque()) {
        return uri;
    }
    final URIBuilder uribuilder = new URIBuilder(uri);
    if (uribuilder.getUserInfo() != null) {
        uribuilder.setUserInfo(null);
    }
    if (TextUtils.isEmpty(uribuilder.getPath())) {
        uribuilder.setPath("/");
    }
    if (uribuilder.getHost() != null) {
        uribuilder.setHost(uribuilder.getHost().toLowerCase(Locale.ENGLISH));
    }
    uribuilder.setFragment(null);
    return uribuilder.build();
}
 
Example 7
Project: openjdk-jdk10   File: Source.java   View Source Code Vote up 6 votes
/**
 * Returns the base directory or URL for the given URL. Used to implement __DIR__.
 * @param url a URL
 * @return base path or URL, or null if argument is not a hierarchical URL
 */
public static String baseURL(final URL url) {
    try {
        final URI uri = url.toURI();

        if (uri.getScheme().equals("file")) {
            final Path path = Paths.get(uri);
            final Path parent = path.getParent();
            return (parent != null) ? (parent + File.separator) : null;
        }
        if (uri.isOpaque() || uri.getPath() == null || uri.getPath().isEmpty()) {
            return null;
        }
        return uri.resolve("").toString();

    } catch (final SecurityException | URISyntaxException | IOError e) {
        return null;
    }
}
 
Example 8
Project: rs-aggregator   File: NormURI.java   View Source Code Vote up 6 votes
public static Optional<URI> normalize(@Nonnull URI uri) {
  URI n = uri.normalize();
  String host = stripWWW(n);
  host = host == null ? null : host.toLowerCase();
  String path = normalizePath(n).getPath();
  try {
    if (n.isOpaque()) {
      return Optional.of(new URI(n.getScheme(), n.getSchemeSpecificPart(), n.getFragment()));
    } else {
      return Optional.of(new URI(n.getScheme(), null, host, n.getPort(), path, null, null));
    }
  } catch (URISyntaxException e) {
    // unlikely deviation
    throw new RuntimeException(e);
  }
}
 
Example 9
Project: Elasticsearch   File: URIPattern.java   View Source Code Vote up 6 votes
private boolean matchNormalized(URI uri) {
    if(uriPattern.isOpaque()) {
        // This url only has scheme, scheme-specific part and fragment
        return uri.isOpaque() &&
                match(uriPattern.getScheme(), uri.getScheme()) &&
                match(uriPattern.getSchemeSpecificPart(), uri.getSchemeSpecificPart()) &&
                match(uriPattern.getFragment(), uri.getFragment());

    } else {
        return match(uriPattern.getScheme(), uri.getScheme()) &&
                match(uriPattern.getAuthority(), uri.getAuthority()) &&
                match(uriPattern.getQuery(), uri.getQuery()) &&
                match(uriPattern.getPath(), uri.getPath()) &&
                match(uriPattern.getFragment(), uri.getFragment());
    }
}
 
Example 10
Project: incubator-netbeans   File: FileOwnerQuery.java   View Source Code Vote up 5 votes
/**
 * Find the project, if any, which "owns" the given URI.
 * @param uri the URI to the file (generally on disk); must be absolute and not opaque (though {@code jar}-protocol URIs are unwrapped as a convenience)
 * @return a project which contains it, or null if there is no known project containing it
 * @throws IllegalArgumentException if the URI is relative or opaque
 */
public static Project getOwner(URI uri) {
    try {
        URL url = uri.toURL();
        if (FileUtil.isArchiveArtifact(url)) {
            url = FileUtil.getArchiveFile(url);
            if (url != null) {
                uri = url.toURI();
            }
        }
    } catch (MalformedURLException | URISyntaxException e) {
        LOG.log(Level.INFO, null, e);
    }
    if (!uri.isAbsolute() || uri.isOpaque()) {
        throw new IllegalArgumentException("Bad URI: " + uri); // NOI18N
    }
    for (FileOwnerQueryImplementation q : getInstances()) {
        Project p = q.getOwner(uri);
        if (p != null) {
            if (LOG.isLoggable(Level.FINE)) {
                LOG.log(Level.FINE, "getOwner({0}) -> {1} from {2}", new Object[] {uri, p, q});
            }
            return p == UNOWNED ? null : p;
        }
    }
    LOG.log(Level.FINE, "getOwner({0}) -> nil", uri);
    return null;
}
 
Example 11
Project: OpenJSharp   File: UnixUriUtils.java   View Source Code Vote up 5 votes
/**
 * Converts URI to Path
 */
static Path fromUri(NetFileSystem fs, URI uri) {
    if (!uri.isAbsolute())
        throw new IllegalArgumentException("URI is not absolute");
    if (uri.isOpaque())
        throw new IllegalArgumentException("URI is not hierarchical");
    String scheme = uri.getScheme();
    if ((scheme == null) || !scheme.equalsIgnoreCase("file"))
        throw new IllegalArgumentException("URI scheme is not \"file\"");
    if (uri.getAuthority() != null)
        throw new IllegalArgumentException("URI has an authority component");
    if (uri.getFragment() != null)
        throw new IllegalArgumentException("URI has a fragment component");
    if (uri.getQuery() != null)
        throw new IllegalArgumentException("URI has a query component");

    // compatibility with java.io.File
    if (!uri.toString().startsWith("file:///"))
        return new File(uri).toPath();

    // transformation use raw path
    String p = uri.getRawPath();
    int len = p.length();
    if (len == 0)
        throw new IllegalArgumentException("URI path component is empty");

    // transform escaped octets and unescaped characters to bytes
    if (p.endsWith("/") && len > 1)
        p = p.substring(0, len - 1);

    return new NetPath(fs, p);
}
 
Example 12
Project: jdk8u-jdk   File: WindowsUriSupport.java   View Source Code Vote up 4 votes
/**
 * Converts given URI to a Path
 */
static WindowsPath fromUri(WindowsFileSystem fs, URI uri) {
    if (!uri.isAbsolute())
        throw new IllegalArgumentException("URI is not absolute");
    if (uri.isOpaque())
        throw new IllegalArgumentException("URI is not hierarchical");
    String scheme = uri.getScheme();
    if ((scheme == null) || !scheme.equalsIgnoreCase("file"))
        throw new IllegalArgumentException("URI scheme is not \"file\"");
    if (uri.getFragment() != null)
        throw new IllegalArgumentException("URI has a fragment component");
    if (uri.getQuery() != null)
        throw new IllegalArgumentException("URI has a query component");
    String path = uri.getPath();
    if (path.equals(""))
        throw new IllegalArgumentException("URI path component is empty");

    // UNC
    String auth = uri.getAuthority();
    if (auth != null && !auth.equals("")) {
        String host = uri.getHost();
        if (host == null)
            throw new IllegalArgumentException("URI authority component has undefined host");
        if (uri.getUserInfo() != null)
            throw new IllegalArgumentException("URI authority component has user-info");
        if (uri.getPort() != -1)
            throw new IllegalArgumentException("URI authority component has port number");

        // IPv6 literal
        // 1. drop enclosing brackets
        // 2. replace ":" with "-"
        // 3. replace "%" with "s" (zone/scopeID delimiter)
        // 4. Append .ivp6-literal.net
        if (host.startsWith("[")) {
            host = host.substring(1, host.length()-1)
                       .replace(':', '-')
                       .replace('%', 's');
            host += IPV6_LITERAL_SUFFIX;
        }

        // reconstitute the UNC
        path = "\\\\" + host + path;
    } else {
        if ((path.length() > 2) && (path.charAt(2) == ':')) {
            // "/c:/foo" --> "c:/foo"
            path = path.substring(1);
        }
    }
    return WindowsPath.parse(fs, path);
}
 
Example 13
Project: pgcodekeeper   File: JdbcConnector.java   View Source Code Vote up 4 votes
public JdbcConnector(String url) throws URISyntaxException {
    this.url = url;
    this.timezone = ApgdiffConsts.UTC;

    String host = null, user = null, pass = null, dbName = null;
    int port = -1;
    if (url.startsWith("jdbc:postgresql:")) {
        // strip jdbc:, URI doesn't understand schemas with colons
        URI uri = new URI(url.substring(5));

        if (uri.isOpaque()) {
            // special case for jdbc:postgres:database_name
            dbName = uri.getSchemeSpecificPart();
        } else {
            host = uri.getHost();
            port = uri.getPort();
            dbName = uri.getPath();
            if (dbName != null && !dbName.isEmpty()) {
                // strip leading /
                dbName = dbName.substring(1);
            }

            String query = uri.getQuery();
            if (query != null) {
                for (String param : query.split("&")) {
                    int eq = param.indexOf('=');
                    if (eq != -1) {
                        String key = param.substring(0, eq);
                        String val = param.substring(eq + 1);
                        switch (key) {
                        case "user":
                            user = val;
                            break;
                        case "password":
                            pass = val;
                            break;
                        }
                    }
                }
            }
        }
    }
    this.host = host == null ? "" : host;
    this.port = port < 1 ? ApgdiffConsts.JDBC_CONSTS.JDBC_DEFAULT_PORT : port;
    this.dbName = dbName == null ? "" : dbName;
    this.user = user == null || user.isEmpty() ? System.getProperty("user.name") : user;
    this.pass = pass == null || pass.isEmpty() ? getPgPassPassword() : pass;
}
 
Example 14
Project: OpenJSharp   File: WindowsUriSupport.java   View Source Code Vote up 4 votes
/**
 * Converts given URI to a Path
 */
static WindowsPath fromUri(WindowsFileSystem fs, URI uri) {
    if (!uri.isAbsolute())
        throw new IllegalArgumentException("URI is not absolute");
    if (uri.isOpaque())
        throw new IllegalArgumentException("URI is not hierarchical");
    String scheme = uri.getScheme();
    if ((scheme == null) || !scheme.equalsIgnoreCase("file"))
        throw new IllegalArgumentException("URI scheme is not \"file\"");
    if (uri.getFragment() != null)
        throw new IllegalArgumentException("URI has a fragment component");
    if (uri.getQuery() != null)
        throw new IllegalArgumentException("URI has a query component");
    String path = uri.getPath();
    if (path.equals(""))
        throw new IllegalArgumentException("URI path component is empty");

    // UNC
    String auth = uri.getAuthority();
    if (auth != null && !auth.equals("")) {
        String host = uri.getHost();
        if (host == null)
            throw new IllegalArgumentException("URI authority component has undefined host");
        if (uri.getUserInfo() != null)
            throw new IllegalArgumentException("URI authority component has user-info");
        if (uri.getPort() != -1)
            throw new IllegalArgumentException("URI authority component has port number");

        // IPv6 literal
        // 1. drop enclosing brackets
        // 2. replace ":" with "-"
        // 3. replace "%" with "s" (zone/scopeID delimiter)
        // 4. Append .ivp6-literal.net
        if (host.startsWith("[")) {
            host = host.substring(1, host.length()-1)
                       .replace(':', '-')
                       .replace('%', 's');
            host += IPV6_LITERAL_SUFFIX;
        }

        // reconstitute the UNC
        path = "\\\\" + host + path;
    } else {
        if ((path.length() > 2) && (path.charAt(2) == ':')) {
            // "/c:/foo" --> "c:/foo"
            path = path.substring(1);
        }
    }
    return WindowsPath.parse(fs, path);
}
 
Example 15
Project: OpenJSharp   File: WindowsUriSupport.java   View Source Code Vote up 4 votes
/**
 * Converts given URI to a Path
 */
static NetPath fromUri(NetFileSystem fs, URI uri) {
    if (!uri.isAbsolute())
        throw new IllegalArgumentException("URI is not absolute");
    if (uri.isOpaque())
        throw new IllegalArgumentException("URI is not hierarchical");
    String scheme = uri.getScheme();
    if ((scheme == null) || !scheme.equalsIgnoreCase("file"))
        throw new IllegalArgumentException("URI scheme is not \"file\"");
    if (uri.getFragment() != null)
        throw new IllegalArgumentException("URI has a fragment component");
    if (uri.getQuery() != null)
        throw new IllegalArgumentException("URI has a query component");
    String path = uri.getPath();
    if (path.equals(""))
        throw new IllegalArgumentException("URI path component is empty");

    // UNC
    String auth = uri.getAuthority();
    if (auth != null && !auth.equals("")) {
        String host = uri.getHost();
        if (host == null)
            throw new IllegalArgumentException("URI authority component has undefined host");
        if (uri.getUserInfo() != null)
            throw new IllegalArgumentException("URI authority component has user-info");
        if (uri.getPort() != -1)
            throw new IllegalArgumentException("URI authority component has port number");

        // IPv6 literal
        // 1. drop enclosing brackets
        // 2. replace ":" with "-"
        // 3. replace "%" with "s" (zone/scopeID delimiter)
        // 4. Append .ivp6-literal.net
        if (host.startsWith("[")) {
            host = host.substring(1, host.length()-1)
                       .replace(':', '-')
                       .replace('%', 's');
            host += IPV6_LITERAL_SUFFIX;
        }

        // reconstitute the UNC
        path = "\\\\" + host + path;
    } else {
        if ((path.length() > 2) && (path.charAt(2) == ':')) {
            // "/c:/foo" --> "c:/foo"
            path = path.substring(1);
        }
    }
    return new NetPath(fs, path);
}
 
Example 16
Project: openjdk-jdk10   File: File.java   View Source Code Vote up 3 votes
/**
 * Creates a new {@code File} instance by converting the given
 * {@code file:} URI into an abstract pathname.
 *
 * <p> The exact form of a {@code file:} URI is system-dependent, hence
 * the transformation performed by this constructor is also
 * system-dependent.
 *
 * <p> For a given abstract pathname <i>f</i> it is guaranteed that
 *
 * <blockquote><code>
 * new File(</code><i>&nbsp;f</i><code>.{@link #toURI()
 * toURI}()).equals(</code><i>&nbsp;f</i><code>.{@link #getAbsoluteFile() getAbsoluteFile}())
 * </code></blockquote>
 *
 * so long as the original abstract pathname, the URI, and the new abstract
 * pathname are all created in (possibly different invocations of) the same
 * Java virtual machine.  This relationship typically does not hold,
 * however, when a {@code file:} URI that is created in a virtual machine
 * on one operating system is converted into an abstract pathname in a
 * virtual machine on a different operating system.
 *
 * @param  uri
 *         An absolute, hierarchical URI with a scheme equal to
 *         {@code "file"}, a non-empty path component, and undefined
 *         authority, query, and fragment components
 *
 * @throws  NullPointerException
 *          If {@code uri} is {@code null}
 *
 * @throws  IllegalArgumentException
 *          If the preconditions on the parameter do not hold
 *
 * @see #toURI()
 * @see java.net.URI
 * @since 1.4
 */
public File(URI uri) {

    // Check our many preconditions
    if (!uri.isAbsolute())
        throw new IllegalArgumentException("URI is not absolute");
    if (uri.isOpaque())
        throw new IllegalArgumentException("URI is not hierarchical");
    String scheme = uri.getScheme();
    if ((scheme == null) || !scheme.equalsIgnoreCase("file"))
        throw new IllegalArgumentException("URI scheme is not \"file\"");
    if (uri.getRawAuthority() != null)
        throw new IllegalArgumentException("URI has an authority component");
    if (uri.getRawFragment() != null)
        throw new IllegalArgumentException("URI has a fragment component");
    if (uri.getRawQuery() != null)
        throw new IllegalArgumentException("URI has a query component");
    String p = uri.getPath();
    if (p.equals(""))
        throw new IllegalArgumentException("URI path component is empty");

    // Okay, now initialize
    p = fs.fromURIPath(p);
    if (File.separatorChar != '/')
        p = p.replace('/', File.separatorChar);
    this.path = fs.normalize(p);
    this.prefixLength = fs.prefixLength(this.path);
}
 
Example 17
Project: incubator-netbeans   File: LayerBuilder.java   View Source Code Vote up 3 votes
/**
 * Adds a URL-valued attribute.
 * @param attr the attribute name
 * @param value the attribute value, e.g. {@code "/my/module/resource.html"}
 *              or {@code "nbresloc:/my/module/resource.html"}; relative values permitted
 *              but not likely useful as base URL would be e.g. {@code "jar:...!/META-INF/"}
 * @return this builder
 * @throws LayerGenerationException in case an opaque URI is passed as {@code value}
 */
public File urlvalue(String attr, URI value) throws LayerGenerationException {
    if (value.isOpaque()) {
        throw new LayerGenerationException("Cannot use an opaque URI: " + value, originatingElement);
    }
    attrs.put(attr, new String[] {"urlvalue", value.toString()});
    return this;
}
 
Example 18
Project: jdk8u-jdk   File: File.java   View Source Code Vote up 3 votes
/**
 * Creates a new <tt>File</tt> instance by converting the given
 * <tt>file:</tt> URI into an abstract pathname.
 *
 * <p> The exact form of a <tt>file:</tt> URI is system-dependent, hence
 * the transformation performed by this constructor is also
 * system-dependent.
 *
 * <p> For a given abstract pathname <i>f</i> it is guaranteed that
 *
 * <blockquote><tt>
 * new File(</tt><i>&nbsp;f</i><tt>.{@link #toURI() toURI}()).equals(</tt><i>&nbsp;f</i><tt>.{@link #getAbsoluteFile() getAbsoluteFile}())
 * </tt></blockquote>
 *
 * so long as the original abstract pathname, the URI, and the new abstract
 * pathname are all created in (possibly different invocations of) the same
 * Java virtual machine.  This relationship typically does not hold,
 * however, when a <tt>file:</tt> URI that is created in a virtual machine
 * on one operating system is converted into an abstract pathname in a
 * virtual machine on a different operating system.
 *
 * @param  uri
 *         An absolute, hierarchical URI with a scheme equal to
 *         <tt>"file"</tt>, a non-empty path component, and undefined
 *         authority, query, and fragment components
 *
 * @throws  NullPointerException
 *          If <tt>uri</tt> is <tt>null</tt>
 *
 * @throws  IllegalArgumentException
 *          If the preconditions on the parameter do not hold
 *
 * @see #toURI()
 * @see java.net.URI
 * @since 1.4
 */
public File(URI uri) {

    // Check our many preconditions
    if (!uri.isAbsolute())
        throw new IllegalArgumentException("URI is not absolute");
    if (uri.isOpaque())
        throw new IllegalArgumentException("URI is not hierarchical");
    String scheme = uri.getScheme();
    if ((scheme == null) || !scheme.equalsIgnoreCase("file"))
        throw new IllegalArgumentException("URI scheme is not \"file\"");
    if (uri.getAuthority() != null)
        throw new IllegalArgumentException("URI has an authority component");
    if (uri.getFragment() != null)
        throw new IllegalArgumentException("URI has a fragment component");
    if (uri.getQuery() != null)
        throw new IllegalArgumentException("URI has a query component");
    String p = uri.getPath();
    if (p.equals(""))
        throw new IllegalArgumentException("URI path component is empty");

    // Okay, now initialize
    p = fs.fromURIPath(p);
    if (File.separatorChar != '/')
        p = p.replace('/', File.separatorChar);
    this.path = fs.normalize(p);
    this.prefixLength = fs.prefixLength(this.path);
}
 
Example 19
Project: OpenJSharp   File: File.java   View Source Code Vote up 3 votes
/**
 * Creates a new <tt>File</tt> instance by converting the given
 * <tt>file:</tt> URI into an abstract pathname.
 *
 * <p> The exact form of a <tt>file:</tt> URI is system-dependent, hence
 * the transformation performed by this constructor is also
 * system-dependent.
 *
 * <p> For a given abstract pathname <i>f</i> it is guaranteed that
 *
 * <blockquote><tt>
 * new File(</tt><i>&nbsp;f</i><tt>.{@link #toURI() toURI}()).equals(</tt><i>&nbsp;f</i><tt>.{@link #getAbsoluteFile() getAbsoluteFile}())
 * </tt></blockquote>
 *
 * so long as the original abstract pathname, the URI, and the new abstract
 * pathname are all created in (possibly different invocations of) the same
 * Java virtual machine.  This relationship typically does not hold,
 * however, when a <tt>file:</tt> URI that is created in a virtual machine
 * on one operating system is converted into an abstract pathname in a
 * virtual machine on a different operating system.
 *
 * @param  uri
 *         An absolute, hierarchical URI with a scheme equal to
 *         <tt>"file"</tt>, a non-empty path component, and undefined
 *         authority, query, and fragment components
 *
 * @throws  NullPointerException
 *          If <tt>uri</tt> is <tt>null</tt>
 *
 * @throws  IllegalArgumentException
 *          If the preconditions on the parameter do not hold
 *
 * @see #toURI()
 * @see java.net.URI
 * @since 1.4
 */
public File(URI uri) {

    // Check our many preconditions
    if (!uri.isAbsolute())
        throw new IllegalArgumentException("URI is not absolute");
    if (uri.isOpaque())
        throw new IllegalArgumentException("URI is not hierarchical");
    String scheme = uri.getScheme();
    if ((scheme == null) || !scheme.equalsIgnoreCase("file"))
        throw new IllegalArgumentException("URI scheme is not \"file\"");
    if (uri.getAuthority() != null)
        throw new IllegalArgumentException("URI has an authority component");
    if (uri.getFragment() != null)
        throw new IllegalArgumentException("URI has a fragment component");
    if (uri.getQuery() != null)
        throw new IllegalArgumentException("URI has a query component");
    String p = uri.getPath();
    if (p.equals(""))
        throw new IllegalArgumentException("URI path component is empty");

    // Okay, now initialize
    p = fs.fromURIPath(p);
    if (File.separatorChar != '/')
        p = p.replace('/', File.separatorChar);
    this.path = fs.normalize(p);
    this.prefixLength = fs.prefixLength(this.path);
}
 
Example 20
Project: truevfs   File: TUriHelper.java   View Source Code Vote up 2 votes
/**
 * Returns whether or not the given {@code uri} has an absolute path.
 * A URI has an absolute path if it doesn't need to be combined with
 * other path information in order to locate a file.
 *
 * @param  uri the URI to test.
 * @return Whether or not the given URI has an absolute path.
 */
static boolean hasAbsolutePath(URI uri) {
    return !uri.isOpaque() && Paths.isAbsolute(
            uri.getSchemeSpecificPart(), SEPARATOR_CHAR);
}