Example usage for java.net URL toString

List of usage examples for java.net URL toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Constructs a string representation of this URL .

Usage

From source file:net.yacy.cora.document.id.MultiProtocolURL.java

public static void main(final String[] args) {
    final String[][] test = new String[][] { new String[] { null, "file://y:/" },
            new String[] { null, "file://y:/yacy" }, new String[] { null, "file://y:/yacy/" },
            new String[] { null, "file://y:" }, new String[] { null, "file://Z:admin\\home" }, // thats wrong but may appear
            new String[] { null, "file://Z:\\admin\\home" },
            new String[] { null, "https://www.example.com/shoe/?p=2&ps=75#t={%22san_NaviPaging%22:2}" }, // ugly strange pagination link
            new String[] { null, "C:WINDOWS\\CMD0.EXE" }, new String[] { null, "file://C:WINDOWS\\CMD0.EXE" },
            new String[] { null, "file:///bin/yacy2" }, // file://<host>/<path> may have many '/' if the host is omitted and the path starts with '/'
            new String[] { null, "file:/bin/yacy1" }, // file://<host>/<path> may have many '/' if the host is omitted and the path starts with '/'
            new String[] { null, "file:C:WINDOWS\\CMD.EXE" },
            new String[] { null, "file:///C:WINDOWS\\CMD1.EXE" },
            new String[] { null, "file:///C|WINDOWS\\CMD2.EXE" },
            new String[] { null, "http://www.anomic.de/test/" }, new String[] { null, "http://www.anomic.de/" },
            new String[] { null, "http://www.anomic.de" },
            new String[] { null, "http://www.anomic.de/home/test?x=1#home" },
            new String[] { null, "http://www.anomic.de/home/test?x=1" },
            new String[] { null, "http://www.anomic.de/home/test#home" },
            new String[] { null, "ftp://ftp.anomic.de/home/test#home" },
            new String[] { null, "ftp://bob:builder@ftp.anomic.de/home/test.gif" },
            new String[] { null, "http://www.anomic.de/home/../abc/" },
            new String[] { null, "mailto:abcdefg@nomailnomail.com" },
            new String[] { "http://www.anomic.de/home", "test" },
            new String[] { "http://www.anomic.de/home", "test/" },
            new String[] { "http://www.anomic.de/home/", "test" },
            new String[] { "http://www.anomic.de/home/", "test/" },
            new String[] { "http://www.anomic.de/home/index.html", "test.htm" },
            new String[] { "http://www.anomic.de/home/index.html", "http://www.yacy.net/test" },
            new String[] { "http://www.anomic.de/home/index.html", "ftp://ftp.yacy.net/test" },
            new String[] { "http://www.anomic.de/home/index.html", "../test" },
            new String[] { "http://www.anomic.de/home/index.html", "mailto:abcdefg@nomailnomail.com" },
            new String[] { null, "news:de.test" }, new String[] { "http://www.anomic.de/home", "news:de.test" },
            new String[] { null, "mailto:bob@web.com" },
            new String[] { "http://www.anomic.de/home", "mailto:bob@web.com" },
            new String[] { "http://www.anomic.de/home", "ftp://ftp.anomic.de/src" },
            new String[] { null, "ftp://ftp.delegate.org/" },
            new String[] { "http://www.anomic.de/home", "ftp://ftp.delegate.org/" },
            new String[] { "http://www.anomic.de", "mailto:yacy@weltherrschaft.org" },
            new String[] { "http://www.anomic.de", "javascipt:temp" },
            new String[] { null,
                    "http://yacy-websuche.de/wiki/index.php?title=De:IntroInformationFreedom&action=history" },
            new String[] { null,
                    "http://diskusjion.no/index.php?s=5bad5f431a106d9a8355429b81bb0ca5&showuser=23585" },
            new String[] { null,
                    "http://diskusjion.no/index.php?s=5bad5f431a106d9a8355429b81bb0ca5&amp;showuser=23585" },
            new String[] { null,
                    "http://www.scc.kit.edu/publikationen/80.php?PHPSESSID=5f3624d3e1c33d4c086ab600d4d5f5a1" },
            new String[] { null, "smb://localhost/" }, new String[] { null, "smb://localhost/repository" }, // paths must end with '/'
            new String[] { null, "smb://localhost/repository/" }, new String[] { null, "\\\\localhost\\" }, // Windows-like notion of smb shares
            new String[] { null, "\\\\localhost\\repository" },
            new String[] { null, "\\\\localhost\\repository\\" },
            new String[] { null, "http://test.net/test1.htm?s=multiple&amp;a=amp&amp;b=in&amp;c=url" },
            new String[] { null, "http://test.net/test2.htm?s=multiple&amp;amp;amp;amp;a=amp" },
            new String[] { null, "http://validator.w3.org/check?uri=http://www.anomic.de/" } };
    //MultiProtocolURI.initSessionIDNames(FileUtils.loadList(new File("defaults/sessionid.names")));
    String environment, url;//from   w w  w .ja  v a2s . com
    MultiProtocolURL aURL, aURL1;
    java.net.URL jURL;
    for (String[] element : test) {
        environment = element[0];
        url = element[1];
        try {
            aURL = MultiProtocolURL.newURL(environment, url);
        } catch (final MalformedURLException e) {
            e.printStackTrace();
            aURL = null;
        }
        if (environment == null) {
            try {
                jURL = new java.net.URL(url);
            } catch (final MalformedURLException e) {
                jURL = null;
            }
        } else {
            try {
                jURL = new java.net.URL(new java.net.URL(environment), url);
            } catch (final MalformedURLException e) {
                jURL = null;
            }
        }

        // check equality to java.net.URL
        if (((aURL == null) && (jURL != null)) || ((aURL != null) && (jURL == null)) || ((aURL != null)
                && (jURL != null) && (!(jURL.toString().equals(aURL.toNormalform(false)))))) {
            System.out.println("Difference for environment=" + environment + ", url=" + url + ":");
            System.out.println((jURL == null) ? "jURL rejected input" : "jURL=" + jURL.toString());
            System.out.println((aURL == null) ? "aURL rejected input"
                    : "aURL=" + aURL.toNormalform(false) + "; host=" + aURL.getHost() + "; path="
                            + aURL.getPath() + "; file=" + aURL.getFile());
        }

        if (aURL != null && jURL != null && jURL.toString().equals(aURL.toNormalform(false))) {
            System.out.println("jURL == aURL=" + aURL.toNormalform(false) + "; host=" + aURL.getHost()
                    + "; path=" + aURL.getPath() + "; file=" + aURL.getFile());
        }

        // check stability: the normalform of the normalform must be equal to the normalform
        if (aURL != null)
            try {
                aURL1 = new MultiProtocolURL(aURL.toNormalform(false));
                if (!(aURL1.toNormalform(false).equals(aURL.toNormalform(false)))) {
                    System.out.println("no stability for url:");
                    System.out.println("aURL0=" + aURL.toNormalform(false));
                    System.out.println("aURL1=" + aURL1.toNormalform(false));
                }
            } catch (final MalformedURLException e) {
                System.out.println("no stability for url:");
                System.out.println("aURL0=" + aURL.toNormalform(false));
                System.out.println("aURL1 cannot be computed:" + e.getMessage());
            }
    }
}

