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