1 package org.apache.maven.plugins.assembly.filter;
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.plugins.assembly.utils.AssemblyFileUtils;
23 import org.codehaus.plexus.archiver.Archiver;
24 import org.codehaus.plexus.archiver.ArchiverException;
25 import org.codehaus.plexus.archiver.ResourceIterator;
26 import org.codehaus.plexus.archiver.UnArchiver;
27 import org.codehaus.plexus.components.io.fileselectors.FileInfo;
28 import org.codehaus.plexus.util.IOUtil;
29
30 import javax.annotation.Nonnull;
31 import java.io.BufferedReader;
32 import java.io.File;
33 import java.io.FileOutputStream;
34 import java.io.IOException;
35 import java.io.InputStreamReader;
36 import java.io.OutputStreamWriter;
37 import java.io.PrintWriter;
38 import java.util.ArrayList;
39 import java.util.HashMap;
40 import java.util.List;
41 import java.util.Map;
42
43 abstract class AbstractLineAggregatingHandler
44 implements ContainerDescriptorHandler
45 {
46
47 private Map<String, List<String>> catalog = new HashMap<String, List<String>>();
48
49 private boolean excludeOverride = false;
50
51 protected abstract String getOutputPathPrefix( final FileInfo fileInfo );
52
53 protected abstract boolean fileMatches( final FileInfo fileInfo );
54
55 String getEncoding()
56 {
57 return "UTF-8";
58 }
59
60 @Override
61 public void finalizeArchiveCreation( final Archiver archiver )
62 {
63
64
65
66
67
68 for ( final ResourceIterator it = archiver.getResources(); it.hasNext(); )
69 {
70 it.next();
71 }
72
73 addToArchive( archiver );
74 }
75
76 void addToArchive( final Archiver archiver )
77 {
78 for ( final Map.Entry<String, List<String>> entry : catalog.entrySet() )
79 {
80 final String name = entry.getKey();
81 final String fname = new File( name ).getName();
82
83 PrintWriter writer = null;
84 File f;
85 try
86 {
87 f = File.createTempFile( "assembly-" + fname, ".tmp" );
88 f.deleteOnExit();
89
90 writer = new PrintWriter( new OutputStreamWriter( new FileOutputStream( f ), getEncoding() ) );
91 for ( final String line : entry.getValue() )
92 {
93 writer.println( line );
94 }
95
96 writer.close();
97 writer = null;
98 }
99 catch ( final IOException e )
100 {
101 throw new ArchiverException(
102 "Error adding aggregated content for: " + fname + " to finalize archive creation. Reason: "
103 + e.getMessage(), e );
104 }
105 finally
106 {
107 IOUtil.close( writer );
108 }
109
110 excludeOverride = true;
111 archiver.addFile( f, name );
112 excludeOverride = false;
113 }
114 }
115
116 @Override
117 public void finalizeArchiveExtraction( final UnArchiver unArchiver )
118 {
119 }
120
121 @Override
122 public List<String> getVirtualFiles()
123 {
124 return new ArrayList<String>( catalog.keySet() );
125 }
126
127 @Override
128 public boolean isSelected( @Nonnull final FileInfo fileInfo )
129 throws IOException
130 {
131 if ( excludeOverride )
132 {
133 return true;
134 }
135
136 String name = AssemblyFileUtils.normalizeFileInfo( fileInfo );
137
138 if ( fileInfo.isFile() && fileMatches( fileInfo ) )
139 {
140 name = getOutputPathPrefix( fileInfo ) + new File( name ).getName();
141
142 List<String> lines = catalog.get( name );
143 if ( lines == null )
144 {
145 lines = new ArrayList<String>();
146 catalog.put( name, lines );
147 }
148
149 readLines( fileInfo, lines );
150
151 return false;
152 }
153
154 return true;
155 }
156
157 void readLines( final FileInfo fileInfo, final List<String> lines )
158 throws IOException
159 {
160 BufferedReader reader = null;
161 try
162 {
163 reader = new BufferedReader( new InputStreamReader( fileInfo.getContents(), getEncoding() ) );
164
165 for ( String line = reader.readLine(); line != null; line = reader.readLine() )
166 {
167 if ( !lines.contains( line ) )
168 {
169 lines.add( line );
170 }
171 }
172
173 reader.close();
174 reader = null;
175 }
176 finally
177 {
178 IOUtil.close( reader );
179 }
180 }
181
182 protected final Map<String, List<String>> getCatalog()
183 {
184 return catalog;
185 }
186
187 protected final void setCatalog( final Map<String, List<String>> catalog )
188 {
189 this.catalog = catalog;
190 }
191
192 }