View Javadoc

1   package org.apache.maven.plugin.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 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   * @version $Id: AssemblyFileUtils.java 1403897 2012-10-30 22:11:04Z dennisl $
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      {
56      }
57      
58      public static String makePathRelativeTo( String path, final File basedir )
59      {
60          if ( basedir == null )
61          {
62              return path;
63          }
64  
65          if ( path == null )
66          {
67              return null;
68          }
69  
70          path = path.trim();
71  
72          String base = basedir.getAbsolutePath();
73          if ( path.startsWith( base ) )
74          {
75              path = path.substring( base.length() );
76              if ( path.length() > 0 )
77              {
78                  if ( path.startsWith( "/" ) || path.startsWith( "\\" ) )
79                  {
80                      path = path.substring( 1 );
81                  }
82              }
83              
84              if ( path.length() == 0 )
85              {
86                  path = ".";
87              }
88          }
89  
90          if ( !new File( path ).isAbsolute() )
91          {
92              path = path.replace( '\\', '/' );
93          }
94  
95          return path;
96      }
97  
98      public static void verifyTempDirectoryAvailability( final File tempDir, final Logger logger )
99      {
100         if (!tempDir.exists())
101         {
102             tempDir.mkdirs();
103         }
104     }
105 
106     /**
107      * Unpacks the archive file.
108      *
109      * @param source
110      *            File to be unpacked.
111      * @param destDir
112      *            Location where to put the unpacked files.
113      */
114     public static void unpack( File source, File destDir, ArchiverManager archiverManager )
115         throws ArchiveExpansionException, NoSuchArchiverException
116     {
117         try
118         {
119             UnArchiver unArchiver = archiverManager.getUnArchiver( source );
120 
121             unArchiver.setSourceFile( source );
122 
123             unArchiver.setDestDirectory( destDir );
124 
125             unArchiver.extract();
126         }
127         catch ( ArchiverException e )
128         {
129             throw new ArchiveExpansionException( "Error unpacking file: " + source + "to: " + destDir, e );
130         }
131     }
132 
133     /**
134      * NOTE: It is the responsibility of the caller to close the source Reader instance.
135      * The file content is written using platform encoding.
136      * @param lineEndings This is the result of the getLineEndingChars(..) method in this utility class; the actual
137      *   line-ending characters.
138      */
139     public static void convertLineEndings( Reader source, File dest, String lineEndings, String encoding )
140         throws IOException
141     {
142         BufferedWriter out = null;
143         BufferedReader bufferedSource = null;
144         try
145         {
146             if ( source instanceof BufferedReader )
147             {
148                 bufferedSource = (BufferedReader) source;
149             }
150             else
151             {
152                 bufferedSource = new BufferedReader( source );
153             }
154 
155             if ( encoding == null )
156             {
157                 out = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( dest ) ) ); // platform encoding
158             }
159             else
160             {
161                 out = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( dest ), encoding ) );
162             }
163 
164             String line;
165 
166             line = bufferedSource.readLine();
167             while ( line != null )
168             {
169                 out.write( line );
170                 line = bufferedSource.readLine();
171                 if ( line != null )
172                 {
173                     out.write( lineEndings );
174                 }
175             }
176 
177             out.flush();
178         }
179         finally
180         {
181             IOUtil.close( out );
182         }
183     }
184 
185     public static String getLineEndingCharacters( String lineEnding )
186         throws AssemblyFormattingException
187     {
188         String value = lineEnding;
189         if ( lineEnding != null )
190         {
191             if ( LINE_ENDING_KEEP.equals( lineEnding ) )
192             {
193                 value = null;
194             }
195             else if ( LINE_ENDING_DOS.equals( lineEnding ) || LINE_ENDING_WINDOWS.equals( lineEnding ) || LINE_ENDING_CRLF.equals( lineEnding ) )
196             {
197                 value = "\r\n";
198             }
199             else if ( LINE_ENDING_UNIX.equals( lineEnding ) || LINE_ENDING_LF.equals( lineEnding ) )
200             {
201                 value = "\n";
202             }
203             else
204             {
205                 throw new AssemblyFormattingException( "Illegal lineEnding specified: '" + lineEnding + "'" );
206             }
207         }
208 
209         return value;
210     }
211 
212     public static void copyFile( File src, File dst ) throws IOException
213     {
214         FileChannel c1 = new RandomAccessFile( src, "r" ).getChannel();
215         FileChannel c2 = new RandomAccessFile( dst, "rw" ).getChannel();
216 
217         long tCount = 0, size = c1.size();
218         while ( ( tCount += c2.transferFrom( c1, 0, size - tCount ) ) < size )
219             ;
220 
221         c1.close();
222         c2.force( true );
223         c2.close();
224     }
225 
226     public static String normalizePath( String path )
227     {
228         return path.replace( '\\', '/' );
229     }
230 
231 }