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.MavenFileFilterRequest; |
25 | |
import org.apache.maven.shared.filtering.MavenFilteringException; |
26 | |
import org.codehaus.plexus.logging.Logger; |
27 | |
import org.codehaus.plexus.util.FileUtils; |
28 | |
import org.codehaus.plexus.util.IOUtil; |
29 | |
import org.codehaus.plexus.util.ReaderFactory; |
30 | |
import org.codehaus.plexus.util.StringUtils; |
31 | |
|
32 | |
import java.io.File; |
33 | |
import java.io.FileInputStream; |
34 | |
import java.io.FileNotFoundException; |
35 | |
import java.io.IOException; |
36 | |
import java.io.InputStreamReader; |
37 | |
import java.io.Reader; |
38 | |
import java.util.Locale; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public class FileFormatter |
44 | |
{ |
45 | |
|
46 | |
private final Logger logger; |
47 | |
|
48 | |
private final AssemblerConfigurationSource configSource; |
49 | |
|
50 | |
public FileFormatter( AssemblerConfigurationSource configSource, Logger logger ) |
51 | 75 | { |
52 | 75 | this.configSource = configSource; |
53 | 75 | this.logger = logger; |
54 | 75 | } |
55 | |
|
56 | |
public File format( File source, boolean filter, String lineEnding, String encoding ) |
57 | |
throws AssemblyFormattingException |
58 | |
{ |
59 | 75 | return format ( source, filter, lineEnding, configSource.getTemporaryRootDirectory(), encoding ); |
60 | |
} |
61 | |
|
62 | |
public File format( File source, boolean filter, String lineEnding, File tempRoot, String encoding ) |
63 | |
throws AssemblyFormattingException |
64 | |
{ |
65 | 96 | AssemblyFileUtils.verifyTempDirectoryAvailability( tempRoot, logger ); |
66 | |
|
67 | 96 | File result = source; |
68 | |
|
69 | 96 | if ( StringUtils.isEmpty( encoding ) && filter ) |
70 | |
{ |
71 | 0 | logger.warn( |
72 | |
"File encoding has not been set, using platform encoding " + ReaderFactory.FILE_ENCODING |
73 | |
+ ", i.e. build is platform dependent!" ); |
74 | |
} |
75 | |
|
76 | 96 | if ( filter ) |
77 | 36 | result = doFileFilter( source, tempRoot, encoding, configSource.getEscapeString() ); |
78 | |
|
79 | 96 | String lineEndingChars = AssemblyFileUtils.getLineEndingCharacters( lineEnding ); |
80 | 96 | if ( lineEndingChars != null ) |
81 | |
{ |
82 | 24 | result = formatLineEndings( lineEndingChars, result, tempRoot, encoding ); |
83 | |
} |
84 | |
|
85 | 96 | return result; |
86 | |
} |
87 | |
|
88 | |
private File doFileFilter( File source, File tempRoot, String encoding, String escapeString ) |
89 | |
throws AssemblyFormattingException |
90 | |
{ |
91 | |
try |
92 | |
{ |
93 | 36 | File target = FileUtils.createTempFile( source.getName() + ".", ".filtered", tempRoot ); |
94 | |
|
95 | |
|
96 | 36 | boolean isPropertiesFile = source.getName().toLowerCase( Locale.ENGLISH ).endsWith( ".properties" ); |
97 | |
|
98 | 36 | MavenFileFilterRequest filterRequest = new MavenFileFilterRequest( source, target, true, configSource.getProject(), |
99 | |
configSource.getFilters(), isPropertiesFile, encoding, configSource.getMavenSession(), null ); |
100 | 36 | filterRequest.setEscapeString( escapeString ); |
101 | 36 | filterRequest.setInjectProjectBuildFilters( true ); |
102 | 36 | configSource.getMavenFileFilter().copyFile( filterRequest ); |
103 | |
|
104 | 36 | return target; |
105 | |
} |
106 | 0 | catch (MavenFilteringException e) |
107 | |
{ |
108 | 0 | throw new AssemblyFormattingException( "Error filtering file '" + source + "': " + e.getMessage(), e ); |
109 | |
} |
110 | |
} |
111 | |
|
112 | |
private File formatLineEndings( String lineEndingChars, File source, File tempRoot, String encoding ) |
113 | |
throws AssemblyFormattingException |
114 | |
{ |
115 | 24 | Reader contentReader = null; |
116 | |
try |
117 | |
{ |
118 | 24 | if ( encoding == null ) |
119 | |
{ |
120 | |
|
121 | 0 | contentReader = new InputStreamReader( new FileInputStream( source ) ); |
122 | |
} |
123 | |
else |
124 | |
{ |
125 | |
|
126 | 24 | contentReader = new InputStreamReader( new FileInputStream( source ), encoding ); |
127 | |
} |
128 | |
|
129 | 24 | File target = FileUtils.createTempFile( source.getName() + ".", ".formatted", tempRoot ); |
130 | |
|
131 | 24 | AssemblyFileUtils.convertLineEndings( contentReader, target, lineEndingChars, encoding ); |
132 | |
|
133 | 24 | return target; |
134 | |
} |
135 | 0 | catch ( FileNotFoundException e ) |
136 | |
{ |
137 | 0 | throw new AssemblyFormattingException( "File to filter not found: " + e.getMessage(), e ); |
138 | |
} |
139 | 0 | catch ( IOException e ) |
140 | |
{ |
141 | 0 | throw new AssemblyFormattingException( "Error line formatting file '" + source + "': " + e.getMessage(), e ); |
142 | |
} |
143 | |
finally |
144 | |
{ |
145 | 24 | IOUtil.close( contentReader ); |
146 | |
} |
147 | |
} |
148 | |
} |