/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/utils/AssemblyFileUtils.java

https://github.com/tobrien/maven-plugins · Java · 222 lines · 157 code · 30 blank · 35 comment · 28 complexity · 7629af8499dafd876a7f93fd9f7086da MD5 · raw file

  1. package org.apache.maven.plugin.assembly.utils;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import java.io.BufferedReader;
  21. import java.io.BufferedWriter;
  22. import java.io.File;
  23. import java.io.FileWriter;
  24. import java.io.IOException;
  25. import java.io.RandomAccessFile;
  26. import java.io.Reader;
  27. import java.nio.channels.FileChannel;
  28. import org.apache.maven.plugin.assembly.archive.ArchiveExpansionException;
  29. import org.apache.maven.plugin.assembly.format.AssemblyFormattingException;
  30. import org.codehaus.plexus.archiver.ArchiverException;
  31. import org.codehaus.plexus.archiver.UnArchiver;
  32. import org.codehaus.plexus.archiver.manager.ArchiverManager;
  33. import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
  34. import org.codehaus.plexus.logging.Logger;
  35. import org.codehaus.plexus.util.IOUtil;
  36. /**
  37. * @version $Id$
  38. */
  39. public final class AssemblyFileUtils
  40. {
  41. public static final String LINE_ENDING_KEEP = "keep";
  42. public static final String LINE_ENDING_DOS = "dos";
  43. public static final String LINE_ENDING_WINDOWS = "windows";
  44. public static final String LINE_ENDING_UNIX = "unix";
  45. public static final String LINE_ENDING_CRLF = "crlf";
  46. public static final String LINE_ENDING_LF = "lf";
  47. private AssemblyFileUtils()
  48. {
  49. }
  50. public static String makePathRelativeTo( String path, final File basedir )
  51. {
  52. if ( basedir == null )
  53. {
  54. return path;
  55. }
  56. if ( path == null )
  57. {
  58. return null;
  59. }
  60. path = path.trim();
  61. String base = basedir.getAbsolutePath();
  62. if ( path.startsWith( base ) )
  63. {
  64. path = path.substring( base.length() );
  65. if ( path.length() > 0 )
  66. {
  67. if ( path.startsWith( "/" ) || path.startsWith( "\\" ) )
  68. {
  69. path = path.substring( 1 );
  70. }
  71. }
  72. if ( path.length() == 0 )
  73. {
  74. path = ".";
  75. }
  76. }
  77. if ( !new File( path ).isAbsolute() )
  78. {
  79. path = path.replace( '\\', '/' );
  80. }
  81. return path;
  82. }
  83. public static void verifyTempDirectoryAvailability( final File tempDir, final Logger logger )
  84. {
  85. if (!tempDir.exists())
  86. {
  87. tempDir.mkdirs();
  88. }
  89. }
  90. /**
  91. * Unpacks the archive file.
  92. *
  93. * @param source
  94. * File to be unpacked.
  95. * @param destDir
  96. * Location where to put the unpacked files.
  97. */
  98. public static void unpack( File source, File destDir, ArchiverManager archiverManager )
  99. throws ArchiveExpansionException, NoSuchArchiverException
  100. {
  101. try
  102. {
  103. UnArchiver unArchiver = archiverManager.getUnArchiver( source );
  104. unArchiver.setSourceFile( source );
  105. unArchiver.setDestDirectory( destDir );
  106. unArchiver.extract();
  107. }
  108. catch ( ArchiverException e )
  109. {
  110. throw new ArchiveExpansionException( "Error unpacking file: " + source + "to: " + destDir, e );
  111. }
  112. }
  113. /**
  114. * NOTE: It is the responsibility of the caller to close the source Reader instance.
  115. * The file content is written using platform encoding.
  116. * @param lineEndings This is the result of the getLineEndingChars(..) method in this utility class; the actual
  117. * line-ending characters.
  118. */
  119. public static void convertLineEndings( Reader source, File dest, String lineEndings )
  120. throws IOException
  121. {
  122. BufferedWriter out = null;
  123. BufferedReader bufferedSource = null;
  124. try
  125. {
  126. if ( source instanceof BufferedReader )
  127. {
  128. bufferedSource = (BufferedReader) source;
  129. }
  130. else
  131. {
  132. bufferedSource = new BufferedReader( source );
  133. }
  134. out = new BufferedWriter( new FileWriter( dest ) ); // platform encoding
  135. String line;
  136. do
  137. {
  138. line = bufferedSource.readLine();
  139. if ( line != null )
  140. {
  141. out.write( line );
  142. out.write( lineEndings );
  143. }
  144. } while ( line != null );
  145. out.flush();
  146. }
  147. finally
  148. {
  149. IOUtil.close( out );
  150. }
  151. }
  152. public static String getLineEndingCharacters( String lineEnding )
  153. throws AssemblyFormattingException
  154. {
  155. String value = lineEnding;
  156. if ( lineEnding != null )
  157. {
  158. if ( LINE_ENDING_KEEP.equals( lineEnding ) )
  159. {
  160. value = null;
  161. }
  162. else if ( LINE_ENDING_DOS.equals( lineEnding ) || LINE_ENDING_WINDOWS.equals( lineEnding ) || LINE_ENDING_CRLF.equals( lineEnding ) )
  163. {
  164. value = "\r\n";
  165. }
  166. else if ( LINE_ENDING_UNIX.equals( lineEnding ) || LINE_ENDING_LF.equals( lineEnding ) )
  167. {
  168. value = "\n";
  169. }
  170. else
  171. {
  172. throw new AssemblyFormattingException( "Illegal lineEnding specified: '" + lineEnding + "'" );
  173. }
  174. }
  175. return value;
  176. }
  177. public static void copyFile( File src, File dst ) throws IOException
  178. {
  179. FileChannel c1 = new RandomAccessFile( src, "r" ).getChannel();
  180. FileChannel c2 = new RandomAccessFile( dst, "rw" ).getChannel();
  181. long tCount = 0, size = c1.size();
  182. while ( ( tCount += c2.transferFrom( c1, 0, size - tCount ) ) < size )
  183. ;
  184. c1.close();
  185. c2.force( true );
  186. c2.close();
  187. }
  188. public static String normalizePath( String path )
  189. {
  190. return path.replace( '\\', '/' );
  191. }
  192. }