Example usage for java.net URL getPath

List of usage examples for java.net URL getPath

Introduction

In this page you can find the example usage for java.net URL getPath.

Prototype

public String getPath() 

Source Link

Document

Gets the path part of this URL .

Usage

From source file:android.databinding.tool.store.LayoutFileParser.java

public static File urlToFile(URL url) throws MalformedURLException {
    try {/*from  w  w  w.j a  va2s. c  o  m*/
        return new File(url.toURI());
    } catch (IllegalArgumentException e) {
        MalformedURLException ex = new MalformedURLException(e.getLocalizedMessage());
        ex.initCause(e);
        throw ex;
    } catch (URISyntaxException e) {
        return new File(url.getPath());
    }
}

From source file:UrlUtils.java

/**
 * Creates and returns a new URL identical to the specified URL, except using the specified port.
 * @param u the URL on which to base the returned URL
 * @param newPort the new port to use in the returned URL
 * @return a new URL identical to the specified URL, except using the specified port
 * @throws MalformedURLException if there is a problem creating the new URL
 *//*from   w w  w.  j  a va 2 s  .  c  om*/
public static URL getUrlWithNewPort(final URL u, final int newPort) throws MalformedURLException {
    return createNewUrl(u.getProtocol(), u.getHost(), newPort, u.getPath(), u.getRef(), u.getQuery());
}

From source file:UrlUtils.java

/**
 * Creates and returns a new URL identical to the specified URL, except using the specified host.
 * @param u the URL on which to base the returned URL
 * @param newHost the new host to use in the returned URL
 * @return a new URL identical to the specified URL, except using the specified host
 * @throws MalformedURLException if there is a problem creating the new URL
 *//*from w ww  .j av a2 s .  c  o m*/
public static URL getUrlWithNewHost(final URL u, final String newHost) throws MalformedURLException {
    return createNewUrl(u.getProtocol(), newHost, u.getPort(), u.getPath(), u.getRef(), u.getQuery());
}

From source file:UrlUtils.java

/**
 * Creates and returns a new URL identical to the specified URL, except using the specified reference.
 * @param u the URL on which to base the returned URL
 * @param newRef the new reference to use in the returned URL
 * @return a new URL identical to the specified URL, except using the specified reference
 * @throws MalformedURLException if there is a problem creating the new URL
 *///from  w w  w .j  a  va2s  .  c  o m
public static URL getUrlWithNewRef(final URL u, final String newRef) throws MalformedURLException {
    return createNewUrl(u.getProtocol(), u.getHost(), u.getPort(), u.getPath(), newRef, u.getQuery());
}

From source file:UrlUtils.java

/**
 * Creates and returns a new URL identical to the specified URL, except using the specified protocol.
 * @param u the URL on which to base the returned URL
 * @param newProtocol the new protocol to use in the returned URL
 * @return a new URL identical to the specified URL, except using the specified protocol
 * @throws MalformedURLException if there is a problem creating the new URL
 *//*from   ww w.j av  a2 s  .co m*/
public static URL getUrlWithNewProtocol(final URL u, final String newProtocol) throws MalformedURLException {
    return createNewUrl(newProtocol, u.getHost(), u.getPort(), u.getPath(), u.getRef(), u.getQuery());
}

From source file:UrlUtils.java

/**
 * Creates and returns a new URL identical to the specified URL, except using the specified query string.
 * @param u the URL on which to base the returned URL
 * @param newQuery the new query string to use in the returned URL
 * @return a new URL identical to the specified URL, except using the specified query string
 * @throws MalformedURLException if there is a problem creating the new URL
 *//*from  www .ja v  a 2s  . com*/
public static URL getUrlWithNewQuery(final URL u, final String newQuery) throws MalformedURLException {
    return createNewUrl(u.getProtocol(), u.getHost(), u.getPort(), u.getPath(), u.getRef(), newQuery);
}

From source file:com.t3.persistence.FileUtil.java

/**
 * Given a URL this method determines the content type of the URL (if possible) and
 * then returns a Reader with the appropriate character encoding.
 * /* www .  ja  va 2  s  .c  o  m*/
 * @param url the source of the data stream
 * @return String representing the data
 * @throws IOException
 */
