1 | |
package org.apache.maven.plugin.assembly.utils; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.BufferedReader; |
23 | |
import java.io.BufferedWriter; |
24 | |
import java.io.File; |
25 | |
import java.io.FileWriter; |
26 | |
import java.io.IOException; |
27 | |
import java.io.RandomAccessFile; |
28 | |
import java.io.Reader; |
29 | |
import java.nio.channels.FileChannel; |
30 | |
|
31 | |
import org.apache.maven.plugin.assembly.archive.ArchiveExpansionException; |
32 | |
import org.apache.maven.plugin.assembly.format.AssemblyFormattingException; |
33 | |
import org.codehaus.plexus.archiver.ArchiverException; |
34 | |
import org.codehaus.plexus.archiver.UnArchiver; |
35 | |
import org.codehaus.plexus.archiver.manager.ArchiverManager; |
36 | |
import org.codehaus.plexus.archiver.manager.NoSuchArchiverException; |
37 | |
import org.codehaus.plexus.logging.Logger; |
38 | |
import org.codehaus.plexus.util.IOUtil; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public final class AssemblyFileUtils |
44 | |
{ |
45 | |
|
46 | |
public static final String LINE_ENDING_KEEP = "keep"; |
47 | |
public static final String LINE_ENDING_DOS = "dos"; |
48 | |
public static final String LINE_ENDING_UNIX = "unix"; |
49 | |
public static final String LINE_ENDING_CRLF = "crlf"; |
50 | |
public static final String LINE_ENDING_LF = "lf"; |
51 | |
|
52 | |
private AssemblyFileUtils() |
53 | 0 | { |
54 | 0 | } |
55 | |
|
56 | |
public static String makePathRelativeTo( String path, final File basedir ) |
57 | |
{ |
58 | 10 | if ( basedir == null ) |
59 | |
{ |
60 | 10 | return path; |
61 | |
} |
62 | |
|
63 | 0 | if ( path == null ) |
64 | |
{ |
65 | 0 | return null; |
66 | |
} |
67 | |
|
68 | 0 | path = path.trim(); |
69 | |
|
70 | 0 | String base = basedir.getAbsolutePath(); |
71 | 0 | if ( path.startsWith( base ) ) |
72 | |
{ |
73 | 0 | path = path.substring( base.length() ); |
74 | 0 | if ( path.length() > 0 ) |
75 | |
{ |
76 | 0 | if ( path.startsWith( "/" ) || path.startsWith( "\\" ) ) |
77 | |
{ |
78 | 0 | path = path.substring( 1 ); |
79 | |
} |
80 | |
} |
81 | |
|
82 | 0 | if ( path.length() == 0 ) |
83 | |
{ |
84 | 0 | path = "."; |
85 | |
} |
86 | |
} |
87 | |
|
88 | 0 | if ( !new File( path ).isAbsolute() ) |
89 | |
{ |
90 | 0 | path = path.replace( '\\', '/' ); |
91 | |
} |
92 | |
|
93 | 0 | return path; |
94 | |
} |
95 | |
|
96 | |
public static void verifyTempDirectoryAvailability( final File tempDir, final Logger logger ) |
97 | |
{ |
98 | 33 | if (!tempDir.exists()) |
99 | |
{ |
100 | 2 | tempDir.mkdirs(); |
101 | |
} |
102 | 33 | } |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
public static void unpack( File source, File destDir, ArchiverManager archiverManager ) |
113 | |
throws ArchiveExpansionException, NoSuchArchiverException |
114 | |
{ |
115 | |
try |
116 | |
{ |
117 | 1 | UnArchiver unArchiver = archiverManager.getUnArchiver( source ); |
118 | |
|
119 | 1 | unArchiver.setSourceFile( source ); |
120 | |
|
121 | 1 | unArchiver.setDestDirectory( destDir ); |
122 | |
|
123 | 1 | unArchiver.extract(); |
124 | |
} |
125 | 0 | catch ( ArchiverException e ) |
126 | |
{ |
127 | 0 | throw new ArchiveExpansionException( "Error unpacking file: " + source + "to: " + destDir, e ); |
128 | 1 | } |
129 | 1 | } |
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
public static void convertLineEndings( Reader source, File dest, String lineEndings ) |
138 | |
throws IOException |
139 | |
{ |
140 | 12 | BufferedWriter out = null; |
141 | 12 | BufferedReader bufferedSource = null; |
142 | |
try |
143 | |
{ |
144 | 12 | if ( source instanceof BufferedReader ) |
145 | |
{ |
146 | 0 | bufferedSource = (BufferedReader) source; |
147 | |
} |
148 | |
else |
149 | |
{ |
150 | 12 | bufferedSource = new BufferedReader( source ); |
151 | |
} |
152 | |
|
153 | 12 | out = new BufferedWriter( new FileWriter( dest ) ); |
154 | |
|
155 | |
String line; |
156 | |
|
157 | |
do |
158 | |
{ |
159 | 36 | line = bufferedSource.readLine(); |
160 | 36 | if ( line != null ) |
161 | |
{ |
162 | 24 | out.write( line ); |
163 | 24 | out.write( lineEndings ); |
164 | |
} |
165 | 36 | } while ( line != null ); |
166 | |
|
167 | 12 | out.flush(); |
168 | |
} |
169 | |
finally |
170 | |
{ |
171 | 12 | IOUtil.close( out ); |
172 | 12 | } |
173 | 12 | } |
174 | |
|
175 | |
public static String getLineEndingCharacters( String lineEnding ) |
176 | |
throws AssemblyFormattingException |
177 | |
{ |
178 | 48 | String value = lineEnding; |
179 | 48 | if ( lineEnding != null ) |
180 | |
{ |
181 | 31 | if ( LINE_ENDING_KEEP.equals( lineEnding ) ) |
182 | |
{ |
183 | 13 | value = null; |
184 | |
} |
185 | 18 | else if ( LINE_ENDING_DOS.equals( lineEnding ) || LINE_ENDING_CRLF.equals( lineEnding ) ) |
186 | |
{ |
187 | 13 | value = "\r\n"; |
188 | |
} |
189 | 5 | else if ( LINE_ENDING_UNIX.equals( lineEnding ) || LINE_ENDING_LF.equals( lineEnding ) ) |
190 | |
{ |
191 | 4 | value = "\n"; |
192 | |
} |
193 | |
else |
194 | |
{ |
195 | 1 | throw new AssemblyFormattingException( "Illlegal lineEnding specified: '" + lineEnding + "'" ); |
196 | |
} |
197 | |
} |
198 | |
|
199 | 47 | return value; |
200 | |
} |
201 | |
|
202 | |
public static void copyFile( File src, File dst ) throws IOException |
203 | |
{ |
204 | 7 | FileChannel c1 = new RandomAccessFile( src, "r" ).getChannel(); |
205 | 7 | FileChannel c2 = new RandomAccessFile( dst, "rw" ).getChannel(); |
206 | |
|
207 | 7 | long tCount = 0, size = c1.size(); |
208 | 7 | while ( ( tCount += c2.transferFrom( c1, 0, size - tCount ) ) < size ) |
209 | 0 | ; |
210 | |
|
211 | 7 | c1.close(); |
212 | 7 | c2.force( true ); |
213 | 7 | c2.close(); |
214 | 7 | } |
215 | |
|
216 | |
public static String normalizePath( String path ) |
217 | |
{ |
218 | 0 | return path.replace( '\\', '/' ); |
219 | |
} |
220 | |
|
221 | |
} |