001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.lib.cvsclient.util;
042:
043: import java.io.*;
044: import java.util.*;
045:
046: /**
047: * @author Milos Kleint, Thomas Singer
048: */
049: public class DefaultIgnoreFileFilter implements IgnoreFileFilter {
050: private final List patterns = new LinkedList();
051:
052: private final List localPatterns = new LinkedList();
053: private boolean processGlobalPatterns = true;
054: private boolean processLocalPatterns = false;
055: private File lastDirectory = null;
056:
057: public DefaultIgnoreFileFilter() {
058: }
059:
060: /**
061: * Creates new DefaultIgnoreFileFilter and fills in patterns.
062: * @param patternList - list of objects, patterns are retrieved
063: * via the Object.toString() method.
064: */
065: public DefaultIgnoreFileFilter(List patternList) {
066: for (Iterator it = patternList.iterator(); it.hasNext();) {
067: String patternString = it.next().toString();
068: SimpleStringPattern pattern = new SimpleStringPattern(
069: patternString);
070: addPattern(pattern);
071: }
072: }
073:
074: /**
075: * Adds a StringPattern to the list of ignore file patters.
076: */
077: public void addPattern(StringPattern pattern) {
078: if (pattern.toString().equals("!")) { //NOI18N
079: clearPatterns();
080: } else {
081: patterns.add(pattern);
082: }
083: }
084:
085: /**
086: * Adds a string to the list of ignore file patters using the SimpleStringPattern.
087: */
088: public void addPattern(String pattern) {
089: if (pattern.equals("!")) { //NOI18N
090: clearPatterns();
091: } else {
092: patterns.add(new SimpleStringPattern(pattern));
093: }
094: }
095:
096: /**
097: * Clears the list of patters.
098: * To be used when the "!" character is used in any of the .cvsignore lists.
099: */
100: public void clearPatterns() {
101: patterns.clear();
102: }
103:
104: /**
105: * A file is checked against the patterns in the filter.
106: * If any of these matches, the file should be ignored. A file will also
107: * be ignored, if its name matches any local <code>.cvsignore</code> file
108: * entry.
109: * @param directory is a file object that refers to the directory the file resides in.
110: * @param noneCvsFile is the name of the file to be checked.
111: */
112: public boolean shouldBeIgnored(File directory, String noneCvsFile) {
113: // current implementation ignores the directory parameter.
114: // in future or different implementations can add the directory dependant .cvsignore lists
115: if (lastDirectory != directory) {
116: lastDirectory = directory;
117: processGlobalPatterns = true;
118: processLocalPatterns = false;
119: localPatterns.clear();
120: String filename = directory.getPath() + File.separator
121: + ".cvsignore"; //NOI18N
122: File cvsIgnoreFile = new File(filename);
123: if (cvsIgnoreFile.exists()) {
124: try {
125: List list = parseCvsIgnoreFile(cvsIgnoreFile);
126: for (Iterator it = list.iterator(); it.hasNext();) {
127: String s = it.next().toString();
128: if (s.equals("!")) { //NOI18N
129: processGlobalPatterns = false;
130: localPatterns.clear();
131: } else {
132: localPatterns
133: .add(new SimpleStringPattern(s));
134: }
135: }
136: } catch (IOException ex) {
137: // ignore exception
138: }
139: }
140: processLocalPatterns = localPatterns.size() > 0;
141: }
142: if (processGlobalPatterns) {
143: for (Iterator it = patterns.iterator(); it.hasNext();) {
144: StringPattern pattern = (StringPattern) it.next();
145: if (pattern.doesMatch(noneCvsFile)) {
146: return true;
147: }
148: }
149: }
150: if (processLocalPatterns) {
151: for (Iterator it = localPatterns.iterator(); it.hasNext();) {
152: StringPattern pattern = (StringPattern) it.next();
153: if (pattern.doesMatch(noneCvsFile)) {
154: return true;
155: }
156: }
157: }
158: return false;
159: }
160:
161: /**
162: * Utility method that reads the .cvsignore file and returns a list of Strings.
163: * These strings represent the patterns read from the file.
164: */
165: public static List parseCvsIgnoreFile(File cvsIgnoreFile)
166: throws IOException, FileNotFoundException {
167: BufferedReader reader = null;
168: List toReturn = new LinkedList();
169: try {
170: reader = new BufferedReader(new FileReader(cvsIgnoreFile));
171: String line;
172: while ((line = reader.readLine()) != null) {
173: StringTokenizer token = new StringTokenizer(line, " ",
174: false); //NOI18N
175: while (token.hasMoreTokens()) {
176: String tok = token.nextToken();
177: toReturn.add(tok);
178: }
179: }
180: } finally {
181: if (reader != null) {
182: reader.close();
183: }
184: }
185: return toReturn;
186: }
187: }
|