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.lang3.StringUtils;
23  
24  import java.net.MalformedURLException;
25  import java.net.URI;
26  import java.nio.file.Path;
27  import java.nio.file.Paths;
28  
29  /**
30   * PathUtil - simple utility methods for path manipulation.
31   *
32   *
33   */
34  public class PathUtil
35  {
36      public static String toUrl( String path )
37      {
38          // Is our work already done for us?
39          if ( path.startsWith( "file:/" ) )
40          {
41              return path;
42          }
43  
44          return toUrl( Paths.get( path ) );
45      }
46  
47      public static String toUrl( Path file )
48      {
49          try
50          {
51              return file.toUri().toURL().toExternalForm();
52          }
53          catch ( MalformedURLException e )
54          {
55              String pathCorrected = StringUtils.replaceChars( file.toAbsolutePath().toString(), '\\', '/' );
56              if ( pathCorrected.startsWith( "file:/" ) )
57              {
58                  return pathCorrected;
59              }
60  
61              return "file://" + pathCorrected;
62          }
63      }
64  
65      /**
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       */
72      public static String getRelative( Path basedir, Path file )
73      {
74          if (basedir.isAbsolute() && !file.isAbsolute()) {
75              return basedir.normalize().relativize(file.toAbsolutePath()).toString();
76          } else if (!basedir.isAbsolute() && file.isAbsolute()) {
77              return basedir.toAbsolutePath().relativize(file.normalize()).toString();
78          } else {
79              return basedir.normalize().relativize(file.normalize()).toString();
80          }
81      }
82  
83      public static String getRelative(String basedir, Path file) {
84          return getRelative(Paths.get(basedir), file);
85      }
86  
87      /**
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       */
94      public static String getRelative( String basedir, String child )
95      {
96  
97          return getRelative(basedir, Paths.get(child));
98      }
99  
100     /**
101      * Returns a path object from the given URI. If the URI has no scheme, the path of the URI is used
102      * for creating the filesystem path.
103      *
104      * @param uri the uri to convert
105      * @return a path object with the given path
106      * @throws java.nio.file.FileSystemNotFoundException if the uri scheme is not known.
107      */
108     public static Path getPathFromUri( URI uri) {
109         if (uri==null) {
110             return Paths.get("");
111         } else if (uri.getScheme()==null) {
112             return Paths.get(uri.getPath());
113         } else {
114             return Paths.get(uri);
115         }
116     }
117 }