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