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