KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > base > remote > RemoteSearchManager


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.base.remote;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.SubProgressMonitor;
20 import org.eclipse.help.internal.base.HelpBasePlugin;
21 import org.eclipse.help.internal.search.ISearchHitCollector;
22 import org.eclipse.help.internal.search.ISearchQuery;
23 import org.eclipse.help.internal.search.QueryTooComplexException;
24 import org.eclipse.help.internal.util.URLCoder;
25
26 /*
27  * Manages indexing and searching for all remote help content.
28  */

29 public class RemoteSearchManager {
30
31     private static final String JavaDoc PATH_SEARCH = "/search"; //$NON-NLS-1$
32
private static final String JavaDoc PARAM_PHRASE = "phrase"; //$NON-NLS-1$
33
private static final String JavaDoc PARAM_LANG = "lang"; //$NON-NLS-1$
34
private RemoteSearchParser parser;
35
36     /*
37      * Performs a search for remote content.
38      */

39     public void search(ISearchQuery searchQuery, ISearchHitCollector collector, IProgressMonitor pm)
40             throws QueryTooComplexException {
41         pm.beginTask("", 100); //$NON-NLS-1$
42
try {
43             // infocenters ignore remote content
44
if (RemoteHelp.isEnabled()) {
45                 InputStream JavaDoc in = null;
46                 try {
47                     URL JavaDoc url = RemoteHelp.getURL(PATH_SEARCH + '?' + PARAM_PHRASE + '=' + URLCoder.encode(searchQuery.getSearchWord()) + '&' + PARAM_LANG + '=' + searchQuery.getLocale());
48                     in = url.openStream();
49                     if (parser == null) {
50                         parser = new RemoteSearchParser();
51                     }
52                     // parse the XML-serialized search results
53
List JavaDoc hits = parser.parse(in, new SubProgressMonitor(pm, 100));
54                     collector.addHits(hits, null);
55                 }
56                 catch (IOException JavaDoc e) {
57                     String JavaDoc msg = "I/O error while trying to contact the remote help server"; //$NON-NLS-1$
58
HelpBasePlugin.logError(msg, e);
59                 }
60                 catch (Throwable JavaDoc t) {
61                     String JavaDoc msg = "Internal error while reading search results from remote server"; //$NON-NLS-1$
62
HelpBasePlugin.logError(msg, t);
63                 }
64                 finally {
65                     if (in != null) {
66                         try {
67                             in.close();
68                         }
69                         catch (IOException JavaDoc e) {
70                             // nothing more we can do
71
}
72                     }
73                 }
74             }
75         }
76         finally {
77             pm.done();
78         }
79     }
80 }
81
Popular Tags