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 | 25 | { |
48 | 25 | this.configSource = configSource; |
49 | 25 | this.logger = logger; |
50 | 25 | } |
51 | |
|
52 | |
public File format( File source, boolean filter, String lineEnding ) |
53 | |
throws AssemblyFormattingException |
54 | |
{ |
55 | 25 | 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 | 32 | AssemblyFileUtils.verifyTempDirectoryAvailability( tempRoot, logger ); |
62 | |
|
63 | 32 | File result = source; |
64 | |
|
65 | 32 | if ( filter ) |
66 | 12 | result = doFileFilter( source, tempRoot ); |
67 | |
|
68 | 32 | String lineEndingChars = AssemblyFileUtils.getLineEndingCharacters( lineEnding ); |
69 | 32 | if ( lineEndingChars != null ) |
70 | |
{ |
71 | 8 | result = formatLineEndings( lineEndingChars, result, tempRoot ); |
72 | |
} |
73 | |
|
74 | 32 | return result; |
75 | |
} |
76 | |
|
77 | |
private File doFileFilter( File source, File tempRoot ) |
78 | |
throws AssemblyFormattingException |
79 | |
{ |
80 | |
try |
81 | |
{ |
82 | 12 | File target = FileUtils.createTempFile( source.getName() + ".", ".filtered", tempRoot ); |
83 | |
|
84 | |
|
85 | 12 | boolean isPropertiesFile = source.getName().toLowerCase( Locale.ENGLISH ).endsWith( ".properties" ); |
86 | |
|
87 | 12 | configSource.getMavenFileFilter().copyFile( source, target, true, configSource.getProject(), |
88 | |
configSource.getFilters(), isPropertiesFile, null, configSource.getMavenSession() ); |
89 | |
|
90 | 12 | return target; |
91 | |
} |
92 | 0 | catch (MavenFilteringException e) |
93 | |
{ |
94 | 0 | 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 | 8 | Reader contentReader = null; |
102 | |
try |
103 | |
{ |
104 | 8 | contentReader = new FileReader( source ); |
105 | |
|
106 | 8 | File target = FileUtils.createTempFile( source.getName() + ".", ".formatted", tempRoot ); |
107 | |
|
108 | 8 | AssemblyFileUtils.convertLineEndings( contentReader, target, lineEndingChars ); |
109 | |
|
110 | 8 | return target; |
111 | |
} |
112 | 0 | catch ( FileNotFoundException e ) |
113 | |
{ |
114 | 0 | throw new AssemblyFormattingException( "File to filter not found: " + e.getMessage(), e ); |
115 | |
} |
116 | 0 | catch ( IOException e ) |
117 | |
{ |
118 | 0 | throw new AssemblyFormattingException( "Error line formatting file '" + source + "': " + e.getMessage(), e ); |
119 | |
} |
120 | |
finally |
121 | |
{ |
122 | 8 | IOUtil.close( contentReader ); |
123 | |
} |
124 | |
} |
125 | |
} |