View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.clean;
20  
21  import java.io.File;
22  import java.util.Arrays;
23  
24  import org.codehaus.plexus.util.DirectoryScanner;
25  import org.codehaus.plexus.util.SelectorUtils;
26  
27  /**
28   * Selects paths based on Ant-like glob patterns.
29   *
30   * @author Benjamin Bentmann
31   */
32  class GlobSelector implements Selector {
33  
34      private final String[] includes;
35  
36      private final String[] excludes;
37  
38      private final String str;
39  
40      GlobSelector(String[] includes, String[] excludes) {
41          this(includes, excludes, false);
42      }
43  
44      GlobSelector(String[] includes, String[] excludes, boolean useDefaultExcludes) {
45          this.str = "includes = " + toString(includes) + ", excludes = " + toString(excludes);
46          this.includes = normalizePatterns(includes);
47          this.excludes = normalizePatterns(addDefaultExcludes(excludes, useDefaultExcludes));
48      }
49  
50      private static String toString(String[] patterns) {
51          return (patterns == null) ? "[]" : Arrays.asList(patterns).toString();
52      }
53  
54      private static String[] addDefaultExcludes(String[] excludes, boolean useDefaultExcludes) {
55          String[] defaults = DirectoryScanner.DEFAULTEXCLUDES;
56          if (!useDefaultExcludes) {
57              return excludes;
58          } else if (excludes == null || excludes.length <= 0) {
59              return defaults;
60          } else {
61              String[] patterns = new String[excludes.length + defaults.length];
62              System.arraycopy(excludes, 0, patterns, 0, excludes.length);
63              System.arraycopy(defaults, 0, patterns, excludes.length, defaults.length);
64              return patterns;
65          }
66      }
67  
68      private static String[] normalizePatterns(String[] patterns) {
69          String[] normalized;
70  
71          if (patterns != null) {
72              normalized = new String[patterns.length];
73              for (int i = patterns.length - 1; i >= 0; i--) {
74                  normalized[i] = normalizePattern(patterns[i]);
75              }
76          } else {
77              normalized = new String[0];
78          }
79  
80          return normalized;
81      }
82  
83      private static String normalizePattern(String pattern) {
84          if (pattern == null) {
85              return "";
86          }
87  
88          String normalized = pattern.replace((File.separatorChar == '/') ? '\\' : '/', File.separatorChar);
89  
90          if (normalized.endsWith(File.separator)) {
91              normalized += "**";
92          }
93  
94          return normalized;
95      }
96  
97      public boolean isSelected(String pathname) {
98          return (includes.length <= 0 || isMatched(pathname, includes))
99                  && (excludes.length <= 0 || !isMatched(pathname, excludes));
100     }
101 
102     private static boolean isMatched(String pathname, String[] patterns) {
103         for (String pattern : patterns) {
104             if (SelectorUtils.matchPath(pattern, pathname)) {
105                 return true;
106             }
107         }
108         return false;
109     }
110 
111     public boolean couldHoldSelected(String pathname) {
112         for (String include : includes) {
113             if (SelectorUtils.matchPatternStart(include, pathname)) {
114                 return true;
115             }
116         }
117         return includes.length <= 0;
118     }
119 
120     public String toString() {
121         return str;
122     }
123 }