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