KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > internal > core > text > FileNamePatternSearchScope


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.search.internal.core.text;
12
13 import java.io.File JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Set JavaDoc;
18 import java.util.regex.Matcher JavaDoc;
19 import java.util.regex.Pattern JavaDoc;
20
21 import org.eclipse.core.runtime.Assert;
22 import org.eclipse.core.runtime.IPath;
23
24 import org.eclipse.core.resources.IResource;
25 import org.eclipse.core.resources.IResourceProxy;
26
27
28 import org.eclipse.search.core.text.TextSearchScope;
29
30 public class FileNamePatternSearchScope extends TextSearchScope {
31
32     /**
33      * Returns a scope for the given resources.
34      * @param description description of the scope
35      * @param resources the resources to be contained
36      * @return a scope for the given resources.
37      */

38     public static FileNamePatternSearchScope newSearchScope(String JavaDoc description, IResource[] resources, boolean includeDerived) {
39         return new FileNamePatternSearchScope(description, removeRedundantEntries(resources, includeDerived), includeDerived);
40     }
41     
42     private static final boolean IS_CASE_SENSITIVE_FILESYSTEM = !new File JavaDoc("Temp").equals(new File JavaDoc("temp")); //$NON-NLS-1$ //$NON-NLS-2$
43

44     private final String JavaDoc fDescription;
45     private final IResource[] fRootElements;
46     
47     private final Set JavaDoc fFileNamePatterns;
48     private Matcher JavaDoc fFileNameMatcher;
49     
50     private boolean fVisitDerived;
51
52     private FileNamePatternSearchScope(String JavaDoc description, IResource[] resources, boolean visitDerived) {
53         Assert.isNotNull(description);
54         fDescription= description;
55         fRootElements= resources;
56         fFileNamePatterns= new HashSet JavaDoc(3);
57         fFileNameMatcher= null;
58         fVisitDerived= visitDerived;
59     }
60     
61     /**
62      * Returns the description of the scope
63      * @return the description of the scope
64      */

65     public String JavaDoc getDescription() {
66         return fDescription;
67     }
68     
69     /* (non-Javadoc)
70      * @see org.eclipse.search.core.text.FileSearchScope#getRoots()
71      */

72     public IResource[] getRoots() {
73         return fRootElements;
74     }
75
76     /* (non-Javadoc)
77      * @see org.eclipse.search.core.text.FileSearchScope#contains(org.eclipse.core.resources.IResourceProxy)
78      */

79     public boolean contains(IResourceProxy proxy) {
80         if (!fVisitDerived && proxy.isDerived()) {
81             return false; // all resources in a derived folder are considered to be derived, see bug 103576
82
}
83         
84         if (proxy.getType() == IResource.FILE) {
85             return matchesFileName(proxy.getName());
86         }
87         return true;
88     }
89         
90     /**
91      * Adds an file name pattern to the scope.
92      * @param pattern
93      */

94     public void addFileNamePattern(String JavaDoc pattern) {
95         if (fFileNamePatterns.add(pattern)) {
96             fFileNameMatcher= null; // clear cache
97
}
98     }
99     
100     public void setFileNamePattern(Pattern JavaDoc pattern) {
101         fFileNameMatcher= pattern.matcher(""); //$NON-NLS-1$
102
}
103     
104     
105     public Pattern JavaDoc getFileNamePattern() {
106         return getFileNameMatcher().pattern();
107     }
108         
109     /**
110      * Returns if derived resources are included in the scope.
111      *
112      * @return if set derived resources are included in the scope.
113      */

114     public boolean isIncludeDerived() {
115         return fVisitDerived;
116     }
117     
118
119     private Matcher JavaDoc getFileNameMatcher() {
120         if (fFileNameMatcher == null) {
121             Pattern JavaDoc pattern;
122             if (fFileNamePatterns.isEmpty()) {
123                 pattern= Pattern.compile(".*"); //$NON-NLS-1$
124
} else {
125                 String JavaDoc[] patternStrings= (String JavaDoc[]) fFileNamePatterns.toArray(new String JavaDoc[fFileNamePatterns.size()]);
126                 pattern= PatternConstructor.createPattern(patternStrings, IS_CASE_SENSITIVE_FILESYSTEM);
127             }
128             fFileNameMatcher= pattern.matcher(""); //$NON-NLS-1$
129
}
130         return fFileNameMatcher;
131     }
132     
133     /**
134      * Tests if a file name matches to the file name patterns contained in the scope
135      * @param fileName The file name to test
136      * @return returns true if the file name is matching to a file name pattern
137      */

138     private boolean matchesFileName(String JavaDoc fileName) {
139         return getFileNameMatcher().reset(fileName).matches();
140     }
141         
142     /**
143      * Returns a description for the file name patterns in the scope
144      * @return the description of the scope
145      */

146     public String JavaDoc getFileNamePatternDescription() {
147         String JavaDoc[] ext= (String JavaDoc[]) fFileNamePatterns.toArray(new String JavaDoc[fFileNamePatterns.size()]);
148         Arrays.sort(ext);
149         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
150         for (int i= 0; i < ext.length; i++) {
151             if (i > 0) {
152                 buf.append(", "); //$NON-NLS-1$
153
}
154             buf.append(ext[i]);
155         }
156         return buf.toString();
157     }
158
159     
160     private static IResource[] removeRedundantEntries(IResource[] elements, boolean includeDerived) {
161         ArrayList JavaDoc res= new ArrayList JavaDoc();
162         for (int i= 0; i < elements.length; i++) {
163             IResource curr= elements[i];
164             addToList(res, curr, includeDerived);
165         }
166         return (IResource[])res.toArray(new IResource[res.size()]);
167     }
168     
169     private static void addToList(ArrayList JavaDoc res, IResource curr, boolean includeDerived) {
170         if (!includeDerived && isDerived(curr)) {
171             return;
172         }
173         IPath currPath= curr.getFullPath();
174         for (int k= res.size() - 1; k >= 0 ; k--) {
175             IResource other= (IResource) res.get(k);
176             IPath otherPath= other.getFullPath();
177             if (otherPath.isPrefixOf(currPath)) {
178                 return;
179             }
180             if (currPath.isPrefixOf(otherPath)) {
181                 res.remove(k);
182             }
183         }
184         res.add(curr);
185     }
186
187     private static boolean isDerived(IResource curr) {
188         do {
189             if (curr.isDerived()) {
190                 return true;
191             }
192             curr= curr.getParent();
193         } while (curr != null);
194         return false;
195     }
196
197
198 }
199
Popular Tags