This project has retired. For details please refer to its Attic page.
Source code
001package org.apache.archiva.common.utils;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.commons.lang3.StringUtils;
023
024import java.net.MalformedURLException;
025import java.net.URI;
026import java.nio.file.Path;
027import java.nio.file.Paths;
028
029/**
030 * PathUtil - simple utility methods for path manipulation.
031 *
032 *
033 */
034public class PathUtil
035{
036    public static String toUrl( String path )
037    {
038        // Is our work already done for us?
039        if ( path.startsWith( "file:/" ) )
040        {
041            return path;
042        }
043
044        return toUrl( Paths.get( path ) );
045    }
046
047    public static String toUrl( Path file )
048    {
049        try
050        {
051            return file.toUri().toURL().toExternalForm();
052        }
053        catch ( MalformedURLException e )
054        {
055            String pathCorrected = StringUtils.replaceChars( file.toAbsolutePath().toString(), '\\', '/' );
056            if ( pathCorrected.startsWith( "file:/" ) )
057            {
058                return pathCorrected;
059            }
060
061            return "file://" + pathCorrected;
062        }
063    }
064
065    /**
066     * Given a basedir and a child file, return the relative path to the child.
067     *
068     * @param basedir the basedir.
069     * @param file    the file to get the relative path for.
070     * @return the relative path to the child. (NOTE: this path will NOT start with a file separator character)
071     */
072    public static String getRelative( Path basedir, Path file )
073    {
074        if (basedir.isAbsolute() && !file.isAbsolute()) {
075            return basedir.normalize().relativize(file.toAbsolutePath()).toString();
076        } else if (!basedir.isAbsolute() && file.isAbsolute()) {
077            return basedir.toAbsolutePath().relativize(file.normalize()).toString();
078        } else {
079            return basedir.normalize().relativize(file.normalize()).toString();
080        }
081    }
082
083    public static String getRelative(String basedir, Path file) {
084        return getRelative(Paths.get(basedir), file);
085    }
086
087    /**
088     * Given a basedir and a child file, return the relative path to the child.
089     *
090     * @param basedir the basedir.
091     * @param child   the child path (can be a full path)
092     * @return the relative path to the child. (NOTE: this path will NOT start with a file separator character)
093     */
094    public static String getRelative( String basedir, String child )
095    {
096
097        return getRelative(basedir, Paths.get(child));
098    }
099
100    /**
101     * Returns a path object from the given URI. If the URI has no scheme, the path of the URI is used
102     * for creating the filesystem path.
103     *
104     * @param uri the uri to convert
105     * @return a path object with the given path
106     * @throws java.nio.file.FileSystemNotFoundException if the uri scheme is not known.
107     */
108    public static Path getPathFromUri( URI uri) {
109        if (uri==null) {
110            return Paths.get("");
111        } else if (uri.getScheme()==null) {
112            return Paths.get(uri.getPath());
113        } else {
114            return Paths.get(uri);
115        }
116    }
117}