1package org.apache.archiva.common.utils;
23/*4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */2122import org.apache.commons.lang3.StringUtils;
2324import java.net.MalformedURLException;
25import java.net.URI;
26import java.nio.file.Path;
27import java.nio.file.Paths;
2829/**30 * PathUtil - simple utility methods for path manipulation.31 *32 *33 */34publicclassPathUtil35 {
36publicstatic String toUrl( String path )
37 {
38// Is our work already done for us?39if ( path.startsWith( "file:/" ) )
40 {
41return path;
42 }
4344return toUrl( Paths.get( path ) );
45 }
4647publicstatic String toUrl( Path file )
48 {
49try50 {
51return file.toUri().toURL().toExternalForm();
52 }
53catch ( MalformedURLException e )
54 {
55 String pathCorrected = StringUtils.replaceChars( file.toAbsolutePath().toString(), '\\', '/' );
56if ( pathCorrected.startsWith( "file:/" ) )
57 {
58return pathCorrected;
59 }
6061return"file://" + pathCorrected;
62 }
63 }
6465/**66 * Given a basedir and a child file, return the relative path to the child.67 *68 * @param basedir the basedir.69 * @param file the file to get the relative path for.70 * @return the relative path to the child. (NOTE: this path will NOT start with a file separator character)71 */72publicstatic String getRelative( Path basedir, Path file )
73 {
74if (basedir.isAbsolute() && !file.isAbsolute()) {
75return basedir.normalize().relativize(file.toAbsolutePath()).toString();
76 } elseif (!basedir.isAbsolute() && file.isAbsolute()) {
77return basedir.toAbsolutePath().relativize(file.normalize()).toString();
78 } else {
79return basedir.normalize().relativize(file.normalize()).toString();
80 }
81 }
8283publicstatic String getRelative(String basedir, Path file) {
84return getRelative(Paths.get(basedir), file);
85 }
8687/**88 * Given a basedir and a child file, return the relative path to the child.89 *90 * @param basedir the basedir.91 * @param child the child path (can be a full path)92 * @return the relative path to the child. (NOTE: this path will NOT start with a file separator character)93 */94publicstatic String getRelative( String basedir, String child )
95 {
9697return getRelative(basedir, Paths.get(child));
98 }
99100/**101 * Returns a path object from the given URI. If the URI has no scheme, the path of the URI is used102 * for creating the filesystem path.103 *104 * @param uri the uri to convert105 * @return a path object with the given path106 * @throws java.nio.file.FileSystemNotFoundException if the uri scheme is not known.107 */108publicstatic Path getPathFromUri( URI uri) {
109if (uri==null) {
110return Paths.get("");
111 } elseif (uri.getScheme()==null) {
112return Paths.get(uri.getPath());
113 } else {
114return Paths.get(uri);
115 }
116 }
117 }