This project has retired. For details please refer to its Attic page.
PathUtil xref
View Javadoc
1   package org.apache.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   *
31   */
32  public class PathUtil
33  {
34      public static String toUrl( String path )
35      {
36          // Is our work already done for us?
37          if ( path.startsWith( "file:/" ) )
38          {
39              return path;
40          }
41  
42          return toUrl( new File( path ) );
43      }
44  
45      public static String toUrl( File file )
46      {
47          try
48          {
49              return file.toURI().toURL().toExternalForm();
50          }
51          catch ( MalformedURLException e )
52          {
53              String pathCorrected = StringUtils.replaceChars( file.getAbsolutePath(), '\\', '/' );
54              if ( pathCorrected.startsWith( "file:/" ) )
55              {
56                  return pathCorrected;
57              }
58  
59              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          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          if ( basedir.endsWith( "/" ) || basedir.endsWith( "\\" ) )
85          {
86              basedir = basedir.substring( 0, basedir.length() - 1 );
87          }
88  
89          if ( child.startsWith( basedir ) )
90          {
91              // simple solution.
92              return child.substring( basedir.length() + 1 );
93          }
94  
95          String absoluteBasedir = new File( basedir ).getAbsolutePath();
96          if ( child.startsWith( absoluteBasedir ) )
97          {
98              // resolved basedir solution.
99              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 }