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