KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-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.util.Arrays JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Set JavaDoc;
17 import java.util.regex.Matcher JavaDoc;
18 import java.util.regex.Pattern JavaDoc;
19
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.resources.IResourceProxy;
23 import org.eclipse.core.runtime.IAdaptable;
24
25 import org.eclipse.ui.IWorkingSet;
26
27 import org.eclipse.search.internal.core.SearchScope;
28 import org.eclipse.search.internal.ui.SearchMessages;
29
30 /**
31  * A special text search scope that take file extensions into account.
32  */

33 public class TextSearchScope extends SearchScope {
34     
35     private static class WorkspaceScope extends TextSearchScope {
36     
37         private WorkspaceScope() {
38             super(SearchMessages.getString("WorkspaceScope")); //$NON-NLS-1$
39
}
40         
41         public void add(IResource element) {
42             // do nothing
43
}
44         
45         public boolean encloses(IResourceProxy proxy) {
46             if (proxy.getType() == IResource.FILE && skipFile(proxy))
47                 return false;
48             return true;
49         }
50     }
51     
52     private Set JavaDoc fExtensions= new HashSet JavaDoc(3);
53
54     /**
55      * Returns a new Workbench scope.
56      */

57     public static TextSearchScope newWorkspaceScope() {
58         return new WorkspaceScope();
59     }
60
61     public TextSearchScope(String JavaDoc description) {
62         super(description);
63     }
64
65     public TextSearchScope(String JavaDoc description, IResource[] resources) {
66         super(description, resources);
67     }
68
69     public TextSearchScope(String JavaDoc description, IAdaptable[] elements) {
70         super(description, convertToResources(elements));
71
72     }
73
74     public TextSearchScope(String JavaDoc description, IWorkingSet[] workingSets) {
75         super(description, convertToResources(getElements(workingSets)));
76     }
77     
78     private static IResource[] convertToResources(IAdaptable[] elements) {
79         int length= elements.length;
80         Set JavaDoc resources= new HashSet JavaDoc(length);
81         for (int i= 0; i < length; i++) {
82             IResource resource= (IResource)elements[i].getAdapter(IResource.class);
83             if (resource != null)
84                 resources.add(resource);
85         }
86         return (IResource[])resources.toArray(new IResource[resources.size()]);
87     }
88
89     private static IAdaptable[] getElements(IWorkingSet[] workingSets) {
90         int length= workingSets.length;
91         Set JavaDoc elements= new HashSet JavaDoc(length);
92         for (int i= 0; i < length; i++) {
93             elements.addAll(Arrays.asList(workingSets[i].getElements()));
94         }
95         return (IAdaptable[])elements.toArray(new IAdaptable[elements.size()]);
96     }
97     
98     /**
99      * Adds an extension to the scope.
100      */

101     public void addExtension(String JavaDoc extension) {
102         Pattern JavaDoc pattern= Pattern.compile(asRegEx(extension), Pattern.CASE_INSENSITIVE);
103         fExtensions.add(pattern.matcher("")); //$NON-NLS-1$
104
}
105
106     /*
107      * Converts '*' and '?' to regEx variables.
108      */

109     private String JavaDoc asRegEx(String JavaDoc pattern) {
110         
111         StringBuffer JavaDoc out= new StringBuffer JavaDoc(pattern.length());
112         
113         boolean escaped= false;
114         boolean quoting= false;
115     
116         int i= 0;
117         while (i < pattern.length()) {
118             char ch= pattern.charAt(i++);
119     
120             if (ch == '*' && !escaped) {
121                 if (quoting) {
122                     out.append("\\E"); //$NON-NLS-1$
123
quoting= false;
124                 }
125                 out.append(".*"); //$NON-NLS-1$
126
escaped= false;
127                 continue;
128             } else if (ch == '?' && !escaped) {
129                 if (quoting) {
130                     out.append("\\E"); //$NON-NLS-1$
131
quoting= false;
132                 }
133                 out.append("."); //$NON-NLS-1$
134
escaped= false;
135                 continue;
136             } else if (ch == '\\' && !escaped) {
137                 escaped= true;
138                 continue;
139     
140             } else if (ch == '\\' && escaped) {
141                 escaped= false;
142                 if (quoting) {
143                     out.append("\\E"); //$NON-NLS-1$
144
quoting= false;
145                 }
146                 out.append("\\\\"); //$NON-NLS-1$
147
continue;
148             }
149     
150             if (!quoting) {
151                 out.append("\\Q"); //$NON-NLS-1$
152
quoting= true;
153             }
154             if (escaped && ch != '*' && ch != '?' && ch != '\\')
155                 out.append('\\');
156             out.append(ch);
157             escaped= ch == '\\';
158     
159         }
160         if (quoting)
161             out.append("\\E"); //$NON-NLS-1$
162

163         return out.toString();
164     }
165
166     /**
167      * Adds all string patterns contained in <code>extensions</code> to this
168      * scope. The allowed pattern characters are <code>*</code> for any character
169      * and <code>?</code> for one character.
170      */

171     public void addExtensions(Set JavaDoc extensions) {
172         if (extensions == null)
173             return;
174         Iterator JavaDoc iter= extensions.iterator();
175         while (iter.hasNext()) {
176             Object JavaDoc obj= iter.next();
177             if (obj instanceof String JavaDoc)
178                 addExtension((String JavaDoc)obj);
179         }
180     }
181
182     /*
183      * Implements method from ISearchScope
184      */

185     public boolean encloses(IResourceProxy proxy) {
186         if (proxy.getType() == IResource.FILE && skipFile(proxy))
187             return false;
188         return super.encloses(proxy);
189     }
190
191     boolean skipFile(IResourceProxy proxy) {
192         if (proxy != null) {
193             Iterator JavaDoc iter= fExtensions.iterator();
194             while (iter.hasNext()) {
195                 if (((Matcher JavaDoc)iter.next()).reset(proxy.getName()).matches())
196                     return false;
197             }
198         }
199         return true;
200     }
201
202     /**
203      * Implements method from ISearchScope
204      *
205      * @deprecated As of 2.1, replaced by {@link #encloses(IResourceProxy)}
206      */

207     public boolean encloses(IResource element) {
208         if (element.getType() == IResource.FILE && skipFile((IFile)element))
209             return false;
210         return super.encloses(element);
211     }
212
213     boolean skipFile(IFile file) {
214         if (file != null) {
215             Iterator JavaDoc iter= fExtensions.iterator();
216             while (iter.hasNext()) {
217                 if (((Matcher JavaDoc)iter.next()).reset(file.getName()).matches())
218                     return false;
219             }
220         }
221         return true;
222     }
223 }
224
Popular Tags