KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > internal > core > SearchScope


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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;
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.Iterator JavaDoc;
18 import java.util.Set JavaDoc;
19 import java.util.regex.Matcher JavaDoc;
20 import java.util.regex.Pattern JavaDoc;
21
22 import org.eclipse.core.runtime.IAdaptable;
23 import org.eclipse.core.runtime.IPath;
24
25 import org.eclipse.core.resources.IResource;
26 import org.eclipse.core.resources.ResourcesPlugin;
27
28 import org.eclipse.jface.util.Assert;
29
30 import org.eclipse.ui.IWorkingSet;
31
32 import org.eclipse.search.internal.core.text.PatternConstructor;
33 import org.eclipse.search.internal.ui.SearchMessages;
34
35 public class SearchScope {
36
37     /**
38      * Returns a workspace scope.
39      * @return a workspace scope.
40      */

41     public static SearchScope newWorkspaceScope() {
42         return new SearchScope(SearchMessages.WorkspaceScope, new IResource[] { ResourcesPlugin.getWorkspace().getRoot() });
43     }
44     
45     /**
46      * Returns a scope for the given resources.
47      * @param description description of the scope
48      * @param resources the resources to be contained
49      * @return a scope for the given resources.
50      */

51     public static SearchScope newSearchScope(String JavaDoc description, IResource[] resources) {
52         return new SearchScope(description, removeRedundantEntries(resources));
53     }
54
55     /**
56      * Returns a scope for the given working sets
57      * @param description description of the scope
58      * @param workingSets the working sets to be contained
59      * @return a scope for the given working sets
60      */

61     public static SearchScope newSearchScope(String JavaDoc description, IWorkingSet[] workingSets) {
62         return new SearchScope(description, convertToResources(workingSets));
63     }
64     
65     private static final boolean IS_CASE_SENSITIVE_FILESYSTEM = !new File JavaDoc("Temp").equals(new File JavaDoc("temp")); //$NON-NLS-1$ //$NON-NLS-2$
66

67     private String JavaDoc fDescription;
68     private final IResource[] fRootElements;
69     
70     private Set JavaDoc fFileNamePatterns= new HashSet JavaDoc(3);
71     private Matcher JavaDoc[] fFileNameMatchers= null;
72
73     private SearchScope(String JavaDoc description, IResource[] resources) {
74         Assert.isNotNull(description);
75         fDescription= description;
76         fRootElements= resources;
77     }
78     
79     /**
80      * Returns the description of the scope
81      * @return the description of the scope
82      */

83     public String JavaDoc getDescription() {
84         return fDescription;
85     }
86     
87     /**
88      * Returns the root elements of this scope
89      * @return the root elements of this scope
90      */

91     public IResource[] getRootElements() {
92         return fRootElements;
93     }
94         
95     /**
96      * Adds an file name pattern to the scope.
97      * @param pattern
98      */

99     public void addFileNamePattern(String JavaDoc pattern) {
100         if (fFileNamePatterns.add(pattern)) {
101             fFileNameMatchers= null; // clear cache
102
}
103     }
104
105     private Matcher JavaDoc[] getFileNameMatchers() {
106         if (fFileNameMatchers == null) {
107             fFileNameMatchers= new Matcher JavaDoc[fFileNamePatterns.size()];
108             int i= 0;
109             for (Iterator JavaDoc iter= fFileNamePatterns.iterator(); iter.hasNext();) {
110                 String JavaDoc ext= (String JavaDoc) iter.next();
111                 Pattern JavaDoc pattern= PatternConstructor.createPattern(ext, IS_CASE_SENSITIVE_FILESYSTEM, false);
112                 fFileNameMatchers[i++]= pattern.matcher(""); //$NON-NLS-1$
113
}
114         }
115         return fFileNameMatchers;
116     }
117     
118     /**
119      * Tests if a file name matches to the file name patterns contained in the scope
120      * @param fileName The file name to test
121      * @return returns true if the file name is matching to a file name pattern
122      */

123     public boolean matchesFileName(String JavaDoc fileName) {
124         Matcher JavaDoc[] matchers= getFileNameMatchers();
125         for (int i= 0; i < matchers.length; i++) {
126             if (matchers[i].reset(fileName).matches()) {
127                 return true;
128             }
129         }
130         return matchers.length == 0;
131     }
132         
133     /**
134      * Returns a description for the file name patterns in the scope
135      * @return the description of the scope
136      */

137     public String JavaDoc getFileNamePatternDescription() {
138         String JavaDoc[] ext= (String JavaDoc[]) fFileNamePatterns.toArray(new String JavaDoc[fFileNamePatterns.size()]);
139         Arrays.sort(ext);
140         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
141         for (int i= 0; i < ext.length; i++) {
142             if (i > 0) {
143                 buf.append(", "); //$NON-NLS-1$
144
}
145             buf.append(ext[i]);
146         }
147         return buf.toString();
148     }
149
150     
151     private static IResource[] removeRedundantEntries(IResource[] elements) {
152         ArrayList JavaDoc res= new ArrayList JavaDoc();
153         for (int i= 0; i < elements.length; i++) {
154             IResource curr= elements[i];
155             addToList(res, curr);
156         }
157         return (IResource[])res.toArray(new IResource[res.size()]);
158     }
159
160     private static IResource[] convertToResources(IWorkingSet[] workingSets) {
161         ArrayList JavaDoc res= new ArrayList JavaDoc();
162         for (int i= 0; i < workingSets.length; i++) {
163             IAdaptable[] elements= workingSets[i].getElements();
164             for (int k= 0; k < elements.length; k++) {
165                 IResource curr= (IResource) elements[k].getAdapter(IResource.class);
166                 if (curr != null) {
167                     addToList(res, curr);
168                 }
169             }
170         }
171         return (IResource[]) res.toArray(new IResource[res.size()]);
172     }
173     
174     private static void addToList(ArrayList JavaDoc res, IResource curr) {
175         IPath currPath= curr.getFullPath();
176         for (int k= res.size() - 1; k >= 0 ; k--) {
177             IResource other= (IResource) res.get(k);
178             IPath otherPath= other.getFullPath();
179             if (otherPath.isPrefixOf(currPath)) {
180                 return;
181             }
182             if (currPath.isPrefixOf(otherPath)) {
183                 res.remove(k);
184             }
185         }
186         res.add(curr);
187     }
188 }
189
Popular Tags