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 | 10 | if ( basedir == null ) |
61 | |
{ |
62 | 10 | 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 | 33 | if (!tempDir.exists()) |
101 | |
{ |
102 | 2 | tempDir.mkdirs(); |
103 | |
} |
104 | 33 | } |
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 | 1 | UnArchiver unArchiver = archiverManager.getUnArchiver( source ); |
120 | |
|
121 | 1 | unArchiver.setSourceFile( source ); |
122 | |
|
123 | 1 | unArchiver.setDestDirectory( destDir ); |
124 | |
|
125 | 1 | unArchiver.extract(); |
126 | |
} |
127 | 0 | catch ( ArchiverException e ) |
128 | |
{ |
129 | 0 | throw new ArchiveExpansionException( "Error unpacking file: " + source + "to: " + destDir, e ); |
130 | 1 | } |
131 | 1 | } |
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 | 12 | BufferedWriter out = null; |
143 | 12 | BufferedReader bufferedSource = null; |
144 | |
try |
145 | |
{ |
146 | 12 | if ( source instanceof BufferedReader ) |
147 | |
{ |
148 | 0 | bufferedSource = (BufferedReader) source; |
149 | |
} |
150 | |
else |
151 | |
{ |
152 | 12 | bufferedSource = new BufferedReader( source ); |
153 | |
} |
154 | |
|
155 | 12 | if ( encoding == null ) |
156 | |
{ |
157 | 4 | out = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( dest ) ) ); |
158 | |
} |
159 | |
else |
160 | |
{ |
161 | 8 | out = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( dest ), encoding ) ); |
162 | |
} |
163 | |
|
164 | |
String line; |
165 | |
|
166 | 12 | line = bufferedSource.readLine(); |
167 | 36 | while ( line != null ) |
168 | |
{ |
169 | 24 | out.write( line ); |
170 | 24 | line = bufferedSource.readLine(); |
171 | 24 | if ( line != null ) |
172 | |
{ |
173 | 12 | out.write( lineEndings ); |
174 | |
} |
175 | |
} |
176 | |
|
177 | 12 | out.flush(); |
178 | |
} |
179 | |
finally |
180 | |
{ |
181 | 12 | IOUtil.close( out ); |
182 | 12 | } |
183 | 12 | } |
184 | |
|
185 | |
public static String getLineEndingCharacters( String lineEnding ) |
186 | |
throws AssemblyFormattingException |
187 | |
{ |
188 | 48 | String value = lineEnding; |
189 | 48 | if ( lineEnding != null ) |
190 | |
{ |
191 | 31 | if ( LINE_ENDING_KEEP.equals( lineEnding ) ) |
192 | |
{ |
193 | 13 | value = null; |
194 | |
} |
195 | 18 | else if ( LINE_ENDING_DOS.equals( lineEnding ) || LINE_ENDING_WINDOWS.equals( lineEnding ) || LINE_ENDING_CRLF.equals( lineEnding ) ) |
196 | |
{ |
197 | 13 | value = "\r\n"; |
198 | |
} |
199 | 5 | else if ( LINE_ENDING_UNIX.equals( lineEnding ) || LINE_ENDING_LF.equals( lineEnding ) ) |
200 | |
{ |
201 | 4 | value = "\n"; |
202 | |
} |
203 | |
else |
204 | |
{ |
205 | 1 | throw new AssemblyFormattingException( "Illegal lineEnding specified: '" + lineEnding + "'" ); |
206 | |
} |
207 | |
} |
208 | |
|
209 | 47 | return value; |
210 | |
} |
211 | |
|
212 | |
public static void copyFile( File src, File dst ) throws IOException |
213 | |
{ |
214 | 7 | FileChannel c1 = new RandomAccessFile( src, "r" ).getChannel(); |
215 | 7 | FileChannel c2 = new RandomAccessFile( dst, "rw" ).getChannel(); |
216 | |
|
217 | 7 | long tCount = 0, size = c1.size(); |
218 | 7 | while ( ( tCount += c2.transferFrom( c1, 0, size - tCount ) ) < size ) |
219 | 0 | ; |
220 | |
|
221 | 7 | c1.close(); |
222 | 7 | c2.force( true ); |
223 | 7 | c2.close(); |
224 | 7 | } |
225 | |
|
226 | |
public static String normalizePath( String path ) |
227 | |
{ |
228 | 0 | return path.replace( '\\', '/' ); |
229 | |
} |
230 | |
|
231 | |
} |