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.lang.StringUtils; 023 024import java.io.File; 025import java.net.MalformedURLException; 026 027/** 028 * PathUtil - simple utility methods for path manipulation. 029 * 030 * 031 */ 032public class PathUtil 033{ 034 public static String toUrl( String path ) 035 { 036 // Is our work already done for us? 037 if ( path.startsWith( "file:/" ) ) 038 { 039 return path; 040 } 041 042 return toUrl( new File( path ) ); 043 } 044 045 public static String toUrl( File file ) 046 { 047 try 048 { 049 return file.toURI().toURL().toExternalForm(); 050 } 051 catch ( MalformedURLException e ) 052 { 053 String pathCorrected = StringUtils.replaceChars( file.getAbsolutePath(), '\\', '/' ); 054 if ( pathCorrected.startsWith( "file:/" ) ) 055 { 056 return pathCorrected; 057 } 058 059 return "file://" + pathCorrected; 060 } 061 } 062 063 /** 064 * Given a basedir and a child file, return the relative path to the child. 065 * 066 * @param basedir the basedir. 067 * @param file the file to get the relative path for. 068 * @return the relative path to the child. (NOTE: this path will NOT start with a {@link File#separator} character) 069 */ 070 public static String getRelative( String basedir, File file ) 071 { 072 return getRelative( basedir, file.getAbsolutePath() ); 073 } 074 075 /** 076 * Given a basedir and a child file, return the relative path to the child. 077 * 078 * @param basedir the basedir. 079 * @param child the child path (can be a full path) 080 * @return the relative path to the child. (NOTE: this path will NOT start with a {@link File#separator} character) 081 */ 082 public static String getRelative( String basedir, String child ) 083 { 084 if ( basedir.endsWith( "/" ) || basedir.endsWith( "\\" ) ) 085 { 086 basedir = basedir.substring( 0, basedir.length() - 1 ); 087 } 088 089 if ( child.startsWith( basedir ) ) 090 { 091 // simple solution. 092 return child.substring( basedir.length() + 1 ); 093 } 094 095 String absoluteBasedir = new File( basedir ).getAbsolutePath(); 096 if ( child.startsWith( absoluteBasedir ) ) 097 { 098 // resolved basedir solution. 099 return child.substring( absoluteBasedir.length() + 1 ); 100 } 101 102 // File is not within basedir. 103 throw new IllegalStateException( 104 "Unable to obtain relative path of file " + child + ", it is not within basedir " + basedir + "." ); 105 } 106}