xref: /OpenGrok/opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/PathAccepter.java (revision 5d9f3aa0ca3da3a714233f987fa732f62c0965f6)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * See LICENSE.txt included in this distribution for the specific
9  * language governing permissions and limitations under the License.
10  *
11  * When distributing Covered Code, include this CDDL HEADER in each
12  * file and include the License file at LICENSE.txt.
13  * If applicable, add the following below this CDDL HEADER, with the
14  * fields enclosed by brackets "[]" replaced with your own identifying
15  * information: Portions Copyright [yyyy] [name of copyright owner]
16  *
17  * CDDL HEADER END
18  */
19 
20 /*
21  * Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
22  * Portions Copyright (c) 2017, 2020, Chris Fraire <cfraire@me.com>.
23  */
24 package org.opengrok.indexer.configuration;
25 
26 import org.opengrok.indexer.logger.LoggerFactory;
27 
28 import java.io.File;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31 
32 /**
33  * Represents a gatekeeper that decides whether a particular file or directory
34  * is acceptable for history or code analysis with respect to configuration of
35  * ignored files and directories or of specified inclusive filtering.
36  */
37 public class PathAccepter {
38 
39     private static final Logger LOGGER = LoggerFactory.getLogger(PathAccepter.class);
40 
41     private final IgnoredNames ignoredNames;
42     private final Filter includedNames;
43 
44     /**
45      * Package-private to be initialized from the runtime environment.
46      */
PathAccepter(IgnoredNames ignoredNames, Filter includedNames)47     PathAccepter(IgnoredNames ignoredNames, Filter includedNames) {
48         this.ignoredNames = ignoredNames;
49         this.includedNames = includedNames;
50     }
51 
52     /**
53      * Evaluates the specified {@code file} versus the runtime configuration of
54      * ignored files and directories or of specified inclusive filtering, and
55      * returns a value whether the {@code file} is to be accepted.
56      * @param file a defined instance under the source root
57      */
accept(File file)58     public boolean accept(File file) {
59         if (!includedNames.isEmpty()
60                 && // the filter should not affect directory names
61                 (!(file.isDirectory() || includedNames.match(file)))) {
62             LOGGER.log(Level.FINER, "not including {0}", file.getAbsolutePath());
63             return false;
64         }
65 
66         if (ignoredNames.ignore(file)) {
67             LOGGER.log(Level.FINER, "ignoring {0}", file.getAbsolutePath());
68             return false;
69         }
70 
71         return true;
72     }
73 }
74