Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7f4b9968a09e97efbc594f7fdc40dc3c31ed40d0 (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
/*******************************************************************************
 * Copyright (c) 2000, 2005 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.help.internal.search;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.eclipse.core.runtime.*;
import org.eclipse.help.IHelpResource;
import org.eclipse.help.internal.base.HelpBaseResources;
import org.eclipse.help.search.ISearchEngine;
import org.eclipse.help.search.ISearchEngineResult;
import org.eclipse.help.search.ISearchEngineResultCollector;
import org.eclipse.help.search.ISearchScope;

/**
 * This implementation of <code>ISearchEngine</code> interface performs search
 * by running a query on the remote web site using the provided query URL.
 * Instances of this engine type are required to supply the URL template with
 * the query string replaced with the substitution string
 * <code>{expression}</code>.
 * <p>
 * This class is made public in order to be instantiated and parametrized
 * directly in the extensions. Clients are required to supply the URL template
 * string as a parameter <code>url</code>.
 *
 * <p>
 * This class is not expected to be subclassed or otherwise accessed
 * programmatically.
 *
 * @since 3.1
 */
public final class WebSearch implements ISearchEngine {
	private static final char C_START = '{';

	private static final char C_STOP = '}';

	public static class Scope implements ISearchScope {
		private String urlTemplate;

		public Scope(String urlTemplate) {
			this.urlTemplate = urlTemplate;
		}

		public String getURLTemplate() {
			return urlTemplate;
		}
	}

	private static class SearchResult implements ISearchEngineResult {
		private String query;

		private String urlTemplate;

		public SearchResult(String query, String urlTemplate) {
			this.query = query;
			this.urlTemplate = urlTemplate;
		}

		@Override
		public String getDescription() {
			return HelpBaseResources.WebSearch_click;
		}

		@Override
		public String getHref() {
			String href = null;
			String equery;
			try {
				equery = URLEncoder.encode(query, "UTF-8"); //$NON-NLS-1$

			} catch (UnsupportedEncodingException e) {
				equery = query;
			}
			href = composeURL(equery, urlTemplate);
			return href;
		}

		@Override
		public String getLabel() {
			return HelpBaseResources.WebSearch_label;
		}

		@Override
		public float getScore() {
			return 1;
		}

		/*
		 * (non-Javadoc)
		 *
		 * @see org.eclipse.help.internal.search.federated.ISearchEngineResult#getCategory()
		 */
		@Override
		public IHelpResource getCategory() {
			return null;
		}

		@Override
		public boolean getForceExternalWindow() {
			return true;
		}

		@Override
		public String toAbsoluteHref(String href, boolean frames) {
			return href;
		}
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.help.internal.search.federated.ISearchEngine#run(java.lang.String,
	 *      org.eclipse.help.internal.search.ISearchScope,
	 *      org.eclipse.help.internal.search.federated.ISearchEngineResultCollector,
	 *      org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	public void run(String query, ISearchScope scope,
			ISearchEngineResultCollector collector, IProgressMonitor monitor)
			throws CoreException {

		collector
				.accept(new SearchResult(query, ((Scope) scope).getURLTemplate()));
	}

	private static String composeURL(String query, String urlTemplate) {
		StringBuffer result = new StringBuffer();
		boolean inSubstitution = false;
		int varStart = -1;
		for (int i = 0; i < urlTemplate.length(); i++) {
			char c = urlTemplate.charAt(i);
			if (c == C_START && !inSubstitution) {
				if (i < urlTemplate.length() - 1) {
					// look ahead
					char c2 = urlTemplate.charAt(i + 1);
					if (c2 == C_START) {
						result.append(c);
						i++;
						continue;
					}
				}
				inSubstitution = true;
				varStart = i;
				continue;
			} else if (c == C_STOP && inSubstitution) {
				if (i < urlTemplate.length() - 1) {
					// look ahead
					char c2 = urlTemplate.charAt(i + 1);
					if (c2 == C_STOP) {
						result.append(c);
						i++;
						continue;
					}
				}
				if (varStart != -1) {
					String key = urlTemplate.substring(varStart + 1, i);
					String value = getVariable(key, query);
					result.append(value);
				}
				inSubstitution = false;
			} else if (!inSubstitution) {
				result.append(c);
			}
		}
		return result.toString();
	}

	private static String getVariable(String key, String query) {
		if (key.equals("expression")) //$NON-NLS-1$
			return query;
		return key;
	}
}

Back to the top