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 {
52 this.configSource = configSource;
53 this.logger = logger;
54 }
55
56 public File format( File source, boolean filter, String lineEnding, String encoding )
57 throws AssemblyFormattingException
58 {
59 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 AssemblyFileUtils.verifyTempDirectoryAvailability( tempRoot, logger );
66
67 File result = source;
68
69 if ( StringUtils.isEmpty( encoding ) && filter )
70 {
71 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 if ( filter )
77 result = doFileFilter( source, tempRoot, encoding, configSource.getEscapeString() );
78
79 String lineEndingChars = AssemblyFileUtils.getLineEndingCharacters( lineEnding );
80 if ( lineEndingChars != null )
81 {
82 result = formatLineEndings( lineEndingChars, result, tempRoot, encoding );
83 }
84
85 return result;
86 }
87
88 private File doFileFilter( File source, File tempRoot, String encoding, String escapeString )
89 throws AssemblyFormattingException
90 {
91 try
92 {
93 File target = FileUtils.createTempFile( source.getName() + ".", ".filtered", tempRoot );
94
95
96 boolean isPropertiesFile = source.getName().toLowerCase( Locale.ENGLISH ).endsWith( ".properties" );
97
98 MavenFileFilterRequest filterRequest = new MavenFileFilterRequest( source, target, true, configSource.getProject(),
99 configSource.getFilters(), isPropertiesFile, encoding, configSource.getMavenSession(), null );
100 filterRequest.setEscapeString( escapeString );
101 filterRequest.setInjectProjectBuildFilters( true );
102 configSource.getMavenFileFilter().copyFile( filterRequest );
103
104 return target;
105 }
106 catch (MavenFilteringException e)
107 {
108 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 Reader contentReader = null;
116 try
117 {
118 if ( encoding == null )
119 {
120
121 contentReader = new InputStreamReader( new FileInputStream( source ) );
122 }
123 else
124 {
125
126 contentReader = new InputStreamReader( new FileInputStream( source ), encoding );
127 }
128
129 File target = FileUtils.createTempFile( source.getName() + ".", ".formatted", tempRoot );
130
131 AssemblyFileUtils.convertLineEndings( contentReader, target, lineEndingChars, encoding );
132
133 return target;
134 }
135 catch ( FileNotFoundException e )
136 {
137 throw new AssemblyFormattingException( "File to filter not found: " + e.getMessage(), e );
138 }
139 catch ( IOException e )
140 {
141 throw new AssemblyFormattingException( "Error line formatting file '" + source + "': " + e.getMessage(), e );
142 }
143 finally
144 {
145 IOUtil.close( contentReader );
146 }
147 }
148 }