1 package org.apache.maven.plugin.assembly.format;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.plugin.assembly.AssemblerConfigurationSource;
23 import org.apache.maven.plugin.assembly.utils.AssemblyFileUtils;
24 import org.apache.maven.shared.filtering.MavenFilteringException;
25 import org.codehaus.plexus.logging.Logger;
26 import org.codehaus.plexus.util.FileUtils;
27 import org.codehaus.plexus.util.IOUtil;
28
29 import java.io.File;
30 import java.io.FileNotFoundException;
31 import java.io.FileReader;
32 import java.io.IOException;
33 import java.io.Reader;
34 import java.util.Locale;
35
36
37
38
39 public class FileFormatter
40 {
41
42 private final Logger logger;
43
44 private final AssemblerConfigurationSource configSource;
45
46 public FileFormatter( AssemblerConfigurationSource configSource, Logger logger )
47 {
48 this.configSource = configSource;
49 this.logger = logger;
50 }
51
52 public File format( File source, boolean filter, String lineEnding )
53 throws AssemblyFormattingException
54 {
55 return format ( source, filter, lineEnding, configSource.getTemporaryRootDirectory() );
56 }
57
58 public File format( File source, boolean filter, String lineEnding, File tempRoot )
59 throws AssemblyFormattingException
60 {
61 AssemblyFileUtils.verifyTempDirectoryAvailability( tempRoot, logger );
62
63 File result = source;
64
65 if ( filter )
66 result = doFileFilter( source, tempRoot );
67
68 String lineEndingChars = AssemblyFileUtils.getLineEndingCharacters( lineEnding );
69 if ( lineEndingChars != null )
70 {
71 result = formatLineEndings( lineEndingChars, result, tempRoot );
72 }
73
74 return result;
75 }
76
77 private File doFileFilter( File source, File tempRoot )
78 throws AssemblyFormattingException
79 {
80 try
81 {
82 File target = FileUtils.createTempFile( source.getName() + ".", ".filtered", tempRoot );
83
84
85 boolean isPropertiesFile = source.getName().toLowerCase( Locale.ENGLISH ).endsWith( ".properties" );
86
87 configSource.getMavenFileFilter().copyFile( source, target, true, configSource.getProject(),
88 configSource.getFilters(), isPropertiesFile, null, configSource.getMavenSession() );
89
90 return target;
91 }
92 catch (MavenFilteringException e)
93 {
94 throw new AssemblyFormattingException( "Error filtering file '" + source + "': " + e.getMessage(), e );
95 }
96 }
97
98 private File formatLineEndings( String lineEndingChars, File source, File tempRoot )
99 throws AssemblyFormattingException
100 {
101 Reader contentReader = null;
102 try
103 {
104 contentReader = new FileReader( source );
105
106 File target = FileUtils.createTempFile( source.getName() + ".", ".formatted", tempRoot );
107
108 AssemblyFileUtils.convertLineEndings( contentReader, target, lineEndingChars );
109
110 return target;
111 }
112 catch ( FileNotFoundException e )
113 {
114 throw new AssemblyFormattingException( "File to filter not found: " + e.getMessage(), e );
115 }
116 catch ( IOException e )
117 {
118 throw new AssemblyFormattingException( "Error line formatting file '" + source + "': " + e.getMessage(), e );
119 }
120 finally
121 {
122 IOUtil.close( contentReader );
123 }
124 }
125 }