Java Utililty Methods URI Normalize

List of utility methods to do URI Normalize

Description

The list of methods to do URI Normalize are organized into topic(s).

Method

Stringnormalize(final String uri)
normalize
URL url = null;
try {
    url = new URL(uri);
    return url.toString().replace("\\", "/");
} catch (MalformedURLException e1) {
    try {
        File file = new File(uri);
        if (file.exists()) {
...
Stringnormalize(String uri, String resourceLocation, String rootLocation)
a 'null' rootLocation argument will causes uri that begins with a '/' to be treated as a workspace relative resource (i.e.
String result = null;
if (uri != null) {
    if (hasProtocol(uri)) {
        if (isPlatformResourceProtocol(uri)) {
            result = resolvePlatformUrl(uri);
        } else {
            result = uri;
    if (result == null) {
        if (uri.indexOf(":") != -1 || uri.startsWith("/") || uri.startsWith("\\")) {
            result = uri;
    if (result == null && resourceLocation != null) {
        if (resourceLocation.endsWith("/")) {
            result = resourceLocation + uri;
        } else {
            result = resourceLocation + "/../" + uri;
    if (result == null) {
        result = uri;
    result = normalize(result);
return result;
URInormalize(URI input)
normalize
URI uri = null;
if (input != null) {
    uri = input.normalize();
    if ("file".equals(uri.getScheme())) {
        try {
            uri = new File(uri).getCanonicalFile().toURI();
        } catch (IOException e) {
            uri = new File(uri).toURI();
...
URInormalize(URI location)
normalize
try {
    String path = location.getPath();
    if (path != null && path.endsWith("/")) {
        return new URI(location.getScheme(), location.getAuthority(), path.substring(0, path.length() - 1),
                location.getQuery(), location.getFragment());
    } else {
        return location;
} catch (URISyntaxException e) {
    throw new RuntimeException(e);
URInormalize(URI uri)
normalize
String path = uri.toString();
if (path.startsWith("jar:")) {
    path = path.substring("jar:".length());
    uri = new URI(path);
return uri;
StringnormalizedSetCookiePath(final String path, final URI originUri)
normalized Set Cookie Path
if (path == null || path.equals("") || path.charAt(0) != '/') {
    return defaultSetCookiePath(originUri);
return path;
URInormalizedUri(URI uri)
normalized Uri
try {
    return new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), null, null, null);
} catch (URISyntaxException e) {
    throw Throwables.propagate(e);
URInormalizeGitRepoLocation(URI location)
Normalizes a git repository location so that it never ends with ".git".
final String locationString = location.toString();
if (locationString.endsWith("/")) { 
    try {
        return new URI(locationString.substring(0, locationString.lastIndexOf("/"))); 
    } catch (URISyntaxException e) {
if (locationString.endsWith(".git")) { 
...
URInormalizeLink(String link, URI parent, boolean removeParams)
Normalizes a link: removes all redundant spaces and parameters.
link = link.toLowerCase().trim().replace('\\', '/').replace(" ", "%20");
URI mergedURL;
if (parent == null) {
    try {
        mergedURL = URI.create(link);
    } catch (Exception ex) {
        return null;
    if (mergedURL.getScheme() == null) {
        return null;
} else {
    try {
        mergedURL = URI.create(parent.toString() + "/").resolve(link);
    } catch (Exception ex) {
        return null;
if (removeParams) {
    try {
        mergedURL = new URI(mergedURL.getScheme(), mergedURL.getUserInfo(), mergedURL.getHost(),
                mergedURL.getPort(), mergedURL.getPath(), null, null);
    } catch (URISyntaxException e) {
        return null;
StringBuilder stringURL = new StringBuilder(mergedURL.toString());
while (stringURL.lastIndexOf("/") == stringURL.length() - 1
        || stringURL.lastIndexOf(".") == stringURL.length() - 1) {
    stringURL.replace(stringURL.length() - 1, stringURL.length(), "");
return URI.create(stringURL.toString());
URInormalizeURI(final URI uri)
Canonicalizes the URI into "TFS format".
return normalizeURI(uri, false);