Skip to main content
summaryrefslogtreecommitdiffstats
blob: 072248517a1eb7f3946e871f0f41a22367a04e2c (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*******************************************************************************
 * Copyright (c) 2000, 2014 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.*;
import java.net.*;
import java.util.Hashtable;

import javax.xml.parsers.*;

import org.eclipse.core.runtime.*;
import org.eclipse.help.IHelpResource;
import org.eclipse.help.internal.base.*;
import org.eclipse.help.internal.base.util.ProxyUtil;
import org.eclipse.help.internal.entityresolver.LocalEntityResolver;
import org.eclipse.help.search.ISearchEngine;
import org.eclipse.help.search.ISearchEngineResult;
import org.eclipse.help.search.ISearchEngineResultCollector;
import org.eclipse.help.search.ISearchScope;
import org.w3c.dom.*;
import org.xml.sax.*;

/**
 * This implementation of <code>ISearchEngine</code> interface performs search
 * by running a query on the remote InfoCenter and presenting the results
 * locally. Instances of this engine type are required to supply the URL to the
 * InfoCenter.
 * 
 * <p>
 * This class is made public in order to be instantiated and parametrized
 * directly in the extentsions. Clients are required to supply the required URL
 * 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 InfoCenter implements ISearchEngine {
	private Hashtable<String, IHelpResource> tocs;

	public static class Scope implements ISearchScope {
		String url;

		boolean searchSelected;

		String[] tocs;

		public Scope(String url, boolean searchSelected, String[] tocs) {
			this.url = url;
			this.searchSelected = searchSelected;
			this.tocs = tocs;
		}
	}

	private class InfoCenterResult implements ISearchEngineResult {
		private IHelpResource category;

		private Element node;

		private String baseURL;

		public InfoCenterResult(String baseURL, Element node) {
			this.baseURL = baseURL;
			this.node = node;
			createCategory(node);
		}

		private void createCategory(Element node) {
			final String href = node.getAttribute("toc"); //$NON-NLS-1$
			final String label = node.getAttribute("toclabel"); //$NON-NLS-1$
			if (href != null && label != null) {
				category = tocs.get(href);
				if (category == null) {
					category = new IHelpResource() {
						public String getLabel() {
							return label;
						}

						public String getHref() {
							return href;
						}
					};
					tocs.put(href, category);
				}
			}
		}

		public String getLabel() {
			return node.getAttribute("label"); //$NON-NLS-1$
		}

		public String getDescription() {
			return null;
		}

		public IHelpResource getCategory() {
			return category;
		}

		public String getHref() {
			return node.getAttribute("href"); //$NON-NLS-1$
		}

		public float getScore() {
			String value = node.getAttribute("score"); //$NON-NLS-1$
			if (value != null)
				return Float.parseFloat(value);
			return (float) 0.0;
		}

		public boolean getForceExternalWindow() {
			return false;
		}

		public String toAbsoluteHref(String href, boolean frames) {
			String url = baseURL;
			if (!url.endsWith("/")) //$NON-NLS-1$
				url = url + "/"; //$NON-NLS-1$
			if (frames) {
				return url + "topic" + href; //$NON-NLS-1$
			}
			if (url.indexOf('?')> 0)  {
				return url + "topic" + href + "&noframes=true"; //$NON-NLS-1$ //$NON-NLS-2$				
			}
			return url + "topic" + href + "?noframes=true"; //$NON-NLS-1$ //$NON-NLS-2$
		}
	}

	/**
	 * The default constructor.
	 */
	public InfoCenter() {
		tocs = new Hashtable<String, IHelpResource>();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ISearchEngine#run(String, ISearchScope,
	 *      ISearchEngineResultCollector, IProgressMonitor)
	 */
	public void run(String query, ISearchScope scope,
			ISearchEngineResultCollector collector, IProgressMonitor monitor)
			throws CoreException {
		URL url = createURL(query, (Scope) scope);
		if (url == null)
			return;
		InputStream is = null;
		tocs.clear();
		try {
			URLConnection connection = ProxyUtil.getConnection(url);
			monitor.beginTask(HelpBaseResources.InfoCenter_connecting, 5);
			is = connection.getInputStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					is, "utf-8"));//$NON-NLS-1$
			monitor.worked(1);
			load(((Scope) scope).url, reader, collector,
					new SubProgressMonitor(monitor, 4));
			reader.close();
		} catch (FileNotFoundException e) {
			reportError(HelpBaseResources.InfoCenter_fileNotFound, e, collector);
		} catch (IOException e) {
			reportError(HelpBaseResources.InfoCenter_io, e, collector);
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
				}
			}
		}
	}

	private void reportError(String message, IOException e,
			ISearchEngineResultCollector collector) {
		Status status = new Status(IStatus.ERROR, HelpBasePlugin.PLUGIN_ID,
				IStatus.OK, message, e);
		collector.error(status);
	}

	private void load(String baseURL, Reader r,
			ISearchEngineResultCollector collector, IProgressMonitor monitor) {
		Document document = null;
		try {
			DocumentBuilder parser = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			parser.setEntityResolver(new LocalEntityResolver());
			if (monitor.isCanceled())
				return;
			monitor.beginTask("", 5); //$NON-NLS-1$
			monitor.subTask(HelpBaseResources.InfoCenter_searching);
			document = parser.parse(new InputSource(r));
			if (monitor.isCanceled())
				return;

			// Strip out any comments first
			Node root = document.getFirstChild();
			while (root.getNodeType() == Node.COMMENT_NODE) {
				document.removeChild(root);
				root = document.getFirstChild();
				if (monitor.isCanceled())
					return;
			}
			monitor.worked(1);
			load(baseURL, document, (Element) root, collector,
					new SubProgressMonitor(monitor, 4));
		} catch (ParserConfigurationException e) {
			// ignore
		} catch (IOException e) {
			// ignore
		} catch (SAXException e) {
			// ignore
		}
	}

	private void load(String baseURL, Document doc, Element root,
			ISearchEngineResultCollector collector, IProgressMonitor monitor) {
		NodeList topics = root.getElementsByTagName("hit"); //$NON-NLS-1$
		ISearchEngineResult[] results = new ISearchEngineResult[topics
				.getLength()];
		monitor.subTask(HelpBaseResources.InfoCenter_processing);
		monitor.beginTask("", results.length); //$NON-NLS-1$
		for (int i = 0; i < topics.getLength(); i++) {
			Element el = (Element) topics.item(i);
			if (monitor.isCanceled())
				break;
			results[i] = new InfoCenterResult(baseURL, el);
			monitor.worked(1);
		}
		collector.accept(results);
	}

	private URL createURL(String query, Scope scope) {
		if (scope.url == null) {
			return null;
		}
		StringBuffer buf = new StringBuffer();
		buf.append(scope.url);
		if (!scope.url.endsWith("/")) //$NON-NLS-1$
			buf.append("/search?phrase="); //$NON-NLS-1$
		else
			buf.append("search?phrase="); //$NON-NLS-1$
		try {
			buf.append(URLEncoder.encode(query, "UTF-8")); //$NON-NLS-1$
		} catch (UnsupportedEncodingException e) {
			buf.append(query);
		}
		buf.append("&locale="); //$NON-NLS-1$
		buf.append(Platform.getNL());
		if (scope.searchSelected && scope.tocs != null) {
			buf.append("&scopedSearch=true"); //$NON-NLS-1$
			for (int i = 0; i < scope.tocs.length; i++) {
				String toc;
				try {
					toc = URLEncoder.encode(scope.tocs[i], "UTF-8"); //$NON-NLS-1$
				} catch (UnsupportedEncodingException e) {
					toc = scope.tocs[i];
				}
				buf.append("&scope="); //$NON-NLS-1$
				buf.append(toc);
			}
		}
		try {
			return new URL(buf.toString());
		} catch (MalformedURLException e) {
			return null;
		}
	}
}

Back to the top