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

The following examples show how to use java.net.URI#isOpaque() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConfigDescription.java    From openhab-core with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Creates a new instance of this class with the specified parameters.
 *
 * @param uri the URI of this description within the {@link ConfigDescriptionRegistry}
 * @param parameters the list of configuration parameters that belong to the given URI
 * @param groups the list of groups associated with the parameters
 * @throws IllegalArgumentException if the URI is null or invalid
 * @deprecated Use the {@link ConfigDescriptionBuilder} instead.
 */
@Deprecated
ConfigDescription(URI uri, @Nullable List<ConfigDescriptionParameter> parameters,
        @Nullable List<ConfigDescriptionParameterGroup> groups) {
    if (uri == null) {
        throw new IllegalArgumentException("The URI must not be null!");
    }
    if (!uri.isAbsolute()) {
        throw new IllegalArgumentException("The scheme is missing!");
    }
    if (!uri.isOpaque()) {
        throw new IllegalArgumentException("The scheme specific part (token) must not start with a slash ('/')!");
    }

    this.uri = uri;
    this.parameters = parameters == null ? Collections.emptyList() : Collections.unmodifiableList(parameters);
    this.parameterGroups = groups == null ? Collections.emptyList() : Collections.unmodifiableList(groups);
}
 
Example 2
Source File: Sources.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public Source append(Source child) {
  if (isFile(child)) {
    if (child.file().isAbsolute()) {
      return child;
    }
  } else {
    try {
      URI uri = child.url().toURI();
      if (!uri.isOpaque()) {
        // The URL is "absolute" (it starts with a slash)
        return child;
      }
    } catch (URISyntaxException e) {
      throw new IllegalArgumentException("Unable to convert URL " + child.url() + " to URI", e);
    }
  }
  String path = child.path();
  if (!urlGenerated) {
    String encodedPath = new File(".").toURI().relativize(new File(path).toURI())
        .getRawSchemeSpecificPart();
    return Sources.url(url + "/" + encodedPath);
  } else {
    return Sources.file(file, path);
  }
}
 
Example 3
Source File: LabelBuilder.java    From allure-java with Apache License 2.0 6 votes vote down vote up
private Optional<String> featurePackage(final String uriString, final String featureName) {
    final Optional<URI> maybeUri = safeUri(uriString);
    if (!maybeUri.isPresent()) {
        return Optional.empty();
    }
    URI uri = maybeUri.get();

    if (!uri.isOpaque()) {
        final URI work = new File("").toURI();
        uri = work.relativize(uri);
    }
    final String schemeSpecificPart = uri.normalize().getSchemeSpecificPart();
    final Stream<String> folders = Stream.of(schemeSpecificPart.replaceAll("\\.", "_").split("/"));
    final Stream<String> name = Stream.of(featureName);
    return Optional.of(Stream.concat(folders, name)
            .filter(Objects::nonNull)
            .filter(s -> !s.isEmpty())
            .collect(Collectors.joining(".")));
}
 
Example 4
Source File: SFTPFileSystemProvider.java    From sftp-fs with Apache License 2.0 6 votes vote down vote up
private void checkURI(URI uri, boolean allowUserInfo, boolean allowPath) {
    if (!uri.isAbsolute()) {
        throw Messages.uri().notAbsolute(uri);
    }
    if (!getScheme().equalsIgnoreCase(uri.getScheme())) {
        throw Messages.uri().invalidScheme(uri, getScheme());
    }
    if (!allowUserInfo && uri.getUserInfo() != null && !uri.getUserInfo().isEmpty()) {
        throw Messages.uri().hasUserInfo(uri);
    }
    if (uri.isOpaque()) {
        throw Messages.uri().notHierarchical(uri);
    }
    if (!allowPath && uri.getPath() != null && !uri.getPath().isEmpty()) {
        throw Messages.uri().hasPath(uri);
    }
    if (uri.getQuery() != null && !uri.getQuery().isEmpty()) {
        throw Messages.uri().hasQuery(uri);
    }
    if (uri.getFragment() != null && !uri.getFragment().isEmpty()) {
        throw Messages.uri().hasFragment(uri);
    }
}
 
Example 5
Source File: URIPattern.java    From crate with Apache License 2.0 6 votes vote down vote up
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 6
Source File: Sources.java    From calcite with Apache License 2.0 6 votes vote down vote up
private static File urlToFile(URL url) {
  if (!"file".equals(url.getProtocol())) {
    return null;
  }
  URI uri;
  try {
    uri = url.toURI();
  } catch (URISyntaxException e) {
    throw new IllegalArgumentException("Unable to convert URL " + url + " to URI", e);
  }
  if (uri.isOpaque()) {
    // It is like file:test%20file.c++
    // getSchemeSpecificPart would return "test file.c++"
    return new File(uri.getSchemeSpecificPart());
  }
  // See https://stackoverflow.com/a/17870390/1261287
  return Paths.get(uri).toFile();
}
 
