Java Path to URL pathAsUrlString(String path)

Here you can find the source of pathAsUrlString(String path)

Description

Convert separators in a filesystem path to URL separators.

License

Open Source License

Parameter

Parameter Description
path a parameter

Declaration

public final static String pathAsUrlString(String path) 

Method Source Code


//package com.java2s;
/*// www  .  j a  va2  s  .  c o m
 * Copyright (c) 2006-2009 by Dirk Riehle, http://dirkriehle.com
 *
 * This file is part of the Wahlzeit photo rating application.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */

import java.io.File;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    /**
     * The string, which separates path segments in a URL
     */
    private static final String URL_SEPARATOR = "/";

    /**
     * Convert separators in a filesystem path to URL separators. It does not escape the URL characters. Use
     * java.net.URLEncoder for this.
     *
     * @param path
     * @return
     * @fixme Review for performance
     */
    public final static String pathAsUrlString(String path) {
        if (!File.separator.equals(URL_SEPARATOR)) {
            // We are not on a platform where file separator matches the url separator,
            // we need to convert between them.
            path = path.replaceAll(Pattern.quote(File.separator), Matcher.quoteReplacement(URL_SEPARATOR));
        }
        return path;
    }
}

Related

  1. getURLs(List paths)
  2. getURLs(String thePaths[])
  3. getUrlsFromClassPath(String path)
  4. getWebDocInfoStr(String urlPath)
  5. getWorkspaceURL(String subpath)
  6. pathFromURL(final URL url)
  7. pathsMatch(URL realm, URL returnTo)
  8. pathsToURLs(ClassLoader classLoader, String... paths)
  9. pathToUrl(String path)