Coverage Report - org.apache.maven.archiva.common.utils.PathUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
PathUtil
0%
0/19
0%
0/12
4
 
 1  
 package org.apache.maven.archiva.common.utils;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *  http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import org.apache.commons.lang.StringUtils;
 23  
 
 24  
 import java.io.File;
 25  
 import java.net.MalformedURLException;
 26  
 
 27  
 /**
 28  
  * PathUtil - simple utility methods for path manipulation. 
 29  
  *
 30  
  * @version $Id: PathUtil.java 718864 2008-11-19 06:33:35Z brett $
 31  
  */
 32  0
 public class PathUtil
 33  
 {
 34  
     public static String toUrl( String path )
 35  
     {
 36  
         // Is our work already done for us?
 37  0
         if ( path.startsWith( "file:/" ) )
 38  
         {
 39  0
             return path;
 40  
         }
 41  
 
 42  0
         return toUrl( new File( path ) );
 43  
     }
 44  
 
 45  
     public static String toUrl( File file )
 46  
     {
 47  
         try
 48  
         {
 49  0
             return file.toURL().toExternalForm();
 50  
         }
 51  0
         catch ( MalformedURLException e )
 52  
         {
 53  0
             String pathCorrected = StringUtils.replaceChars( file.getAbsolutePath(), '\\', '/' );
 54  0
             if ( pathCorrected.startsWith( "file:/" ) )
 55  
             {
 56  0
                 return pathCorrected;
 57  
             }
 58  
 
 59  0
             return "file://" + pathCorrected;
 60  
         }
 61  
     }
 62  
 
 63  
     /**
 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  
      */
 70  
     public static String getRelative( String basedir, File file )
 71  
     {
 72  0
         return getRelative( basedir, file.getAbsolutePath() );
 73  
     }
 74  
 
 75  
     /**
 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  
      */
 82  
     public static String getRelative( String basedir, String child )
 83  
     {
 84  0
         if ( basedir.endsWith( "/" ) || basedir.endsWith( "\\" ) )
 85  
         {
 86  0
             basedir = basedir.substring( 0, basedir.length() - 1 );
 87  
         }
 88  
 
 89  0
         if ( child.startsWith( basedir ) )
 90  
         {
 91  
             // simple solution.
 92  0
             return child.substring( basedir.length() + 1 );
 93  
         }
 94  
 
 95  0
         String absoluteBasedir = new File( basedir ).getAbsolutePath();
 96  0
         if ( child.startsWith( absoluteBasedir ) )
 97  
         {
 98  
             // resolved basedir solution.
 99  0
             return child.substring( absoluteBasedir.length() + 1 );
 100  
         }
 101  
 
 102  
         // File is not within basedir.
 103  0
         throw new IllegalStateException( "Unable to obtain relative path of file " + child
 104  
             + ", it is not within basedir " + basedir + "." );
 105  
     }
 106  
 }