Java Path to URL getURLPathFromURI(String uri)

Here you can find the source of getURLPathFromURI(String uri)

Description

get URL Path From URI

License

Open Source License

Declaration

public static String getURLPathFromURI(String uri) throws URISyntaxException 

Method Source Code

//package com.java2s;
/*//w  w w.j  av a2 s. c om
 * ====================================================================
 *
 * This code is subject to the freebxml License, Version 1.1
 *
 * Copyright (c) 2001 - 2003 freebxml.org.  All rights reserved.
 *
 * $Header: /cvsroot/ebxmlrr/omar/src/java/org/freebxml/omar/common/Utility.java,v 1.40 2007/05/18 18:58:59 psterk Exp $
 * ====================================================================
 */

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

public class Main {
    public static final String FILE_SEPARATOR = System.getProperty("file.separator");

    public static String getURLPathFromURI(String uri) throws URISyntaxException {
        String path = null;

        try {
            URL url = new URL(uri);
            path = url.getPath();
        } catch (MalformedURLException e) {
            //Not a URL. Convert to URN
            String urn = fixURN(uri);
            path = getURLPathFromURN(urn);
        }

        return path;
    }

    /**
     * Fixes an input URN to comply with URN syntax by replacing invalid chars with '_'
     */
    public static String fixURN(String inputId) {
        String outputId = inputId.replace(FILE_SEPARATOR.charAt(0), ':');
        if (!(outputId.startsWith("urn:"))) {
            outputId = "urn:" + outputId;
        }
        outputId = outputId.replaceAll("[^a-zA-Z_0-9:_]", "_");
        return outputId;
    }

    public static String getURLPathFromURN(String urn) throws URISyntaxException {
        String path = null;

        URI uri = new java.net.URI(urn);
        path = urn.replaceAll("[:]", "/");
        return path;
    }
}

Related

  1. getURLPath(URI uri)
  2. getURLPath(URL repositoryURL)
  3. getURLPath(URL url)
  4. getUrlPath(URL url)
  5. getUrlPath(URL url)
  6. getURLPathFromURN(String urn)
  7. getURLs(List paths)
  8. getURLs(String thePaths[])
  9. getUrlsFromClassPath(String path)