View Javadoc

1   package org.apache.maven.plugin.clean;
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 java.io.File;
23  import java.util.Arrays;
24  
25  import org.codehaus.plexus.util.DirectoryScanner;
26  import org.codehaus.plexus.util.SelectorUtils;
27  
28  /**
29   * Selects paths based on Ant-like glob patterns.
30   * 
31   * @author Benjamin Bentmann
32   */
33  class GlobSelector
34      implements Selector
35  {
36  
37      private final String[] includes;
38  
39      private final String[] excludes;
40  
41      private final String str;
42  
43      public GlobSelector( String[] includes, String[] excludes )
44      {
45          this( includes, excludes, false );
46      }
47  
48      public GlobSelector( String[] includes, String[] excludes, boolean useDefaultExcludes )
49      {
50          this.str = "includes = " + toString( includes ) + ", excludes = " + toString( excludes );
51          this.includes = normalizePatterns( includes );
52          this.excludes = normalizePatterns( addDefaultExcludes( excludes, useDefaultExcludes ) );
53      }
54  
55      private static String toString( String[] patterns )
56      {
57          return ( patterns == null ) ? "[]" : Arrays.asList( patterns ).toString();
58      }
59  
60      private static String[] addDefaultExcludes( String[] excludes, boolean useDefaultExcludes )
61      {
62          String[] defaults = DirectoryScanner.DEFAULTEXCLUDES;
63          if ( !useDefaultExcludes )
64          {
65              return excludes;
66          }
67          else if ( excludes == null || excludes.length <= 0 )
68          {
69              return defaults;
70          }
71          else
72          {
73              String[] patterns = new String[excludes.length + defaults.length];
74              System.arraycopy( excludes, 0, patterns, 0, excludes.length );
75              System.arraycopy( defaults, 0, patterns, excludes.length, defaults.length );
76              return patterns;
77          }
78      }
79  
80      private static String[] normalizePatterns( String[] patterns )
81      {
82          String[] normalized;
83  
84          if ( patterns != null )
85          {
86              normalized = new String[patterns.length];
87              for ( int i = patterns.length - 1; i >= 0; i-- )
88              {
89                  normalized[i] = normalizePattern( patterns[i] );
90              }
91          }
92          else
93          {
94              normalized = new String[0];
95          }
96  
97          return normalized;
98      }
99  
100     private static String normalizePattern( String pattern )
101     {
102         String normalized = pattern.replace( ( File.separatorChar == '/' ) ? '\\' : '/', File.separatorChar );
103 
104         if ( normalized.endsWith( File.separator ) )
105         {
106             normalized += "**";
107         }
108 
109         return normalized;
110     }
111 
112     public boolean isSelected( String pathname )
113     {
114         return ( includes.length <= 0 || isMatched( pathname, includes ) )
115             && ( excludes.length <= 0 || !isMatched( pathname, excludes ) );
116     }
117 
118     private static boolean isMatched( String pathname, String[] patterns )
119     {
120         for ( int i = patterns.length - 1; i >= 0; i-- )
121         {
122             String pattern = patterns[i];
123             if ( SelectorUtils.matchPath( pattern, pathname ) )
124             {
125                 return true;
126             }
127         }
128         return false;
129     }
130 
131     public boolean couldHoldSelected( String pathname )
132     {
133         for ( int i = includes.length - 1; i >= 0; i-- )
134         {
135             String include = includes[i];
136             if ( SelectorUtils.matchPatternStart( include, pathname ) )
137             {
138                 return true;
139             }
140         }
141         return includes.length <= 0;
142     }
143 
144     public String toString()
145     {
146         return str;
147     }
148 
149 }