aboutsummaryrefslogtreecommitdiffstats
path: root/anttasks/src/com/android/ant/InputPath.java
blob: 2299f0767685db8b2cd7afa96b071c11e19d442c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ant;

import java.io.File;
import java.util.Set;

public class InputPath {

    private final File mFile;
    /**
     * A set of extensions. Only files with an extension in this set will
     * be considered for a modification check. All deleted/created files will still be
     * checked.
     */
    private final Set<String> mTouchedExtensions;

    public InputPath(File file) {
        this(file, null);
    }

    public InputPath(File file, Set<String> extensionsToCheck) {
        if (file == null) {
            throw new RuntimeException("File in InputPath(File) can't be null");
        }
        mFile = file;
        mTouchedExtensions = extensionsToCheck;
    }

    public File getFile() {
        return mFile;
    }

    /**
     * Returns whether this input path (likely actually a folder) must check this files for
     * modification (all files are checked for add/delete).
     *
     * This is configured by constructing the {@link InputPath} with additional restriction
     * parameters such as specific extensions.
     * @param file the file to check
     * @return true if the file must be checked for modification.
     */
    public boolean checksForModification(File file) {
        if (ignores(file)) {
            return false;
        }

        if (mTouchedExtensions != null &&
                mTouchedExtensions.contains(getExtension(file)) == false) {
            return false;
        }

        return true;
    }

    /**
     * Returns whether the InputPath ignores a given file or folder. If it is ignored then
     * the file (or folder) is not checked for any event (modification/add/delete).
     * If it's a folder, then it and its content are completely ignored.
     * @param file the file or folder to check
     * @return true if the file or folder are ignored.
     */
    public boolean ignores(File file) {
        // always ignore hidden files/folders.
        return file.getName().startsWith(".");
    }

    /**
     *  Gets the extension (if present) on a file by looking at the filename
     *  @param file the file to get the extension from
     *  @return the extension if present, or the empty string if the filename doesn't have
     *          and extension.
     */
   protected static String getExtension(File file) {
       return getExtension(file.getName());
   }

   /**
    *  Gets the extension (if present) on a file by looking at the filename
    *  @param fileName the filename to get the extension from
    *  @return the extension if present, or the empty string if the filename doesn't have
    *          and extension.
    */
   protected static String getExtension(String fileName) {
       int index = fileName.lastIndexOf('.');
       if (index == -1) {
           return "";
       }
       // Don't include the leading '.' in the extension
       return fileName.substring(index + 1);
   }

}