Coverage Report - org.apache.maven.plugin.war.util.PathSet
 
Classes in this File Line Coverage Branch Coverage Complexity
PathSet
100%
47/47
100%
12/12
1,263
 
 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
 36  
  * the paths normalization. All paths are converted to unix form (slashes) and
 37  
  * they don't start with starting /.
 38  
  *
 39  
  * @author Piotr Tabor
 40  
  * @version $Id: PathSet.java 791368 2009-07-06 04:08:27Z snicoll $
 41  
  */
 42  
 
 43  
 public class PathSet
 44  
 {
 45  
 
 46  
     /**
 47  
      * Set of normalized paths
 48  
      */
 49  382
     private Set/* <String> */pathsSet = new LinkedHashSet();
 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  2441
         return normalizeFilePathStatic( path );
 65  
     }
 66  
 
 67  
     /*-------------------- Business interface ------------------------------*/
 68  
 
 69  
     /**
 70  
      * Creates an empty paths set
 71  
      */
 72  
     public PathSet()
 73  228
     {
 74  
         /*Empty default constructor*/
 75  228
     }
 76  
 
 77  
     /**
 78  
      * Creates paths set and normalizate and adds all 'paths'.
 79  
      * The source 'paths' will not be changed
 80  
      *
 81  
      * @param paths to be added
 82  
      */
 83  
     public PathSet( Collection/*String>*/ paths )
 84  1
     {
 85  1
         addAll( paths );
 86  1
     }
 87  
 
 88  
     /**
 89  
      * Creates paths set and normalizate and adds all 'paths'.
 90  
      * The source 'paths' will not be changed
 91  
      *
 92  
      * @param paths to be added
 93  
      */
 94  
     public PathSet( String[] paths )
 95  153
     {
 96  153
         addAll( paths );
 97  153
     }
 98  
 
 99  
     /**
 100  
      * Normalizes and adds given path to the set.
 101  
      *
 102  
      * @param path to be added
 103  
      */
 104  
     public void add( String path )
 105  
     {
 106  965
         pathsSet.add( normalizeFilePath( path ) );
 107  965
     }
 108  
 
 109  
     /**
 110  
      * Normalizes and adds given paths (collection of strings)
 111  
      * to the set. The source collection will not be changed
 112  
      *
 113  
      * @param paths  - collection of strings to be added
 114  
      * @param prefix added to all given paths
 115  
      */
 116  
     public void addAll( Collection/*<String>*/ paths, String prefix )
 117  
     {
 118  2
         for ( Iterator iter = paths.iterator(); iter.hasNext(); )
 119  
         {
 120  8
             add( prefix + iter.next() );
 121  
         }
 122  2
     }
 123  
 
 124  
     /**
 125  
      * Normalizes and adds given paths to the set.
 126  
      * The source collection will not be changed
 127  
      *
 128  
      * @param paths  to be added
 129  
      * @param prefix added to all given paths
 130  
      */
 131  
     public void addAll( String[] paths, String prefix )
 132  
     {
 133  519
         for ( int i = 0; i < paths.length; i++ )
 134  
         {
 135  360
             add( prefix + paths[i] );
 136  
         }
 137  159
     }
 138  
 
 139  
     /**
 140  
      * Adds given paths to the set.
 141  
      * The source collection will not be changed
 142  
      *
 143  
      * @param paths  to be added
 144  
      * @param prefix added to all given paths
 145  
      */
 146  
     public void addAll( PathSet paths, String prefix )
 147  
     {
 148  16
         for ( Iterator iter = paths.iterator(); iter.hasNext(); )
 149  
         {
 150  41
             add( prefix + iter.next() );
 151  
         }
 152  16
     }
 153  
 
 154  
     /**
 155  
      * Normalizes and adds given paths (collection of strings)
 156  
      * to the set. The source collection will not be changed
 157  
      *
 158  
      * @param paths - collection of strings to be added
 159  
      */
 160  
     public void addAll( Collection/*<String>*/ paths )
 161  
     {
 162  2
         addAll( paths, "" );
 163  2
     }
 164  
 
 165  
     /**
 166  
      * Normalizes and adds given paths to the set.
 167  
      * The source collection will not be changed
 168  
      *
 169  
      * @param paths to be added
 170  
      */
 171  
     public void addAll( String[] paths )
 172  
     {
 173  154
         addAll( paths, "" );
 174  154
     }
 175  
 
 176  
     /**
 177  
      * Adds given paths to the set.
 178  
      * The source collection will not be changed
 179  
      *
 180  
      * @param paths to be added
 181  
      */
 182  
     public void addAll( PathSet paths )
 183  
     {
 184  16
         addAll( paths, "" );
 185  16
     }
 186  
 
 187  
     /**
 188  
      * Checks if the set constains given path. The path is normalized
 189  
      * before check.
 190  
      *
 191  
      * @param path we are looking for in the set.
 192  
      * @return information if the set constains the path.
 193  
      */
 194  
     public boolean contains( String path )
 195  
     {
 196  1473
         return pathsSet.contains( normalizeFilePath( path ) );
 197  
     }
 198  
 
 199  
     /**
 200  
      * Removes the specified path if it exists.
 201  
      *
 202  
      * @param path the path to remove
 203  
      * @return true if the path was removed, false if it did not existed
 204  
      */
 205  
     boolean remove( String path )
 206  
     {
 207  1
         final String normalizedPath = normalizeFilePath( path );
 208  1
         return pathsSet.remove( normalizedPath );
 209  
 
 210  
     }
 211  
 
 212  
     /**
 213  
      * Returns iterator of normalized paths (strings)
 214  
      *
 215  
      * @return iterator of normalized paths (strings)
 216  
      */
 217  
     public Iterator iterator()
 218  
     {
 219  235
         return pathsSet.iterator();
 220  
     }
 221  
 
 222  
     /**
 223  
      * Adds given prefix to all paths in the set.
 224  
      * <p/>
 225  
      * The prefix should be ended by '/'. The generated paths are normalized.
 226  
      *
 227  
      * @param prefix to be added to all items
 228  
      */
 229  
     public void addPrefix( String prefix )
 230  
     {
 231  1
         final Set/*<String>*/ newSet = new HashSet();
 232  1
         for ( Iterator iter = pathsSet.iterator(); iter.hasNext(); )
 233  
         {
 234  2
             String path = (String) iter.next();
 235  2
             newSet.add( normalizeFilePath( prefix + path ) );
 236  2
         }
 237  1
         pathsSet = newSet;
 238  1
     }
 239  
 
 240  
     /**
 241  
      * Returns count of the paths in the set
 242  
      *
 243  
      * @return count of the paths in the set
 244  
      */
 245  
     public int size()
 246  
     {
 247  16
         return pathsSet.size();
 248  
     }
 249  
 
 250  
     /**
 251  
      * Adds to the set all files in the given directory
 252  
      *
 253  
      * @param directory that will be searched for file's paths to add
 254  
      * @param prefix    to be added to all found files
 255  
      */
 256  
     public void addAllFilesInDirectory( File directory, String prefix )
 257  
     {
 258  3
         DirectoryScanner scanner = new DirectoryScanner();
 259  3
         scanner.setBasedir( directory );
 260  3
         scanner.scan();
 261  3
         addAll( scanner.getIncludedFiles(), prefix );
 262  3
     }
 263  
 
 264  
     /*-------------------- Universal static mathods ------------------------*/
 265  
     /**
 266  
      * The method normalizes the path.
 267  
      * <p/>
 268  
      * <ul>
 269  
      * <li>changes directory separator to unix's separator(/)</li>
 270  
      * <li>deletes all trailing slashes</li>
 271  
      * </ul>
 272  
      *
 273  
      * @param path to normalization
 274  
      * @return normalized path
 275  
      */
 276  
     public static String normalizeFilePathStatic( String path )
 277  
     {
 278  2456
         return trimTrailingSlashes( StringUtils.replace( path, '\\', '/' ) );
 279  
     }
 280  
 
 281  
     /**
 282  
      * The method deletes all trailing slashes from the given string
 283  
      *
 284  
      * @param str a string
 285  
      * @return trimed string
 286  
      */
 287  
     public static String trimTrailingSlashes( String str )
 288  
     {
 289  
         int i;
 290  2465
         for ( i = 0; i < str.length() && str.charAt( i ) == '/'; i++ )
 291  
         {
 292  
             // just calculate i
 293  
         }
 294  2465
         return str.substring( i );
 295  
     }
 296  
 
 297  
 }