KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > ui > text > FileTextSearchScope


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
12 package org.eclipse.search.ui.text;
13
14 import java.io.File JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Arrays JavaDoc;
17 import java.util.regex.Matcher JavaDoc;
18 import java.util.regex.Pattern JavaDoc;
19
20 import org.eclipse.core.runtime.IAdaptable;
21 import org.eclipse.core.runtime.IPath;
22 import org.eclipse.core.runtime.content.IContentType;
23
24 import org.eclipse.core.resources.IResource;
25 import org.eclipse.core.resources.IResourceProxy;
26 import org.eclipse.core.resources.ResourcesPlugin;
27
28 import org.eclipse.ui.IWorkingSet;
29
30 import org.eclipse.search.core.text.TextSearchScope;
31
32 import org.eclipse.search.internal.core.text.PatternConstructor;
33 import org.eclipse.search.internal.ui.Messages;
34 import org.eclipse.search.internal.ui.SearchMessages;
35 import org.eclipse.search.internal.ui.WorkingSetComparator;
36 import org.eclipse.search.internal.ui.util.FileTypeEditor;
37
38 /**
39  * A text search scope used by the file search dialog. Additionally to roots it allows to define file name
40  * patterns and exclude all derived resources.
41  *
42  * <p>
43  * Clients should not instantiate or subclass this class.
44  * </p>
45  * @since 3.2
46  */

