View Javadoc
1   package org.apache.maven.plugins.assembly.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.codehaus.plexus.components.io.fileselectors.FileInfo;
23  
24  import javax.annotation.Nonnull;
25  import java.io.File;
26  import java.util.Locale;
27  
28  /**
29   *
30   */
31  public final class AssemblyFileUtils
32  {
33  
34      private AssemblyFileUtils()
35      {
36          // no op
37      }
38  
39      public static String makePathRelativeTo( String path, final File basedir )
40      {
41          if ( basedir == null )
42          {
43              return path;
44          }
45  
46          if ( path == null )
47          {
48              return null;
49          }
50  
51          path = path.trim();
52  
53          String base = basedir.getAbsolutePath();
54          if ( path.startsWith( base ) )
55          {
56              path = path.substring( base.length() );
57              if ( path.length() > 0 )
58              {
59                  if ( path.startsWith( "/" ) || path.startsWith( "\\" ) )
60                  {
61                      path = path.substring( 1 );
62                  }
63              }
64  
65              if ( path.length() == 0 )
66              {
67                  path = ".";
68              }
69          }
70  
71          if ( !new File( path ).isAbsolute() )
72          {
73              path = path.replace( '\\', '/' );
74          }
75  
76          return path;
77      }
78  
79      @SuppressWarnings( "ResultOfMethodCallIgnored" )
80      public static void verifyTempDirectoryAvailability( @Nonnull final File tempDir )
81      {
82          if ( !tempDir.exists() )
83          {
84              tempDir.mkdirs();
85          }
86      }
87  
88      @Nonnull
89      private static String normalizePath( @Nonnull String path )
90      {
91          return path.replace( '\\', '/' );
92      }
93  
94      @Nonnull
95      public static String normalizeFileInfo( @Nonnull FileInfo fileInfo )
96      {
97          String name = fileInfo.getName();
98          name = normalizePath( name );
99          return name.replace( File.separatorChar, '/' ); // How can this be anything but a no-op
100     }
101 
102     public static boolean isPropertyFile( String sourceName )
103     {
104         return sourceName.toLowerCase( Locale.ENGLISH ).endsWith( ".properties" );
105     }
106 
107     public static boolean isPropertyFile( File file )
108     {
109         return isPropertyFile( file.getName() );
110     }
111 
112     /**
113      * This method exists because {@link File#isAbsolute()} is not OS independent.
114      *
115      * In addition to the check in {@link File#isAbsolute()} we will also test for a leading '/'.
116      *
117      * @return {@code true} if {@code File#isAbsolute()} or starts with a '/'
118      */
119     public static boolean isAbsolutePath( File dir )
120     {
121         return dir != null
122                 && ( dir.isAbsolute()
123                    || dir.getPath().startsWith( "\\" ) ); // on Win* platforms
124     }
125 }