View Javadoc
1   package org.codehaus.plexus.components.io.resources;
2   
3   /*
4    * Copyright 2007 The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * Default implementation of a resource collection.
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       * Sets a string of patterns, which excluded files
67       * should match.
68       */
69      public void setExcludes(String[] excludes) {
70          this.excludes = excludes;
71      }
72  
73      /**
74       * Returns a string of patterns, which excluded files
75       * should match.
76       */
77      public String[] getExcludes() {
78          return excludes;
79      }
80  
81      /**
82       * Sets a set of file selectors, which should be used
83       * to select the included files.
84       */
85      public void setFileSelectors(FileSelector[] fileSelectors) {
86          this.fileSelectors = fileSelectors;
87      }
88  
89      /**
90       * Returns a set of file selectors, which should be used
91       * to select the included files.
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      * Sets a string of patterns, which included files
111      * should match.
112      */
113     public void setIncludes(String[] includes) {
114         this.includes = includes;
115     }
116 
117     /**
118      * Returns a string of patterns, which included files
119      * should match.
120      */
121     public String[] getIncludes() {
122         return includes;
123     }
124 
125     /**
126      * Sets the prefix, which the file sets contents shall
127      * have.
128      */
129     public void setPrefix(String prefix) {
130         this.prefix = prefix;
131     }
132 
133     /**
134      * Returns the prefix, which the file sets contents shall
135      * have.
136      */
137     public String getPrefix() {
138         return prefix;
139     }
140 
141     /**
142      * Sets, whether the include/exclude patterns are
143      * case sensitive. Defaults to true.
144      */
145     public void setCaseSensitive(boolean caseSensitive) {
146         this.caseSensitive = caseSensitive;
147     }
148 
149     /**
150      * Returns, whether the include/exclude patterns are
151      * case sensitive. Defaults to true.
152      */
153     public boolean isCaseSensitive() {
154         return caseSensitive;
155     }
156 
157     /**
158      * Sets, whether the default excludes are being
159      * applied. Defaults to true.
160      */
161     public void setUsingDefaultExcludes(boolean usingDefaultExcludes) {
162         this.usingDefaultExcludes = usingDefaultExcludes;
163     }
164 
165     /**
166      * Returns, whether the default excludes are being
167      * applied. Defaults to true.
168      */
169     public boolean isUsingDefaultExcludes() {
170         return usingDefaultExcludes;
171     }
172 
173     /**
174      * Sets, whether empty directories are being included. Defaults
175      * to true.
176      */
177     public void setIncludingEmptyDirectories(boolean includingEmptyDirectories) {
178         this.includingEmptyDirectories = includingEmptyDirectories;
179     }
180 
181     /**
182      * Returns, whether empty directories are being included. Defaults
183      * to true.
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      * Returns the file name mappers, which are used to transform
203      * the resource names.
204      */
205     public FileMapper[] getFileMappers() {
206         return fileMappers;
207     }
208 
209     /**
210      * Sets the file name mappers, which are used to transform
211      * the resource names.
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 }