KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > util > DefaultIgnoreFileFilter


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.lib.cvsclient.util;
20
21 import java.io.*;
22 import java.util.*;
23
24 /**
25  * @author Milos Kleint, Thomas Singer
26  */

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     /**
39      * Creates new DefaultIgnoreFileFilter and fills in patterns.
40      * @param patternList - list of objects, patterns are retrieved
41      * via the Object.toString() method.
42      */

43     public DefaultIgnoreFileFilter(List patternList) {
44         for (Iterator it = patternList.iterator(); it.hasNext();) {
45             String JavaDoc patternString = it.next().toString();
46             SimpleStringPattern pattern = new SimpleStringPattern(patternString);
47             addPattern(pattern);
48         }
49     }
50
51     /**
52      * Adds a StringPattern to the list of ignore file patters.
53      */

54     public void addPattern(StringPattern pattern) {
55         if (pattern.toString().equals("!")) { //NOI18N
56
clearPatterns();
57         }
58         else {
59             patterns.add(pattern);
60         }
61     }
62
63     /**
64      * Adds a string to the list of ignore file patters using the SimpleStringPattern.
65      */

66     public void addPattern(String JavaDoc pattern) {
67         if (pattern.equals("!")) { //NOI18N
68
clearPatterns();
69         }
70         else {
71             patterns.add(new SimpleStringPattern(pattern));
72         }
73     }
74
75     /**
76      * Clears the list of patters.
77      * To be used when the "!" character is used in any of the .cvsignore lists.
78      */

79     public void clearPatterns() {
80         patterns.clear();
81     }
82
83     /**
84      * A file is checked against the patterns in the filter.
85      * If any of these matches, the file should be ignored. A file will also
86      * be ignored, if its name matches any local <code>.cvsignore</code> file
87      * entry.
88      * @param directory is a file object that refers to the directory the file resides in.
89      * @param noneCvsFile is the name of the file to be checked.
90      */

91     public boolean shouldBeIgnored(File directory, String JavaDoc noneCvsFile) {
92         // current implementation ignores the directory parameter.
93
// in future or different implementations can add the directory dependant .cvsignore lists
94
if (lastDirectory != directory) {
95             lastDirectory = directory;
96             processGlobalPatterns = true;
97             processLocalPatterns = false;
98             localPatterns.clear();
99             String JavaDoc filename = directory.getPath() + File.separator + ".cvsignore"; //NOI18N
100
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 JavaDoc s = it.next().toString();
106                         if (s.equals("!")) { //NOI18N
107
processGlobalPatterns = false;
108                             localPatterns.clear();
109                         } else {
110                             localPatterns.add(new SimpleStringPattern(s));
111                         }
112                     }
113                 }
114                 catch (IOException ex) {
115                     // ignore exception
116
}
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     /**
140      * Utility method that reads the .cvsignore file and returns a list of Strings.
141      * These strings represent the patterns read from the file.
142      */

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 JavaDoc line;
150             while ((line = reader.readLine()) != null) {
151                 StringTokenizer token = new StringTokenizer(line, " ", false); //NOI18N
152
while (token.hasMoreTokens()) {
153                     String JavaDoc 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