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 {
54 }
55
56 public static String makePathRelativeTo( String path, final File basedir )
57 {
58 if ( basedir == null )
59 {
60 return path;
61 }
62
63 if ( path == null )
64 {
65 return null;
66 }
67
68 path = path.trim();
69
70 String base = basedir.getAbsolutePath();
71 if ( path.startsWith( base ) )
72 {
73 path = path.substring( base.length() );
74 if ( path.length() > 0 )
75 {
76 if ( path.startsWith( "/" ) || path.startsWith( "\\" ) )
77 {
78 path = path.substring( 1 );
79 }
80 }
81
82 if ( path.length() == 0 )
83 {
84 path = ".";
85 }
86 }
87
88 if ( !new File( path ).isAbsolute() )
89 {
90 path = path.replace( '\\', '/' );
91 }
92
93 return path;
94 }
95
96 public static void verifyTempDirectoryAvailability( final File tempDir, final Logger logger )
97 {
98 if (!tempDir.exists())
99 {
100 tempDir.mkdirs();
101 }
102 }
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 UnArchiver unArchiver = archiverManager.getUnArchiver( source );
118
119 unArchiver.setSourceFile( source );
120
121 unArchiver.setDestDirectory( destDir );
122
123 unArchiver.extract();
124 }
125 catch ( ArchiverException e )
126 {
127 throw new ArchiveExpansionException( "Error unpacking file: " + source + "to: " + destDir, e );
128 }
129 }
130
131
132
133
134
135
136
137 public static void convertLineEndings( Reader source, File dest, String lineEndings )
138 throws IOException
139 {
140 BufferedWriter out = null;
141 BufferedReader bufferedSource = null;
142 try
143 {
144 if ( source instanceof BufferedReader )
145 {
146 bufferedSource = (BufferedReader) source;
147 }
148 else
149 {
150 bufferedSource = new BufferedReader( source );
151 }
152
153 out = new BufferedWriter( new FileWriter( dest ) );
154
155 String line;
156
157 do
158 {
159 line = bufferedSource.readLine();
160 if ( line != null )
161 {
162 out.write( line );
163 out.write( lineEndings );
164 }
165 } while ( line != null );
166
167 out.flush();
168 }
169 finally
170 {
171 IOUtil.close( out );
172 }
173 }
174
175 public static String getLineEndingCharacters( String lineEnding )
176 throws AssemblyFormattingException
177 {
178 String value = lineEnding;
179 if ( lineEnding != null )
180 {
181 if ( LINE_ENDING_KEEP.equals( lineEnding ) )
182 {
183 value = null;
184 }
185 else if ( LINE_ENDING_DOS.equals( lineEnding ) || LINE_ENDING_CRLF.equals( lineEnding ) )
186 {
187 value = "\r\n";
188 }
189 else if ( LINE_ENDING_UNIX.equals( lineEnding ) || LINE_ENDING_LF.equals( lineEnding ) )
190 {
191 value = "\n";
192 }
193 else
194 {
195 throw new AssemblyFormattingException( "Illlegal lineEnding specified: '" + lineEnding + "'" );
196 }
197 }
198
199 return value;
200 }
201
202 public static void copyFile( File src, File dst ) throws IOException
203 {
204 FileChannel c1 = new RandomAccessFile( src, "r" ).getChannel();
205 FileChannel c2 = new RandomAccessFile( dst, "rw" ).getChannel();
206
207 long tCount = 0, size = c1.size();
208 while ( ( tCount += c2.transferFrom( c1, 0, size - tCount ) ) < size )
209 ;
210
211 c1.close();
212 c2.force( true );
213 c2.close();
214 }
215
216 public static String normalizePath( String path )
217 {
218 return path.replace( '\\', '/' );
219 }
220
221 }