Java URI to Relative URI getRelativeURI(String href)

Here you can find the source of getRelativeURI(String href)

Description

Returns the relative URI of the href argument

License

Open Source License

Return

the relative URI the given href

Declaration

public static String getRelativeURI(String href) 

Method Source Code


//package com.java2s;
import java.io.*;
import java.net.URL;
import java.net.MalformedURLException;

public class Main {
    /**//from w w  w.  j a va  2  s .  c o  m
     * the path separator for an URI
     */
    private static final char HREF_PATH_SEP = '/';

    /**
     * Returns the relative URI of the href argument
     *
     * @return the relative URI the given href
     **/
    public static String getRelativeURI(String href) {

        if (href == null)
            return href;

        int idx = -1;
        // -- check for URL
        try {
            // -- try to create a new URL and see if MalformedURLExcetion is
            // -- ever thrown
            new URL(href);
            idx = href.lastIndexOf(HREF_PATH_SEP);
        } catch (MalformedURLException muex) {
            // -- The following contains a fix from Shane Hathaway
            // -- to handle the case when both "\" and "/" appear in filename
            int idx2 = href.lastIndexOf(HREF_PATH_SEP);
            idx = href.lastIndexOf(File.separator);
            if (idx2 > idx)
                idx = idx2;
        }

        if (idx >= 0)
            return href.substring(idx + 1);

        return href;
    }
}

Related

  1. getRelativeLocalURI(String suffix)
  2. getRelativeName(File file, URI directory)
  3. getRelativePath(URI base, File file)
  4. getRelativePath(URI targetURI, URI baseURI)
  5. getRelativePath(URI targetUri, URI baseUri)
  6. getRelativeURI(URI base, URI uri)
  7. getRelativeURI(URI repositoryXml, URI bundleJar)
  8. getRelativeUri(URI rootURI, File file)
  9. getRelativeURI(URI uri, URI relativeTo)