47 public final class FileTextSearchScope extends TextSearchScope {
48     
49     private static final boolean IS_CASE_SENSITIVE_FILESYSTEM = !new File JavaDoc("Temp").equals(new File JavaDoc("temp")); //$NON-NLS-1$ //$NON-NLS-2$
50

51     /**
52      * Returns a scope for the workspace. The created scope contains all resources in the workspace
53      * that match the given file name patterns. Depending on <code>includeDerived</code>, derived resources or
54      * resources inside a derived container are part of the scope or not.
55      *
56      * @param fileNamePatterns file name pattern that all files have to match <code>null</code> to include all file names.
57      * @param includeDerived defines if derived files and files inside derived containers are included in the scope.
58      * @return a scope containing all files in the workspace that match the given file name patterns.
59      */

60     public static FileTextSearchScope newWorkspaceScope(String JavaDoc[] fileNamePatterns, boolean includeDerived) {
61         return new FileTextSearchScope(SearchMessages.WorkspaceScope, new IResource[] { ResourcesPlugin.getWorkspace().getRoot() }, null, fileNamePatterns, includeDerived);
62     }
63     
64     /**
65      * Returns a scope for the given root resources. The created scope contains all root resources and their
66      * children that match the given file name patterns. Depending on <code>includeDerived</code>, derived resources or
67      * resources inside a derived container are part of the scope or not.
68      *
69      * @param roots the roots resources defining the scope.
70      * @param fileNamePatterns file name pattern that all files have to match <code>null</code> to include all file names.
71      * @param includeDerived defines if derived files and files inside derived containers are included in the scope.
72      * @return a scope containing the resources and its children if they match the given file name patterns.
73      */

74     public static FileTextSearchScope newSearchScope(IResource[] roots, String JavaDoc[] fileNamePatterns, boolean includeDerived) {
75         roots= removeRedundantEntries(roots, includeDerived);
76         
77         String JavaDoc description;
78         if (roots.length == 0) {
79             description= SearchMessages.FileTextSearchScope_scope_empty;
80         } else if (roots.length == 1) {
81             String JavaDoc label= SearchMessages.FileTextSearchScope_scope_single;
82             description= Messages.format(label, roots[0].getName());
83         } else if (roots.length == 2) {
84             String JavaDoc label= SearchMessages.FileTextSearchScope_scope_double;
85             description= Messages.format(label, new String JavaDoc[] { roots[0].getName(), roots[1].getName()});
86         } else {
87             String JavaDoc label= SearchMessages.FileTextSearchScope_scope_multiple;
88             description= Messages.format(label, new String JavaDoc[] { roots[0].getName(), roots[1].getName()});
89         }
90         return new FileTextSearchScope(description, roots, null, fileNamePatterns, includeDerived);
91     }
92
93     /**
94      * Returns a scope for the given working sets. The created scope contains all resources in the
95      * working sets that match the given file name patterns. Depending on <code>includeDerived</code>, derived resources or
96      * resources inside a derived container are part of the scope or not.
97      *
98      * @param workingSets the working sets defining the scope.
99      * @param fileNamePatterns file name pattern that all files have to match <code>null</code> to include all file names.
100      * @param includeDerived defines if derived files and files inside derived containers are included in the scope.
101      * @return a scope containing the resources in the working set if they match the given file name patterns.
102      */

103     public static FileTextSearchScope newSearchScope(IWorkingSet[] workingSets, String JavaDoc[] fileNamePatterns, boolean includeDerived) {
104         String JavaDoc description;
105         Arrays.sort(workingSets, new WorkingSetComparator());
106         if (workingSets.length == 0) {
107             description= SearchMessages.FileTextSearchScope_ws_scope_empty;
108         } else if (workingSets.length == 1) {
109             String JavaDoc label= SearchMessages.FileTextSearchScope_ws_scope_single;
110             description= Messages.format(label, workingSets[0].getLabel());
111         } else if (workingSets.length == 2) {
112             String JavaDoc label= SearchMessages.FileTextSearchScope_ws_scope_double;
113             description= Messages.format(label, new String JavaDoc[] { workingSets[0].getLabel(), workingSets[1].getLabel()});
114         } else {
115             String JavaDoc label= SearchMessages.FileTextSearchScope_ws_scope_multiple;
116             description= Messages.format(label, new String JavaDoc[] { workingSets[0].getLabel(), workingSets[1].getLabel()});
117         }
118         FileTextSearchScope scope= new FileTextSearchScope(description, convertToResources(workingSets, includeDerived), workingSets, fileNamePatterns, includeDerived);
119         return scope;
120     }
121
122     private final String JavaDoc fDescription;
123     private final IResource[] fRootElements;
124     private final String JavaDoc[] fFileNamePatterns;
125     private final Matcher JavaDoc fPositiveFileNameMatcher;
126     private final Matcher JavaDoc fNegativeFileNameMatcher;
127     
128     private boolean fVisitDerived;
129     private IWorkingSet[] fWorkingSets;
130
131     private FileTextSearchScope(String JavaDoc description, IResource[] resources, IWorkingSet[] workingSets, String JavaDoc[] fileNamePatterns, boolean visitDerived) {
132         fDescription= description;
133         fRootElements= resources;
134         fFileNamePatterns= fileNamePatterns;
135         fVisitDerived= visitDerived;
136         fWorkingSets= workingSets;
137         fPositiveFileNameMatcher= createMatcher(fileNamePatterns, false);
138         fNegativeFileNameMatcher= createMatcher(fileNamePatterns, true);
139     }
140     
141     /**
142      * Returns the description of the scope
143      *
144      * @return the description of the scope
145      */

146     public String JavaDoc getDescription() {
147         return fDescription;
148     }
149
150     /**
151      * Returns the file name pattern configured for this scope or <code>null</code> to match
152      * all file names.
153      *
154      * @return the file name pattern starings
155      */

156     public String JavaDoc[] getFileNamePatterns() {
157         return fFileNamePatterns;
158     }
159     
160     /**
161      * Returns the working-sets that were used to configure this scope or <code>null</code>
162      * if the scope was not created off working sets.
163      *
164      * @return the working-sets the scope is based on.
165      */

166     public IWorkingSet[] getWorkingSets() {
167         return fWorkingSets;
168     }
169
170     /**
171      * Returns the content types configured for this scope or <code>null</code> to match
172      * all content types.
173      *
174      * @return the file name pattern starings
175      */

176     public IContentType[] getContentTypes() {
177         return null; // to be implemented in the future
178
}
179     
180     /**
181      * Returns a description describing the file name patterns and content types.
182      *
183      * @return the description of the scope
184      */

185     public String JavaDoc getFilterDescription() {
186         String JavaDoc[] ext= fFileNamePatterns;
187         if (ext == null) {
188             return "*"; //$NON-NLS-1$
189
}
190         Arrays.sort(ext);
191         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
192         for (int i= 0; i < ext.length; i++) {
193             if (i > 0) {
194                 buf.append(", "); //$NON-NLS-1$
195
}
196             buf.append(ext[i]);
197         }
198         return buf.toString();
199     }
200     
201     /**
202      * Returns whether derived resources are included in this search scope.
203      *
204      * @return whether derived resources are included in this search scope.
205      */

206     public boolean includeDerived() {
207         return fVisitDerived;
208     }
209     
210     /* (non-Javadoc)
211      * @see org.eclipse.search.core.text.FileSearchScope#getRoots()
212      */

213     public IResource[] getRoots() {
214         return fRootElements;
215     }
216
217     /* (non-Javadoc)
218      * @see org.eclipse.search.core.text.FileSearchScope#contains(org.eclipse.core.resources.IResourceProxy)
219      */

220     public boolean contains(IResourceProxy proxy) {
221         if (!fVisitDerived && proxy.isDerived()) {
222             return false; // all resources in a derived folder are considered to be derived, see bug 103576
223
}
224         
225         if (proxy.getType() == IResource.FILE) {
226             return matchesFileName(proxy.getName());
227         }
228         return true;
229     }
230     
231     private boolean matchesFileName(String JavaDoc fileName) {
232         if (fPositiveFileNameMatcher != null && !fPositiveFileNameMatcher.reset(fileName).matches()) {
233             return false;
234         }
235         if (fNegativeFileNameMatcher != null && fNegativeFileNameMatcher.reset(fileName).matches()) {
236             return false;
237         }
238         return true;
239     }
240     
241     private Matcher JavaDoc createMatcher(String JavaDoc[] fileNamePatterns, boolean negativeMatcher) {
242         if (fileNamePatterns == null || fileNamePatterns.length == 0) {
243             return null;
244         }
245         ArrayList JavaDoc patterns= new ArrayList JavaDoc();
246         for (int i= 0; i < fileNamePatterns.length; i++) {
247             String JavaDoc pattern= fFileNamePatterns[i];
248             if (negativeMatcher == pattern.startsWith(FileTypeEditor.FILE_PATTERN_NEGATOR)) {
249                 if (negativeMatcher) {
250                     pattern= pattern.substring(FileTypeEditor.FILE_PATTERN_NEGATOR.length()).trim();
251                 }
252                 if (pattern.length() > 0) {
253                     patterns.add(pattern);
254                 }
255             }
256         }
257         if (!patterns.isEmpty()) {
258             String JavaDoc[] patternArray= (String JavaDoc[]) patterns.toArray(new String JavaDoc[patterns.size()]);
259             Pattern JavaDoc pattern= PatternConstructor.createPattern(patternArray, IS_CASE_SENSITIVE_FILESYSTEM);
260             return pattern.matcher(""); //$NON-NLS-1$
261
}
262         return null;
263     }
264     
265     private static IResource[] removeRedundantEntries(IResource[] elements, boolean includeDerived) {
266         ArrayList JavaDoc res= new ArrayList JavaDoc();
267         for (int i= 0; i < elements.length; i++) {
268             IResource curr= elements[i];
269             addToList(res, curr, includeDerived);
270         }
271         return (IResource[])res.toArray(new IResource[res.size()]);
272     }
273
274     private static IResource[] convertToResources(IWorkingSet[] workingSets, boolean includeDerived) {
275         ArrayList JavaDoc res= new ArrayList JavaDoc();
276         for (int i= 0; i < workingSets.length; i++) {
277             IWorkingSet workingSet= workingSets[i];
278             if (workingSet.isAggregateWorkingSet() && workingSet.isEmpty()) {
279                 return new IResource[] { ResourcesPlugin.getWorkspace().getRoot() };
280             }
281             IAdaptable[] elements= workingSet.getElements();
282             for (int k= 0; k < elements.length; k++) {
283                 IResource curr= (IResource) elements[k].getAdapter(IResource.class);
284                 if (curr != null) {
285                     addToList(res, curr, includeDerived);
286                 }
287             }
288         }
289         return (IResource[]) res.toArray(new IResource[res.size()]);
290     }
291     
292     private static void addToList(ArrayList JavaDoc res, IResource curr, boolean includeDerived) {
293         if (!includeDerived && isDerived(curr)) {
294             return;
295         }
296         IPath currPath= curr.getFullPath();
297         for (int k= res.size() - 1; k >= 0 ; k--) {
298             IResource other= (IResource) res.get(k);
299             IPath otherPath= other.getFullPath();
300             if (otherPath.isPrefixOf(currPath)) {
301                 return;
302             }
303             if (currPath.isPrefixOf(otherPath)) {
304                 res.remove(k);
305             }
306         }
307         res.add(curr);
308     }
309
310     private static boolean isDerived(IResource curr) {
311         do {
312             if (curr.isDerived()) {
313                 return true;
314             }
315             curr= curr.getParent();
316         } while (curr != null);
317         return false;
318     }
319 }
320
Popular Tags