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 if (file == null) { 38 throw new RuntimeException("File in InputPath(File) can't be null"); 39 } 40 mFile = file; 41 mTouchedExtensions = extensionsToCheck; 42 } 43 44 public File getFile() { 45 return mFile; 46 } 47 48 /** 49 * Returns whether this input path (likely actually a folder) must check this files for 50 * modification (all files are checked for add/delete). 51 * 52 * This is configured by constructing the {@link InputPath} with additional restriction 53 * parameters such as specific extensions. 54 * @param file the file to check 55 * @return true if the file must be checked for modification. 56 */ 57 public boolean checksForModification(File file) { 58 if (ignores(file)) { 59 return false; 60 } 61 62 if (mTouchedExtensions != null && 63 mTouchedExtensions.contains(getExtension(file)) == false) { 64 return false; 65 } 66 67 return true; 68 } 69 70 /** 71 * Returns whether the InputPath ignores a given file or folder. If it is ignored then 72 * the file (or folder) is not checked for any event (modification/add/delete). 73 * If it's a folder, then it and its content are completely ignored. 74 * @param file the file or folder to check 75 * @return true if the file or folder are ignored. 76 */ 77 public boolean ignores(File file) { 78 // always ignore hidden files/folders. 79 return file.getName().startsWith("."); 80 } 81 82 /** 83 * Gets the extension (if present) on a file by looking at the filename 84 * @param file the file to get the extension from 85 * @return the extension if present, or the empty string if the filename doesn't have 86 * and extension. 87 */ 88 protected static String getExtension(File file) { 89 return getExtension(file.getName()); 90 } 91 92 /** 93 * Gets the extension (if present) on a file by looking at the filename 94 * @param fileName the filename to get the extension from 95 * @return the extension if present, or the empty string if the filename doesn't have 96 * and extension. 97 */ 98 protected static String getExtension(String fileName) { 99 int index = fileName.lastIndexOf('.'); 100 if (index == -1) { 101 return ""; 102 } 103 // Don't include the leading '.' in the extension 104 return fileName.substring(index + 1); 105 } 106 107 } 108