Skip to main content
summaryrefslogtreecommitdiffstats
blob: a169e91b97d4ad7e4c1101a0a2c421228356f48c (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
/*******************************************************************************
 * Copyright (c) 2004, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - Initial API and implementation
 *******************************************************************************/

package org.eclipse.wst.common.core.search.internal;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.core.expressions.EvaluationContext;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.wst.common.core.search.SearchParticipant;
import org.eclipse.wst.common.core.search.pattern.SearchPattern;

public class SearchParticipantRegistry
{

	protected Map idMap = new HashMap(); // maps searchParticipant id to a
											// searchParticipant descriptor

	public SearchParticipantRegistry()
	{
	}

	public void putSearchParticipant(String id,
			SearchParticipantDescriptor searchParticipantDescriptor)
	{
		idMap.put(id, searchParticipantDescriptor);
	}

	public String[] getSearchParticipantIds()
	{
		Set ids = idMap.keySet();
		return (String[]) ids.toArray(new String[ids.size()]);
	}

	public Collection getSearchParticipants()
	{
		return idMap.values();
	}


	public SearchParticipant getSearchParticipant(String id)
	{
		SearchParticipantDescriptor searchParticipantDescriptor = null;
		if (id != null)
		{
			searchParticipantDescriptor = (SearchParticipantDescriptor) idMap
					.get(id);
		}
		return searchParticipantDescriptor != null ? searchParticipantDescriptor
				.getSearchParticipant()
				: null;

	}

	public SearchParticipant[] getParticipants(SearchPattern pattern, Map searchOptions)
	{

		EvaluationContext evalContext = createEvaluationContext(pattern);
		List result = new ArrayList();
		for (Iterator iter = getSearchParticipants().iterator(); iter.hasNext();)
		{
			SearchParticipantDescriptor descriptor = (SearchParticipantDescriptor) iter
					.next();
			try
			{
				if (descriptor.matches(evalContext))
				{
					try
					{
						SearchParticipant participant = descriptor
								.getSearchParticipant();
						if (!SearchParticipant.class.isInstance(participant))
							throw new ClassCastException();
						if (participant.isApplicable(pattern, searchOptions))
						{
							result.add(participant);
						}
					} catch (ClassCastException e)
					{
						iter.remove();
					}
				}

			} catch (CoreException e)
			{
				iter.remove();
			}

		}

		return (SearchParticipant[]) result
				.toArray(new SearchParticipant[result.size()]);
	}

	private static EvaluationContext createEvaluationContext(
			SearchPattern pattern)
	{
		EvaluationContext result = new EvaluationContext(null, pattern);
		result.addVariable("pattern", pattern); //$NON-NLS-1$
		return result;
	}

}

Back to the top