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