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 org.codehaus.plexus.components.io.filemappers.FileMapper;
20  import org.codehaus.plexus.components.io.filemappers.PrefixFileMapper;
21  import org.codehaus.plexus.components.io.fileselectors.FileSelector;
22  import org.codehaus.plexus.components.io.functions.InputStreamTransformer;
23  
24  import javax.annotation.Nonnull;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.util.Iterator;
28  
29  /**
30   * Default implementation of a resource collection.
31   */
32  public abstract class AbstractPlexusIoResourceCollection
33      implements PlexusIoResourceCollection
34  {
35  
36      static class IdentityTransformer
37          implements InputStreamTransformer
38      {
39          @Nonnull
40          public InputStream transform( @Nonnull PlexusIoResource resource, @Nonnull InputStream inputStream )
41              throws IOException
42          {
43              return inputStream;
44          }
45      }
46  
47      public static final InputStreamTransformer identityTransformer = new IdentityTransformer();
48  
49      private String prefix;
50  
51      private String[] includes;
52  
53      private String[] excludes;
54  
55      private FileSelector[] fileSelectors;
56  
57      private boolean caseSensitive = true;
58  
59      private boolean usingDefaultExcludes = true;
60  
61      private boolean includingEmptyDirectories = true;
62  
63      private FileMapper[] fileMappers;
64  
65      private InputStreamTransformer streamTransformer = identityTransformer;
66  
67      protected AbstractPlexusIoResourceCollection()
68      {
69      }
70  
71      /**
72       * Sets a string of patterns, which excluded files
73       * should match.
74       */
75      public void setExcludes( String[] excludes )
76      {
77          this.excludes = excludes;
78      }
79  
80      /**
81       * Returns a string of patterns, which excluded files
82       * should match.
83       */
84      public String[] getExcludes()
85      {
86          return excludes;
87      }
88  
89      /**
90       * Sets a set of file selectors, which should be used
91       * to select the included files.
92       */
93      public void setFileSelectors( FileSelector[] fileSelectors )
94      {
95          this.fileSelectors = fileSelectors;
96      }
97  
98      /**
99       * Returns a set of file selectors, which should be used
100      * to select the included files.
101      */
102     public FileSelector[] getFileSelectors()
103     {
104         return fileSelectors;
105     }
106 
107     public void setStreamTransformer( InputStreamTransformer streamTransformer )
108     {
109         if ( streamTransformer == null )
110         {
111             this.streamTransformer = identityTransformer;
112         }
113         else
114         {
115             this.streamTransformer = streamTransformer;
116         }
117     }
118 
119     protected InputStreamTransformer getStreamTransformer()
120     {
121         return streamTransformer;
122     }
123 
124     /**
125      * Sets a string of patterns, which included files
126      * should match.
127      */
128     public void setIncludes( String[] includes )
129     {
130         this.includes = includes;
131     }
132 
133     /**
134      * Returns a string of patterns, which included files
135      * should match.
136      */
137     public String[] getIncludes()
138     {
139         return includes;
140     }
141 
142     /**
143      * Sets the prefix, which the file sets contents shall
144      * have.
145      */
146     public void setPrefix( String prefix )
147     {
148         this.prefix = prefix;
149     }
150 
151     /**
152      * Returns the prefix, which the file sets contents shall
153      * have.
154      */
155     public String getPrefix()
156     {
157         return prefix;
158     }
159 
160     /**
161      * Sets, whether the include/exclude patterns are
162      * case sensitive. Defaults to true.
163      */
164     public void setCaseSensitive( boolean caseSensitive )
165     {
166         this.caseSensitive = caseSensitive;
167     }
168 
169     /**
170      * Returns, whether the include/exclude patterns are
171      * case sensitive. Defaults to true.
172      */
173     public boolean isCaseSensitive()
174     {
175         return caseSensitive;
176     }
177 
178     /**
179      * Sets, whether the default excludes are being
180      * applied. Defaults to true.
181      */
182     public void setUsingDefaultExcludes( boolean usingDefaultExcludes )
183     {
184         this.usingDefaultExcludes = usingDefaultExcludes;
185     }
186 
187     /**
188      * Returns, whether the default excludes are being
189      * applied. Defaults to true.
190      */
191     public boolean isUsingDefaultExcludes()
192     {
193         return usingDefaultExcludes;
194     }
195 
196     /**
197      * Sets, whether empty directories are being included. Defaults
198      * to true.
199      */
200     public void setIncludingEmptyDirectories( boolean includingEmptyDirectories )
201     {
202         this.includingEmptyDirectories = includingEmptyDirectories;
203     }
204 
205     /**
206      * Returns, whether empty directories are being included. Defaults
207      * to true.
208      */
209     public boolean isIncludingEmptyDirectories()
210     {
211         return includingEmptyDirectories;
212     }
213 
214     protected boolean isSelected( PlexusIoResource plexusIoResource )
215         throws IOException
216     {
217         FileSelector[] fileSelectors = getFileSelectors();
218         if ( fileSelectors != null )
219         {
220             for ( FileSelector fileSelector : fileSelectors )
221             {
222                 if ( !fileSelector.isSelected( plexusIoResource ) )
223                 {
224                     return false;
225                 }
226             }
227         }
228         return true;
229     }
230 
231     /**
232      * Returns the file name mappers, which are used to transform
233      * the resource names.
234      */
235     public FileMapper[] getFileMappers()
236     {
237         return fileMappers;
238     }
239 
240     /**
241      * Sets the file name mappers, which are used to transform
242      * the resource names.
243      */
244     public void setFileMappers( FileMapper[] fileMappers )
245     {
246         this.fileMappers = fileMappers;
247     }
248 
249     public Iterator<PlexusIoResource> iterator()
250     {
251         try
252         {
253             return getResources();
254         }
255         catch ( IOException e )
256         {
257             throw new RuntimeException( e );
258         }
259     }
260 
261     public String getName( PlexusIoResource resource )
262     {
263         return getName( resource.getName() );
264     }
265 
266     protected String getName( String resourceName )
267     {
268         String name = resourceName;
269         final FileMapper[] mappers = getFileMappers();
270         if ( mappers != null )
271         {
272             for ( FileMapper mapper : mappers )
273             {
274                 name = mapper.getMappedFileName( name );
275             }
276         }
277         return PrefixFileMapper.getMappedFileName( getPrefix(), name );
278     }
279 
280 
281     public InputStream getInputStream( PlexusIoResource resource )
282         throws IOException
283     {
284         InputStream contents = resource.getContents();
285         return new ClosingInputStream( streamTransformer.transform( resource, contents ), contents );
286     }
287 
288 
289     public PlexusIoResource resolve( final PlexusIoResource resource )
290         throws IOException
291     {
292         final Deferred deferred = new Deferred( resource, this, streamTransformer != identityTransformer );
293         return deferred.asResource();
294     }
295 
296     public long getLastModified()
297         throws IOException
298     {
299         long lastModified = PlexusIoResource.UNKNOWN_MODIFICATION_DATE;
300         for ( final Iterator iter = getResources(); iter.hasNext(); )
301         {
302             final PlexusIoResource res = (PlexusIoResource) iter.next();
303             long l = res.getLastModified();
304             if ( l == PlexusIoResource.UNKNOWN_MODIFICATION_DATE )
305             {
306                 return PlexusIoResource.UNKNOWN_MODIFICATION_DATE;
307             }
308             if ( lastModified == PlexusIoResource.UNKNOWN_MODIFICATION_DATE || l > lastModified )
309             {
310                 lastModified = l;
311             }
312         }
313         return lastModified;
314     }
315 
316 }