View Javadoc
1   /**
2    * This file Copyright (c) 2015 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.resourceloader.file;
35  
36  import java.io.IOException;
37  import java.nio.file.DirectoryStream;
38  import java.nio.file.Path;
39  import java.nio.file.PathMatcher;
40  import java.util.ArrayList;
41  import java.util.List;
42  import java.util.regex.Pattern;
43  
44  import com.google.common.base.Predicate;
45  
46  /**
47   * Predicate and DirectoryStream.Filter which checks a given Path is under the root and is able to exclude
48   * - directories,
49   * - file extensions,
50   * - any other pattern.
51   * @see java.nio.file.DirectoryStream.Filter
52   */
53  public class ExclusionsFilter implements Predicate<Path>, DirectoryStream.Filter<Path> {
54  
55      private final Path rootPath;
56      private final List<String> excludePatterns;
57  
58      /**
59       * @param rootPath an absolute root path to evaluate paths against
60       * @param excludedDirectories directory names that will be excluded regardless of their location.
61       * @param excludedExtensions file extensions that will be excluded.
62       * @param otherPatterns regex or glob path patterns, relative to given <code>rootPath</code>. Defaults to regex: if no prefix is given.
63       * @see java.nio.file.FileSystem#getPathMatcher(java.lang.String)
64       */
65      public ExclusionsFilter(Path rootPath, List<String> excludedDirectories, List<String> excludedExtensions, List<String> otherPatterns) {
66          this.rootPath = validateRootPath(rootPath);
67          this.excludePatterns = new ArrayList<>();
68          addDirectoryExcludes(excludePatterns, excludedDirectories);
69          addExtensionExcludes(excludePatterns, excludedExtensions);
70          addPatternList(excludePatterns, otherPatterns);
71      }
72  
73      @Override
74      public boolean accept(Path entry) throws IOException {
75          return apply(entry);
76      }
77  
78      @Override
79      public boolean apply(Path dir) {
80          // is starting visiting from root directory
81          if (!dir.isAbsolute() || !dir.startsWith(rootPath)) {
82              return false;
83          }
84  
85          Path rel = rootPath.relativize(dir);
86  
87          // is an excluded directory
88          for (String excludePattern : excludePatterns) {
89              //final PathMatcher matcher = dir.getFileSystem().getPathMatcher("regex:" + excludePattern.pattern());
90              final PathMatcher matcher = dir.getFileSystem().getPathMatcher(excludePattern);
91              if (matcher.matches(rel)) {
92                  return false;
93              }
94          }
95  
96          return true;
97      }
98  
99      private Path validateRootPath(Path rootPath) {
100         if (!rootPath.isAbsolute()) {
101             throw new IllegalStateException(rootPath + " is not an absolute Path.");
102         }
103         return rootPath;
104     }
105 
106     private void addDirectoryExcludes(List<String> excludePatterns, List<String> excludedDirectories) {
107         for (String directory : excludedDirectories) {
108             excludePatterns.add("regex:(^|.*/)" + Pattern.quote(directory) + "($|/.*)");
109         }
110     }
111 
112     private void addExtensionExcludes(List<String> excludePatterns, List<String> excludedExtensions) {
113         for (String ext : excludedExtensions) {
114             excludePatterns.add("regex:.*\\." + Pattern.quote(ext) + "$");
115         }
116     }
117 
118     private void addPatternList(List<String> excludePatterns, List<String> stringPatterns) {
119         for (String pattern : stringPatterns) {
120             if (pattern.startsWith("glob:") || pattern.startsWith("regex:")) {
121                 excludePatterns.add(pattern);
122             } else {
123                 excludePatterns.add("regex:" + pattern);
124             }
125         }
126     }
127 
128 }