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