Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5d42f4feb0a5f9264e30bf313023340755210586 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.search.internal.core.text;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceProxy;
import org.eclipse.core.runtime.IAdaptable;

import org.eclipse.ui.IWorkingSet;

import org.eclipse.search.internal.core.SearchScope;
import org.eclipse.search.internal.ui.SearchMessages;

/**
 * A special text search scope that take file extensions into account.
 */
public class TextSearchScope extends SearchScope {
	
	private static class WorkspaceScope extends TextSearchScope {
	
		private WorkspaceScope() {
			super(SearchMessages.getString("WorkspaceScope")); //$NON-NLS-1$
		}
		
		public void add(IResource element) {
			// do nothing
		}
		
		public boolean encloses(IResourceProxy proxy) {
			if (proxy.getType() == IResource.FILE && skipFile(proxy))
				return false;
			return true;	
		}	
	}
	
	private Set fExtensions= new HashSet(3);

	/**
	 * Returns a new Workbench scope.
	 */
	public static TextSearchScope newWorkspaceScope() {
		return new WorkspaceScope();
	}

	public TextSearchScope(String description) {
		super(description);
	}

	public TextSearchScope(String description, IResource[] resources) {
		super(description, resources);
	}

	public TextSearchScope(String description, IAdaptable[] elements) {
		super(description, convertToResources(elements));

	}

	public TextSearchScope(String description, IWorkingSet[] workingSets) {
		super(description, convertToResources(getElements(workingSets)));
	}
	
	private static IResource[] convertToResources(IAdaptable[] elements) {
		int length= elements.length;
		Set resources= new HashSet(length);
		for (int i= 0; i < length; i++) {
			IResource resource= (IResource)elements[i].getAdapter(IResource.class);
			if (resource != null)
				resources.add(resource);
		}
		return (IResource[])resources.toArray(new IResource[resources.size()]);
	}

	private static IAdaptable[] getElements(IWorkingSet[] workingSets) {
		int length= workingSets.length;
		Set elements= new HashSet(length);
		for (int i= 0; i < length; i++) {
			elements.addAll(Arrays.asList(workingSets[i].getElements()));
		}
		return (IAdaptable[])elements.toArray(new IAdaptable[elements.size()]);
	}
	
	/**
	 * Adds an extension to the scope.
	 */
	public void addExtension(String extension) {
		Pattern pattern= Pattern.compile(asRegEx(extension), Pattern.CASE_INSENSITIVE);
		fExtensions.add(pattern.matcher("")); //$NON-NLS-1$
	}

	/*
	 * Converts '*' and '?' to regEx variables.
	 */
	private String asRegEx(String pattern) {
		
		StringBuffer out= new StringBuffer(pattern.length());
		
		boolean escaped= false;
		boolean quoting= false;
	
		int i= 0;
		while (i < pattern.length()) {
			char ch= pattern.charAt(i++);
	
			if (ch == '*' && !escaped) {
				if (quoting) {
					out.append("\\E"); //$NON-NLS-1$
					quoting= false;
				}
				out.append(".*"); //$NON-NLS-1$
				escaped= false;
				continue;
			} else if (ch == '?' && !escaped) {
				if (quoting) {
					out.append("\\E"); //$NON-NLS-1$
					quoting= false;
				}
				out.append("."); //$NON-NLS-1$
				escaped= false;
				continue;
			} else if (ch == '\\' && !escaped) {
				escaped= true;
				continue;								
	
			} else if (ch == '\\' && escaped) {
				escaped= false;
				if (quoting) {
					out.append("\\E"); //$NON-NLS-1$
					quoting= false;
				}
				out.append("\\\\"); //$NON-NLS-1$
				continue;								
			}
	
			if (!quoting) {
				out.append("\\Q"); //$NON-NLS-1$
				quoting= true;
			}
			if (escaped && ch != '*' && ch != '?' && ch != '\\')
				out.append('\\');
			out.append(ch);
			escaped= ch == '\\';
	
		}
		if (quoting)
			out.append("\\E"); //$NON-NLS-1$
		
		return out.toString();
	}

	/**
	 * Adds all string patterns contained in <code>extensions</code> to this
	 * scope. The allowed pattern characters are <code>*</code> for any character
	 * and <code>?</code> for one character.
	 */
	public void addExtensions(Set extensions) {
		if (extensions == null)
			return;
		Iterator iter= extensions.iterator();
		while (iter.hasNext()) {
			Object obj= iter.next();
			if (obj instanceof String)
				addExtension((String)obj);
		}
	}  

	/*
	 * Implements method from ISearchScope
	 */
	public boolean encloses(IResourceProxy proxy) {
		if (proxy.getType() == IResource.FILE && skipFile(proxy))
			return false;
		return super.encloses(proxy);	
	}

	boolean skipFile(IResourceProxy proxy) {
		if (proxy != null) {
			Iterator iter= fExtensions.iterator();
			while (iter.hasNext()) {
				if (((Matcher)iter.next()).reset(proxy.getName()).matches())
					return false;
			}
		}
		return true;
	}

	/**
	 * Implements method from ISearchScope
	 * 
	 * @deprecated As of 2.1, replaced by {@link #encloses(IResourceProxy)}
	 */
	public boolean encloses(IResource element) {
		if (element.getType() == IResource.FILE && skipFile((IFile)element))
			return false;
		return super.encloses(element);	
	}

	boolean skipFile(IFile file) {
		if (file != null) {
			Iterator iter= fExtensions.iterator();
			while (iter.hasNext()) {
				if (((Matcher)iter.next()).reset(file.getName()).matches())
					return false;
			}
		}
		return true;
	}
}

Back to the top