From source file:BrowserLaunch.java

public static void launch(URL url) {
    launch(url.toString());
}

From source file:Main.java

public static URL getString(URL str) {
    String ssString = str.toString();
    String[] aaStrings = ssString.split("/");
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < aaStrings.length; i++) {
        if (i == 4) {
            aaStrings[i] = "180";
        }//from  w  ww. j  a v  a  2 s .  c om
        sb.append(aaStrings[i] + "/");
    }
    sb.deleteCharAt(sb.length() - 1);
    URL url = null;
    try {
        url = new URL(sb.toString());
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    return url;
}

From source file:Main.java

public static Document load(URL url) {
    return load(new InputSource(url.toString()));
}

From source file:Main.java

/**
 * @param url The path to inspect./*from w  ww.j a v  a2  s.  c  o  m*/
 * @return TRUE if the passed URL is contained in a JAR file.
 * @throws IOException
 */
public static boolean isUrlInJar(URL url) throws IOException {
    return url == null ? false : url.toString().startsWith("jar:");
}

From source file:Main.java

private static File getFile(URL url) {

    String dir = url.toString();
    File md;// w w  w  .  j a  v a2 s  . c  om

    try {
        md = new File(new URI(dir));
    } catch (IllegalArgumentException e) {

        // this is an attempt at a crazy workaround that should not really work
        if (dir.startsWith("file://")) {
            md = new File(dir.substring(5, dir.length()).replaceAll("\\%20", " "));
        } else {
            System.err.println("this should never happen! bad file: " + dir);
            md = new File("maps/");
        }
    } catch (Exception e) {
        throw new RuntimeException("Cant create file: " + dir, e);
    }

    return md;

}

From source file:Main.java

public static String normalizeUrl(String sourceUrl) {
    String normalizedUrl = "";
    try {//from w w w .  j a  v a2 s. c  o m
        URL url = new URL(sourceUrl);
        normalizedUrl = url.toString();
        normalizedUrl = normalizedUrl.replace("\n", "");
        normalizedUrl = normalizedUrl.replace(" ", "");
        if (!normalizedUrl.endsWith("/"))
            normalizedUrl = normalizedUrl + "/";
    } catch (MalformedURLException e) {
        Log.e(TAG, "normalizeUrl: invalid URL");
    }
    return normalizedUrl;
}

From source file:Main.java

/**
 * @param urlForDomain extract the domain from this url
 * @param path         this url does not have a domain
 * @return/*from  w ww .  ja v  a2s .com*/
 */
public static String useDomainOfFirstArg4Second(String urlForDomain, String path) {
    try {
        // See: http://stackoverflow.com/questions/1389184/building-an-absolute-url-from-a-relative-url-in-java
        URL baseUrl = new URL(urlForDomain);
        URL relativeurl = new URL(baseUrl, path);
        return relativeurl.toString();
    } catch (MalformedURLException ex) {
        return path;
    }
}

From source file:Main.java

/**
 * @deprecated use {@link ResourceLocator.resourceURL(String)} instead. 
 *//* ww w . jav a  2s .c o  m*/
@Deprecated
public static String resourceURL(String name) {
    ClassLoader tcl = Thread.currentThread().getContextClassLoader();
    URL url = tcl.getResource(name);
    return url != null ? url.toString() : null;
}

From source file:com.reprezen.kaizen.oasparser.jsonoverlay.JsonLoader.java

public static JsonNode load(URL url) throws IOException {
    String urlString = url.toString();
    if (cache.containsKey(urlString)) {
        return cache.get(urlString);
    }/*  w w w.j a v a  2  s .  co m*/
    String json = IOUtils.toString(url.openStream(), Charsets.UTF_8);
    return loadString(url, json);
}