1 package org.codehaus.plexus.components.io.resources;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import javax.annotation.Nonnull;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.Iterator;
24
25 import org.codehaus.plexus.components.io.filemappers.FileMapper;
26 import org.codehaus.plexus.components.io.filemappers.PrefixFileMapper;
27 import org.codehaus.plexus.components.io.fileselectors.FileSelector;
28 import org.codehaus.plexus.components.io.functions.InputStreamTransformer;
29
30
31
32
33 public abstract class AbstractPlexusIoResourceCollection implements PlexusIoResourceCollection {
34
35 static class IdentityTransformer implements InputStreamTransformer {
36 @Nonnull
37 public InputStream transform(@Nonnull PlexusIoResource resource, @Nonnull InputStream inputStream)
38 throws IOException {
39 return inputStream;
40 }
41 }
42
43 public static final InputStreamTransformer identityTransformer = new IdentityTransformer();
44
45 private String prefix;
46
47 private String[] includes;
48
49 private String[] excludes;
50
51 private FileSelector[] fileSelectors;
52
53 private boolean caseSensitive = true;
54
55 private boolean usingDefaultExcludes = true;
56
57 private boolean includingEmptyDirectories = true;
58
59 private FileMapper[] fileMappers;
60
61 private InputStreamTransformer streamTransformer = identityTransformer;
62
63 protected AbstractPlexusIoResourceCollection() {}
64
65
66
67
68
69 public void setExcludes(String[] excludes) {
70 this.excludes = excludes;
71 }
72
73
74
75
76
77 public String[] getExcludes() {
78 return excludes;
79 }
80
81
82
83
84
85 public void setFileSelectors(FileSelector[] fileSelectors) {
86 this.fileSelectors = fileSelectors;
87 }
88
89
90
91
92
93 public FileSelector[] getFileSelectors() {
94 return fileSelectors;
95 }
96
97 public void setStreamTransformer(InputStreamTransformer streamTransformer) {
98 if (streamTransformer == null) {
99 this.streamTransformer = identityTransformer;
100 } else {
101 this.streamTransformer = streamTransformer;
102 }
103 }
104
105 protected InputStreamTransformer getStreamTransformer() {
106 return streamTransformer;
107 }
108
109
110
111
112
113 public void setIncludes(String[] includes) {
114 this.includes = includes;
115 }
116
117
118
119
120
121 public String[] getIncludes() {
122 return includes;
123 }
124
125
126
127
128
129 public void setPrefix(String prefix) {
130 this.prefix = prefix;
131 }
132
133
134
135
136
137 public String getPrefix() {
138 return prefix;
139 }
140
141
142
143
144
145 public void setCaseSensitive(boolean caseSensitive) {
146 this.caseSensitive = caseSensitive;
147 }
148
149
150
151
152
153 public boolean isCaseSensitive() {
154 return caseSensitive;
155 }
156
157
158
159
160
161 public void setUsingDefaultExcludes(boolean usingDefaultExcludes) {
162 this.usingDefaultExcludes = usingDefaultExcludes;
163 }
164
165
166
167
168
169 public boolean isUsingDefaultExcludes() {
170 return usingDefaultExcludes;
171 }
172
173
174
175
176
177 public void setIncludingEmptyDirectories(boolean includingEmptyDirectories) {
178 this.includingEmptyDirectories = includingEmptyDirectories;
179 }
180
181
182
183
184
185 public boolean isIncludingEmptyDirectories() {
186 return includingEmptyDirectories;
187 }
188
189 protected boolean isSelected(PlexusIoResource plexusIoResource) throws IOException {
190 FileSelector[] fileSelectors = getFileSelectors();
191 if (fileSelectors != null) {
192 for (FileSelector fileSelector : fileSelectors) {
193 if (!fileSelector.isSelected(plexusIoResource)) {
194 return false;
195 }
196 }
197 }
198 return true;
199 }
200
201
202
203
204
205 public FileMapper[] getFileMappers() {
206 return fileMappers;
207 }
208
209
210
211
212
213 public void setFileMappers(FileMapper[] fileMappers) {
214 this.fileMappers = fileMappers;
215 }
216
217 public Iterator<PlexusIoResource> iterator() {
218 try {
219 return getResources();
220 } catch (IOException e) {
221 throw new RuntimeException(e);
222 }
223 }
224
225 public String getName(PlexusIoResource resource) {
226 return getName(resource.getName());
227 }
228
229 protected String getName(String resourceName) {
230 String name = resourceName;
231 final FileMapper[] mappers = getFileMappers();
232 if (mappers != null) {
233 for (FileMapper mapper : mappers) {
234 name = mapper.getMappedFileName(name);
235 }
236 }
237 return PrefixFileMapper.getMappedFileName(getPrefix(), name);
238 }
239
240 public InputStream getInputStream(PlexusIoResource resource) throws IOException {
241 InputStream contents = resource.getContents();
242 return new ClosingInputStream(streamTransformer.transform(resource, contents), contents);
243 }
244
245 public PlexusIoResource resolve(final PlexusIoResource resource) throws IOException {
246 final Deferred deferred = new Deferred(resource, this, streamTransformer != identityTransformer);
247 return deferred.asResource();
248 }
249
250 public long getLastModified() throws IOException {
251 long lastModified = PlexusIoResource.UNKNOWN_MODIFICATION_DATE;
252 for (Iterator<PlexusIoResource> iter = getResources(); iter.hasNext(); ) {
253 long l = iter.next().getLastModified();
254 if (l == PlexusIoResource.UNKNOWN_MODIFICATION_DATE) {
255 return PlexusIoResource.UNKNOWN_MODIFICATION_DATE;
256 }
257 if (lastModified == PlexusIoResource.UNKNOWN_MODIFICATION_DATE || l > lastModified) {
258 lastModified = l;
259 }
260 }
261 return lastModified;
262 }
263 }