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 2007 Sun Microsystems, Inc. All rights reserved. 22 * Use is subject to license terms. 23 */ 24 package org.opensolaris.opengrok.index; 25 26 import java.io.File; 27 import java.io.FileFilter; 28 import java.util.ArrayList; 29 import java.util.HashSet; 30 import java.util.List; 31 import java.util.Set; 32 import org.apache.oro.io.GlobFilenameFilter; 33 34 /** 35 * This class maintains a list of file names (like "cscope.out"), SRC_ROOT relative 36 * file paths (like "usr/src/uts" or "usr/src/Makefile"), and glob patterns 37 * (like .make.*) which opengrok should ignore. 38 * 39 * @author Chandan 40 */ 41 public class IgnoredNames { 42 private static final String[] defaultPatterns = { 43 "SCCS", 44 "CVS", 45 "RCS", 46 "cscope.in.out", 47 "cscope.out.po", 48 "cscope.out.in", 49 "cscope.po.out", 50 "cscope.po.in", 51 "cscope.files", 52 "cscope.out", 53 "Codemgr_wsdata", 54 ".cvsignore", 55 "CVSROOT", 56 "TAGS", 57 "tags", 58 ".svn", 59 ".git", 60 ".hg", 61 ".hgtags", 62 ".bzr", 63 ".p4config", 64 ".razor", 65 "*~", 66 "deleted_files", 67 ".make.*", 68 ".del-*" 69 }; 70 71 /** The list of exact filenames to ignore */ 72 private Set<String> ignore; 73 /** The list of filenames with wildcards to ignore */ 74 private List<FileFilter> patterns; 75 /** The list of paths that should be ignored */ 76 private List<String> path; 77 /** The full list of all patterns. This list will be saved in the 78 * configuration file (if used) 79 */ 80 private List<String> ignoredPatterns; 81 82 public IgnoredNames() { 83 ignore = new HashSet<String>(); 84 patterns = new ArrayList<FileFilter>(); 85 path = new ArrayList<String>(); 86 ignoredPatterns = new PatternList(this); 87 addDefaultPatterns(); 88 } 89 90 public List<String> getIgnoredPatterns() { 91 return ignoredPatterns; 92 } 93 94 public void setIgnoredPatterns(List<String> ignoredPatterns) { 95 clear(); 96 for (String s : ignoredPatterns) { 97 add(s); 98 } 99 } 100 101 /** 102 * Add a pattern to the list of patterns of filenames to ignore 103 * @param pattern the pattern to ignore 104 */ 105 public void add(String pattern) { 106 if (!ignoredPatterns.contains(pattern)) { 107 ignoredPatterns.add(pattern); 108 } 109 } 110 111 /** 112 * Remove all installed patterns from the list of files to ignore 113 */ 114 public void clear() { 115 patterns.clear(); 116 ignore.clear(); 117 path.clear(); 118 ignoredPatterns.clear(); 119 } 120 121 /** 122 * Should the file be ignored or not? 123 * @param file the file to check 124 * @return true if this file should be ignored, false otherwise 125 */ 126 public boolean ignore(File file) { 127 boolean ret = false; 128 129 if (ignore.contains(file.getName())) { 130 ret = true; 131 } else { 132 for (FileFilter fe : patterns) { 133 if (fe.accept(file)) { 134 ret = true; 135 break; 136 } 137 } 138 } 139 140 if (!ret) { 141 String absolute = file.getAbsolutePath(); 142 for (String s : path) { 143 if (absolute.endsWith(s)) { 144 ret = true; 145 break; 146 } 147 } 148 } 149 150 return ret; 151 } 152 153 /** 154 * Should the file be ignored or not? 155 * @param name the name of the file to check 156 * @return true if this pathname should be ignored, false otherwise 157 */ 158 public boolean ignore(String name) { 159 return ignore(new File(name)); 160 } 161 162 public void addDefaultPatterns() { 163 for (String s : defaultPatterns) { 164 add(s); 165 } 166 } 167 168 private void addPattern(String pattern) { 169 if (pattern.indexOf('*') != -1 || pattern.indexOf('?') != -1) { 170 patterns.add(new GlobFilenameFilter(pattern)); 171 } else if (pattern.indexOf(File.separatorChar) != -1) { 172 if (pattern.charAt(0) == File.separatorChar) { 173 path.add(pattern); 174 } else { 175 path.add(File.separator + pattern); 176 } 177 } else { 178 ignore.add(pattern); 179 } 180 } 181 182 /** 183 * During the load of the configuration file, the framework will add 184 * entries to the ignored pattern list. Since I use them in different 185 * lists, I need to detect when an object is beeing added to this list 186 * (So I may populate it to the correct list as well) 187 */ 188 public class PatternList extends ArrayList<String> { 189 private IgnoredNames owner; 190 191 public PatternList(IgnoredNames owner) { 192 this.owner = owner; 193 } 194 195 196 public boolean add(String pattern) { 197 boolean ret = super.add(pattern); 198 if (ret) { 199 owner.addPattern(pattern); 200 } 201 return ret; 202 } 203 } 204 } 205