1 19 package org.netbeans.lib.cvsclient.util; 20 21 import java.io.*; 22 import java.util.*; 23 24 27 public class DefaultIgnoreFileFilter implements IgnoreFileFilter { 28 private final List patterns = new LinkedList(); 29 30 private final List localPatterns = new LinkedList(); 31 private boolean processGlobalPatterns = true; 32 private boolean processLocalPatterns = false; 33 private File lastDirectory = null; 34 35 public DefaultIgnoreFileFilter() { 36 } 37 38 43 public DefaultIgnoreFileFilter(List patternList) { 44 for (Iterator it = patternList.iterator(); it.hasNext();) { 45 String patternString = it.next().toString(); 46 SimpleStringPattern pattern = new SimpleStringPattern(patternString); 47 addPattern(pattern); 48 } 49 } 50 51 54 public void addPattern(StringPattern pattern) { 55 if (pattern.toString().equals("!")) { clearPatterns(); 57 } 58 else { 59 patterns.add(pattern); 60 } 61 } 62 63 66 public void addPattern(String pattern) { 67 if (pattern.equals("!")) { clearPatterns(); 69 } 70 else { 71 patterns.add(new SimpleStringPattern(pattern)); 72 } 73 } 74 75 79 public void clearPatterns() { 80 patterns.clear(); 81 } 82 83 91 public boolean shouldBeIgnored(File directory, String noneCvsFile) { 92 if (lastDirectory != directory) { 95 lastDirectory = directory; 96 processGlobalPatterns = true; 97 processLocalPatterns = false; 98 localPatterns.clear(); 99 String filename = directory.getPath() + File.separator + ".cvsignore"; File cvsIgnoreFile = new File(filename); 101 if (cvsIgnoreFile.exists()) { 102 try { 103 List list = parseCvsIgnoreFile(cvsIgnoreFile); 104 for (Iterator it = list.iterator(); it.hasNext();) { 105 String s = it.next().toString(); 106 if (s.equals("!")) { processGlobalPatterns = false; 108 localPatterns.clear(); 109 } else { 110 localPatterns.add(new SimpleStringPattern(s)); 111 } 112 } 113 } 114 catch (IOException ex) { 115 } 117 } 118 processLocalPatterns = localPatterns.size() > 0; 119 } 120 if (processGlobalPatterns) { 121 for (Iterator it = patterns.iterator(); it.hasNext();) { 122 StringPattern pattern = (StringPattern)it.next(); 123 if (pattern.doesMatch(noneCvsFile)) { 124 return true; 125 } 126 } 127 } 128 if (processLocalPatterns) { 129 for (Iterator it = localPatterns.iterator(); it.hasNext();) { 130 StringPattern pattern = (StringPattern)it.next(); 131 if (pattern.doesMatch(noneCvsFile)) { 132 return true; 133 } 134 } 135 } 136 return false; 137 } 138 139 143 public static List parseCvsIgnoreFile(File cvsIgnoreFile) 144 throws IOException, FileNotFoundException { 145 BufferedReader reader = null; 146 List toReturn = new LinkedList(); 147 try { 148 reader = new BufferedReader(new FileReader(cvsIgnoreFile)); 149 String line; 150 while ((line = reader.readLine()) != null) { 151 StringTokenizer token = new StringTokenizer(line, " ", false); while (token.hasMoreTokens()) { 153 String tok = token.nextToken(); 154 toReturn.add(tok); 155 } 156 } 157 } 158 finally { 159 if (reader != null) { 160 reader.close(); 161 } 162 } 163 return toReturn; 164 } 165 } 166 | Popular Tags |