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.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   * @version $Id: AssemblyFileUtils.java 782119 2009-06-05 19:55:15Z jdcasey $
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      * Unpacks the archive file.
106      *
107      * @param source
108      *            File to be unpacked.
109      * @param destDir
110      *            Location where to put the unpacked files.
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      * NOTE: It is the responsibility of the caller to close the source Reader instance.
133      * The file content is written using platform encoding.
134      * @param lineEndings This is the result of the getLineEndingChars(..) method in this utility class; the actual
135      *   line-ending characters.
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 ) ); // platform encoding
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 }