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.lang.StringUtils;
2324import java.io.File;
25import java.net.MalformedURLException;
2627/**28 * PathUtil - simple utility methods for path manipulation.29 *30 *31 */32publicclassPathUtil33 {
34publicstatic String toUrl( String path )
35 {
36// Is our work already done for us?37if ( path.startsWith( "file:/" ) )
38 {
39return path;
40 }
4142return toUrl( new File( path ) );
43 }
4445publicstatic String toUrl( File file )
46 {
47try48 {
49return file.toURI().toURL().toExternalForm();
50 }
51catch ( MalformedURLException e )
52 {
53 String pathCorrected = StringUtils.replaceChars( file.getAbsolutePath(), '\\', '/' );
54if ( pathCorrected.startsWith( "file:/" ) )
55 {
56return pathCorrected;
57 }
5859return"file://" + pathCorrected;
60 }
61 }
6263/**64 * Given a basedir and a child file, return the relative path to the child.65 *66 * @param basedir the basedir.67 * @param file the file to get the relative path for.68 * @return the relative path to the child. (NOTE: this path will NOT start with a {@link File#separator} character)69 */70publicstatic String getRelative( String basedir, File file )
71 {
72return getRelative( basedir, file.getAbsolutePath() );
73 }
7475/**76 * Given a basedir and a child file, return the relative path to the child.77 *78 * @param basedir the basedir.79 * @param child the child path (can be a full path)80 * @return the relative path to the child. (NOTE: this path will NOT start with a {@link File#separator} character)81 */82publicstatic String getRelative( String basedir, String child )
83 {
84if ( basedir.endsWith( "/" ) || basedir.endsWith( "\\" ) )
85 {
86 basedir = basedir.substring( 0, basedir.length() - 1 );
87 }
8889if ( child.startsWith( basedir ) )
90 {
91// simple solution.92return child.substring( basedir.length() + 1 );
93 }
9495 String absoluteBasedir = new File( basedir ).getAbsolutePath();
96if ( child.startsWith( absoluteBasedir ) )
97 {
98// resolved basedir solution.99return child.substring( absoluteBasedir.length() + 1 );
100 }
101102// File is not within basedir.103thrownew IllegalStateException(
104"Unable to obtain relative path of file " + child + ", it is not within basedir " + basedir + "." );
105 }
106 }