Example 7
Source File: Input.java    From jaamsim with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a file path entry in a configuration file to a URI.
 * @param kw - keyword input containing the file path data
 * @return the URI corresponding to the file path data.
 * @throws InputErrorException
 */
public static URI parseURI(KeywordIndex kw)
throws InputErrorException {
	Input.assertCount(kw, 1);

	String arg = kw.getArg(0);

	// Convert the file path to a URI
	URI uri = null;
	try {
		if (kw.context != null)
			uri = InputAgent.getFileURI(kw.context.context, arg, kw.context.jail);
		else
			uri = InputAgent.getFileURI(null, arg, null);
	}
	catch (URISyntaxException ex) {
		throw new InputErrorException("File Entity parse error: %s", ex.getMessage());
	}

	if (uri == null)
		throw new InputErrorException("Unable to parse the file path:\n%s", arg);

	if (!uri.isOpaque() && uri.getPath() == null)
		 throw new InputErrorException("Unable to parse the file path:\n%s", arg);

	return uri;
}
 
Example 8
Source File: DataUriFetcher.java    From caja with Apache License 2.0 5 votes vote down vote up
private static boolean isDataUri(URI uri) {
  if (null != uri  && "data".equals(uri.getScheme())
      && uri.isOpaque()) {
    return true;
  }
  return false;
}
 
Example 9
Source File: ConfigDescription.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Creates a new instance of this class with the specified parameters.
 *
 * @param uri the URI of this description within the {@link ConfigDescriptionRegistry} (must neither be null nor
 *            empty)
 * @param parameters the list of configuration parameters that belong to the given URI
 *            (could be null or empty)
 * @param groups the list of groups associated with the parameters
 * @throws IllegalArgumentException if the URI is null or invalid
 */
public ConfigDescription(URI uri, List<ConfigDescriptionParameter> parameters,
        List<ConfigDescriptionParameterGroup> groups) {
    if (uri == null) {
        throw new IllegalArgumentException("The URI must not be null!");
    }
    if (!uri.isAbsolute()) {
        throw new IllegalArgumentException("The scheme is missing!");
    }
    if (!uri.isOpaque()) {
        throw new IllegalArgumentException("The scheme specific part (token) must not start with a slash ('/')!");
    }

    this.uri = uri;

    if (parameters != null) {
        this.parameters = Collections.unmodifiableList(parameters);
    } else {
        this.parameters = Collections.unmodifiableList(new ArrayList<ConfigDescriptionParameter>(0));
    }

    if (groups != null) {
        this.parameterGroups = Collections.unmodifiableList(groups);
    } else {
        this.parameterGroups = Collections.unmodifiableList(new ArrayList<ConfigDescriptionParameterGroup>(0));
    }
}
 
Example 10
Source File: WindowsUriSupport.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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 11
Source File: HttpRequest.java    From timbuctoo with GNU General Public License v3.0 4 votes vote down vote up
public HttpRequest(
  String method,
  String url,
  LinkedListMultimap<String, String> headers,
  String body,
  String server,
  LinkedListMultimap<String, String> queryParameters
) {
  if (url != null) {
    URI uri = URI.create(url);
    if (uri.isOpaque()) {
      throw new IllegalArgumentException("Should be a URL, not a URI");
    }
    this.path = uri.getPath();
    if (uri.isAbsolute() && server == null) {
      this.server = uri.getScheme() + "://" + uri.getRawAuthority();
    } else {
      this.server = server;
    }
    //if you have query parameters in the base url _and_ separate query parameters then the result is a concatenation
    if (uri.getRawQuery() != null) {
      this.queryParameters = LinkedListMultimap.create();
      for (String querySegment : Splitter.on("&").split(uri.getRawQuery())) {
        int equalsLocation = querySegment.indexOf('=');
        String key = querySegment.substring(0, equalsLocation);
        String value = querySegment.substring(equalsLocation + 1);
        this.queryParameters.put(key, value);
      }
      if (queryParameters != null) {
        this.queryParameters.putAll(queryParameters);
      }
    } else {
      this.queryParameters = queryParameters;
    }
  } else {
    this.server = server;
    this.path = "";
    this.queryParameters = queryParameters;
  }
  this.method = method;
  this.headers = headers;
  this.body = body;
}
 
Example 12
Source File: WindowsUriSupport.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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
Source File: TGFileUtils.java    From tuxguitar with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static String getDecodedFileName(URI uri) {
	return (!uri.isOpaque() ? getDecodedFileName(uri.getPath()) : null);
}
 
Example 14
Source File: WindowsUriSupport.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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.getRawFragment() != null)
        throw new IllegalArgumentException("URI has a fragment component");
    if (uri.getRawQuery() != 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.getRawAuthority();
    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
Source File: File.java    From jdk8u-dev-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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 16
Source File: File.java    From jdk-1.7-annotated with Apache License 2.0 3 votes vote down vote up
/**
 * 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 17
Source File: File.java    From jdk8u_jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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 18
Source File: File.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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
Source File: File.java    From jdk8u-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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
Source File: File.java    From Java8CN with Apache License 2.0 3 votes vote down vote up
/**
 * 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);
}