View Javadoc
1   package org.apache.maven.plugin.war.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.codehaus.plexus.util.DirectoryScanner;
23  import org.codehaus.plexus.util.StringUtils;
24  
25  import java.io.File;
26  import java.util.Collection;
27  import java.util.HashSet;
28  import java.util.Iterator;
29  import java.util.LinkedHashSet;
30  import java.util.Set;
31  
32  /**
33   * Set of file's paths.
34   * <p/>
35   * The class extends functionality of a "normal" set of strings by a process of the paths normalization. All paths are
36   * converted to unix form (slashes) and they don't start with starting /.
37   *
38   * @author Piotr Tabor
39   * @version $Id: PathSet.html 925069 2014-10-08 17:03:57Z khmarbaise $
40   */
41  
42  public class PathSet
43      implements Iterable<String>
44  {
45  
46      /**
47       * Set of normalized paths
48       */
49      private Set<String> pathsSet = new LinkedHashSet<String>();
50  
51      /**
52       * The method normalizes the path.
53       * <p/>
54       * <ul>
55       * <li>changes directory separator to unix's separator(/)</li>
56       * <li>deletes all trailing slashes</li>
57       * </ul>
58       *
59       * @param path to normalization
60       * @return normalized path
61       */
62      protected String normalizeFilePath( String path )
63      {
64          return normalizeFilePathStatic( path );
65      }
66  
67      /*-------------------- Business interface ------------------------------*/
68  
69      /**
70       * Creates an empty paths set
71       */
72      public PathSet()
73      {
74          /* Empty default constructor */
75      }
76  
77      /**
78       * Creates paths set and normalizate and adds all 'paths'. The source 'paths' will not be changed
79       *
80       * @param paths to be added
81       */
82      public PathSet( Collection<String> paths )
83      {
84          addAll( paths );
85      }
86  
87      /**
88       * Creates paths set and normalizate and adds all 'paths'. The source 'paths' will not be changed
89       *
90       * @param paths to be added
91       */
92      public PathSet( String[] paths )
93      {
94          addAll( paths );
95      }
96  
97      /**
98       * Normalizes and adds given path to the set.
99       *
100      * @param path to be added
101      */
102     public void add( String path )
103     {
104         pathsSet.add( normalizeFilePath( path ) );
105     }
106 
107     /**
108      * Normalizes and adds given paths (collection of strings) to the set. The source collection will not be changed
109      *
110      * @param paths - collection of strings to be added
111      * @param prefix added to all given paths
112      */
113     public void addAll( Collection<String> paths, String prefix )
114     {
115         for ( String val : paths )
116         {
117             add( prefix + val );
118         }
119     }
120 
121     /**
122      * Normalizes and adds given paths to the set. The source collection will not be changed
123      *
124      * @param paths to be added
125      * @param prefix added to all given paths
126      */
127     public void addAll( String[] paths, String prefix )
128     {
129         for ( String val : paths )
130         {
131             add( prefix + val );
132         }
133     }
134 
135     /**
136      * Adds given paths to the set. The source collection will not be changed
137      *
138      * @param paths to be added
139      * @param prefix added to all given paths
140      */
141     public void addAll( PathSet paths, String prefix )
142     {
143         for ( String path : paths )
144         {
145             add( prefix + path );
146         }
147     }
148 
149     /**
150      * Normalizes and adds given paths (collection of strings) to the set. The source collection will not be changed
151      *
152      * @param paths - collection of strings to be added
153      */
154     public void addAll( Collection<String> paths )
155     {
156         addAll( paths, "" );
157     }
158 
159     /**
160      * Normalizes and adds given paths to the set. The source collection will not be changed
161      *
162      * @param paths to be added
163      */
164     public void addAll( String[] paths )
165     {
166         addAll( paths, "" );
167     }
168 
169     /**
170      * Adds given paths to the set. The source collection will not be changed
171      *
172      * @param paths to be added
173      */
174     public void addAll( PathSet paths )
175     {
176         addAll( paths, "" );
177     }
178 
179     /**
180      * Checks if the set constains given path. The path is normalized before check.
181      *
182      * @param path we are looking for in the set.
183      * @return information if the set constains the path.
184      */
185     public boolean contains( String path )
186     {
187         return pathsSet.contains( normalizeFilePath( path ) );
188     }
189 
190     /**
191      * Removes the specified path if it exists.
192      *
193      * @param path the path to remove
194      * @return true if the path was removed, false if it did not existed
195      */
196     boolean remove( String path )
197     {
198         final String normalizedPath = normalizeFilePath( path );
199         return pathsSet.remove( normalizedPath );
200 
201     }
202 
203     /**
204      * Returns iterator of normalized paths (strings)
205      *
206      * @return iterator of normalized paths (strings)
207      */
208     public Iterator<String> iterator()
209     {
210         return pathsSet.iterator();
211     }
212 
213     public Collection<String> paths()
214     {
215         return pathsSet;
216     }
217 
218     /**
219      * Adds given prefix to all paths in the set.
220      * <p/>
221      * The prefix should be ended by '/'. The generated paths are normalized.
222      *
223      * @param prefix to be added to all items
224      */
225     public void addPrefix( String prefix )
226     {
227         final Set<String> newSet = new HashSet<String>();
228         for ( String path : pathsSet )
229         {
230             newSet.add( normalizeFilePath( prefix + path ) );
231         }
232         pathsSet = newSet;
233     }
234 
235     /**
236      * Returns count of the paths in the set
237      *
238      * @return count of the paths in the set
239      */
240     public int size()
241     {
242         return pathsSet.size();
243     }
244 
245     /**
246      * Adds to the set all files in the given directory
247      *
248      * @param directory that will be searched for file's paths to add
249      * @param prefix to be added to all found files
250      */
251     public void addAllFilesInDirectory( File directory, String prefix )
252     {
253         DirectoryScanner scanner = new DirectoryScanner();
254         scanner.setBasedir( directory );
255         scanner.scan();
256         addAll( scanner.getIncludedFiles(), prefix );
257     }
258 
259     /*-------------------- Universal static mathods ------------------------*/
260     /**
261      * The method normalizes the path.
262      * <p/>
263      * <ul>
264      * <li>changes directory separator to unix's separator(/)</li>
265      * <li>deletes all trailing slashes</li>
266      * </ul>
267      *
268      * @param path to normalization
269      * @return normalized path
270      */
271     public static String normalizeFilePathStatic( String path )
272     {
273         return trimTrailingSlashes( StringUtils.replace( path, '\\', '/' ) );
274     }
275 
276     /**
277      * The method deletes all trailing slashes from the given string
278      *
279      * @param str a string
280      * @return trimed string
281      */
282     public static String trimTrailingSlashes( String str )
283     {
284         int i;
285         for ( i = 0; i < str.length() && str.charAt( i ) == '/'; i++ )
286         {
287             // just calculate i
288         }
289         return str.substring( i );
290     }
291 
292 }