1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.assembly.format;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.InputStreamReader;
24 import java.io.Reader;
25 import java.util.LinkedHashSet;
26 import java.util.List;
27 import java.util.Properties;
28 import java.util.Set;
29
30 import org.apache.commons.io.input.ReaderInputStream;
31 import org.apache.maven.plugins.assembly.AssemblerConfigurationSource;
32 import org.apache.maven.plugins.assembly.utils.AssemblyFileUtils;
33 import org.apache.maven.plugins.assembly.utils.LineEndings;
34 import org.apache.maven.plugins.assembly.utils.LineEndingsUtils;
35 import org.apache.maven.shared.filtering.MavenFilteringException;
36 import org.apache.maven.shared.filtering.MavenReaderFilterRequest;
37 import org.codehaus.plexus.components.io.functions.InputStreamTransformer;
38 import org.codehaus.plexus.components.io.resources.PlexusIoResource;
39
40
41
42
43 public class ReaderFormatter {
44 private static Reader createReaderFilter(
45 Reader source,
46 String escapeString,
47 List<String> delimiters,
48 AssemblerConfigurationSource configSource,
49 boolean isPropertiesFile,
50 Properties additionalProperties)
51 throws IOException {
52 try {
53
54 MavenReaderFilterRequest filterRequest = new MavenReaderFilterRequest(
55 source,
56 true,
57 configSource.getProject(),
58 configSource.getFilters(),
59 isPropertiesFile,
60 configSource.getMavenSession(),
61 additionalProperties);
62
63 filterRequest.setEscapeString(escapeString);
64
65
66 if (delimiters != null && !delimiters.isEmpty()) {
67 LinkedHashSet<String> delims = new LinkedHashSet<>();
68 for (String delim : delimiters) {
69 if (delim == null) {
70
71 delims.add("${*}");
72 } else {
73 delims.add(delim);
74 }
75 }
76
77 filterRequest.setDelimiters(delims);
78 } else {
79 filterRequest.setDelimiters(filterRequest.getDelimiters());
80 }
81
82 filterRequest.setInjectProjectBuildFilters(configSource.isIncludeProjectBuildFilters());
83 return configSource.getMavenReaderFilter().filter(filterRequest);
84 } catch (MavenFilteringException e) {
85 IOException ioe = new IOException("Error filtering file '" + source + "': " + e.getMessage(), e);
86 throw ioe;
87 }
88 }
89
90 private static boolean isForbiddenFiletypes(PlexusIoResource plexusIoResource) {
91 String fileName = plexusIoResource.getName().toLowerCase();
92 return (fileName.endsWith(".zip") || fileName.endsWith(".jar"));
93 }
94
95 private static void checkifFileTypeIsAppropriateForLineEndingTransformation(PlexusIoResource plexusIoResource)
96 throws IOException {
97 if (isForbiddenFiletypes(plexusIoResource)) {
98 throw new IOException("Cannot transform line endings on this kind of file: " + plexusIoResource.getName()
99 + "\nDoing so is more or less guaranteed to destroy the file, and it indicates"
100 + " a problem with your assembly descriptor."
101 + "\nThis error message is new as of 2.5.3. "
102 + "\nEarlier versions of assembly-plugin will silently destroy your file. "
103 + "Fix your descriptor");
104 }
105 }
106
107 public static InputStreamTransformer getFileSetTransformers(
108 final AssemblerConfigurationSource configSource,
109 final boolean isFiltered,
110 final Set<String> nonFilteredFileExtensions,
111 String fileSetLineEnding)
112 throws AssemblyFormattingException {
113 final LineEndings lineEndingToUse = LineEndingsUtils.getLineEnding(fileSetLineEnding);
114
115 final boolean transformLineEndings = !LineEndings.keep.equals(lineEndingToUse);
116
117 if (transformLineEndings || isFiltered) {
118 return new InputStreamTransformer() {
119 @Override
120 public InputStream transform(PlexusIoResource plexusIoResource, InputStream inputStream)
121 throws IOException {
122 final String fileName = plexusIoResource.getName();
123 for (String extension : nonFilteredFileExtensions) {
124 if (fileName.endsWith('.' + extension)) {
125 return inputStream;
126 }
127 }
128
129 InputStream result = inputStream;
130 if (isFiltered) {
131 boolean isPropertyFile = AssemblyFileUtils.isPropertyFile(plexusIoResource.getName());
132 final String encoding = isPropertyFile ? "ISO-8859-1" : configSource.getEncoding();
133
134 Reader source = encoding != null
135 ? new InputStreamReader(inputStream, encoding)
136 : new InputStreamReader(inputStream);
137 Reader filtered = createReaderFilter(
138 source,
139 configSource.getEscapeString(),
140 configSource.getDelimiters(),
141 configSource,
142 isPropertyFile,
143 configSource.getAdditionalProperties());
144 result = encoding != null
145 ? new ReaderInputStream(filtered, encoding)
146 : new ReaderInputStream(filtered);
147 }
148 if (transformLineEndings) {
149 checkifFileTypeIsAppropriateForLineEndingTransformation(plexusIoResource);
150 result = LineEndingsUtils.lineEndingConverter(result, lineEndingToUse);
151 }
152 return result;
153 }
154 };
155 }
156 return null;
157 }
158 }