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         if ( pattern == null )
103         {
104             return "";
105         }
106 
107         String normalized = pattern.replace( ( File.separatorChar == '/' ) ? '\\' : '/', File.separatorChar );
108 
109         if ( normalized.endsWith( File.separator ) )
110         {
111             normalized += "**";
112         }
113 
114         return normalized;
115     }
116 
117     public boolean isSelected( String pathname )
118     {
119         return ( includes.length <= 0 || isMatched( pathname, includes ) )
120             && ( excludes.length <= 0 || !isMatched( pathname, excludes ) );
121     }
122 
123     private static boolean isMatched( String pathname, String[] patterns )
124     {
125         for ( int i = patterns.length - 1; i >= 0; i-- )
126         {
127             String pattern = patterns[i];
128             if ( SelectorUtils.matchPath( pattern, pathname ) )
129             {
130                 return true;
131             }
132         }
133         return false;
134     }
135 
136     public boolean couldHoldSelected( String pathname )
137     {
138         for ( int i = includes.length - 1; i >= 0; i-- )
139         {
140             String include = includes[i];
141             if ( SelectorUtils.matchPatternStart( include, pathname ) )
142             {
143                 return true;
144             }
145         }
146         return includes.length <= 0;
147     }
148 
149     public String toString()
150     {
151         return str;
152     }
153 
154 }