Home | History | Annotate | Download | only in ant
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.ant;
     18 
     19 import java.io.File;
     20 import java.util.Set;
     21 
     22 public class InputPath {
     23 
     24     private final File mFile;
     25     /**
     26      * A set of extensions. Only files with an extension in this set will
     27      * be considered for a modification check. All deleted/created files will still be
     28      * checked.
     29      */
     30     private final Set<String> mTouchedExtensions;
     31 
     32     public InputPath(File file) {
     33         this(file, null);
     34     }
     35 
     36     public InputPath(File file, Set<String> extensionsToCheck) {
     37         mFile = file;
     38         mTouchedExtensions = extensionsToCheck;
     39     }
     40 
     41     public File getFile() {
     42         return mFile;
     43     }
     44 
     45     /**
     46      * Returns whether this input path (likely actually a folder) must check this files for
     47      * modification (all files are checked for add/delete).
     48      *
     49      * This is configured by constructing the {@link InputPath} with additional restriction
     50      * parameters such as specific extensions.
     51      * @param file the file to check
     52      * @return true if the file must be checked for modification.
     53      */
     54     public boolean checksForModification(File file) {
     55         if (ignores(file)) {
     56             return false;
     57         }
     58 
     59         if (mTouchedExtensions != null &&
     60                 mTouchedExtensions.contains(getExtension(file)) == false) {
     61             return false;
     62         }
     63 
     64         return true;
     65     }
     66 
     67     /**
     68      * Returns whether the InputPath ignores a given file or folder. If it is ignored then
     69      * the file (or folder) is not checked for any event (modification/add/delete).
     70      * If it's a folder, then it and its content are completely ignored.
     71      * @param file the file or folder to check
     72      * @return true if the file or folder are ignored.
     73      */
     74     public boolean ignores(File file) {
     75         return false;
     76     }
     77 
     78     /**
     79      *  Gets the extension (if present) on a file by looking at the filename
     80      *  @param file the file to get the extension of
     81      *  @return the extension if present, or the empty string if the filename doesn't have
     82      *          and extension.
     83      */
     84    protected static String getExtension(File file) {
     85        String filename = file.getName();
     86        int index = filename.lastIndexOf('.');
     87        if (index == -1) {
     88            return "";
     89        }
     90        // Don't include the leading '.' in the extension
     91        return filename.substring(index + 1);
     92    }
     93 
     94 }
     95