public static Reader getURLAsReader(URL url) throws IOException {
    InputStreamReader isr = null;
    URLConnection conn = null;
    String encoding = "UTF-8";
    // We're assuming character here, but it could be bytes.  Perhaps we should
    // check the MIME type returned by the network server?
    conn = url.openConnection();
    if (log.isDebugEnabled()) {
        String type = URLConnection.guessContentTypeFromName(url.getPath());
        log.debug("result from guessContentTypeFromName(" + url.getPath() + ") is " + type);
        type = getContentType(conn.getInputStream());
        // Now make a guess and change 'encoding' to match the content type...
    }
    isr = new InputStreamReader(conn.getInputStream(), encoding);
    return isr;
}

From source file:msi.gama.application.workspace.WorkspaceModelsManager.java

/**
 * @param plugin/* w w w  . j  av  a 2s. c o  m*/
 */

private static void linkModelsToWorkspace(final String plugin, final String path, final boolean core) {
    final IWorkspace workspace = ResourcesPlugin.getWorkspace();
    final URL urlRep = null;
    File modelsRep = null;
    try {
        final String ext = path == "." ? "/" : "/" + path + "/";
        // urlRep = FileLocator.toFileURL(new URL("platform:/plugin/" + plugin + ext));
        // urlRep = urlRep.toURI().normalize().toURL();

        // urlRep = FileLocator.resolve(new URL("platform:/plugin/" + plugin + ext));

        final URL new_url = FileLocator.resolve(new URL("platform:/plugin/" + plugin + ext));
        final String path_s = new_url.getPath().replaceFirst("^/(.:/)", "$1");
        final java.nio.file.Path normalizedPath = Paths.get(path_s).normalize();
        // urlRep = normalizedPath.toUri().toURL();
        modelsRep = normalizedPath.toFile();

    } catch (final IOException e) {
        e.printStackTrace();
        return;
    } /*
        * catch (URISyntaxException e) {
        * e.printStackTrace();
        * }
        */

    // File modelsRep = new File(urlRep.getPath());
    // System.out.println("chargemen" + modelsRep.getAbsolutePath());
    final Map<File, IPath> foundProjects = new HashMap<>();
    findProjects(modelsRep, foundProjects);
    importBuiltInProjects(plugin, core, workspace, foundProjects);

    if (core)
        stampWorkspaceFromModels();

}

From source file:fr.gael.dhus.service.ProductService.java

private static boolean checkUrl(URL url) {
    Objects.requireNonNull(url, "`url` parameter must not be null");

    // OData Synchronized product, DELME
    if (url.getPath().endsWith("$value")) {
        // Ignoring ...
        return true;
    }/*  w w  w  .  ja va 2 s.  co m*/

    // Case of simple file
    try {
        File f = new File(url.toString());
        if (f.exists())
            return true;
    } catch (Exception e) {
        logger.debug("url \"" + url + "\" not formatted as a file");
    }

    // Case of local URL
    try {
        URI local = new File(".").toURI();
        URI uri = local.resolve(url.toURI());
        File f = new File(uri);
        if (f.exists())
            return true;
    } catch (Exception e) {
        logger.debug("url \"" + url + "\" not a local URL");
    }

    // Case of remote URL
    try {
        URLConnection con = url.openConnection();
        con.connect();
        InputStream is = con.getInputStream();
        is.close();
        return true;
    } catch (Exception e) {
        logger.debug("url \"" + url + "\" not a remote URL");
    }
    // Unrecovrable case
    return false;
}

From source file:com.tesora.dve.common.PEFileUtils.java

private static String getCanonicalPathFromURL(final URL url) throws PEException {
    File resourceFile;/*from  w  ww . ja  va2  s .  com*/
    try {
        final String os = System.getProperty("os.name");
        if (StringUtils.containsIgnoreCase(os, "win")) {
            return url.getFile();
        }

        resourceFile = new File(url.toURI());
    } catch (final URISyntaxException e) {
        resourceFile = new File(url.getPath());
    }

    try {
        return resourceFile.getCanonicalPath();
    } catch (final IOException e) {
        throw new PEException("Could not canonicalize the path '" + resourceFile.getAbsolutePath() + "'", e);
    }
}