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