KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > search > InfoCenter


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.search;
12
13 import java.io.*;
14 import java.net.*;
15 import java.util.Hashtable JavaDoc;
16
17 import javax.xml.parsers.*;
18
19 import org.eclipse.core.runtime.*;
20 import org.eclipse.help.IHelpResource;
21 import org.eclipse.help.internal.base.*;
22 import org.eclipse.help.search.ISearchEngine;
23 import org.eclipse.help.search.ISearchEngineResult;
24 import org.eclipse.help.search.ISearchEngineResultCollector;
25 import org.eclipse.help.search.ISearchScope;
26 import org.w3c.dom.*;
27 import org.xml.sax.*;
28
29 /**
30  * This implementation of <code>ISearchEngine</code> interface performs search
31  * by running a query on the remote InfoCenter and presenting the results
32  * locally. Instances of this engine type are required to supply the URL to the
33  * InfoCenter.
34  *
35  * <p>
36  * This class is made public in order to be instantiated and parametrized
37  * directly in the extentsions. Clients are required to supply the required URL
38  * as a parameter <code>url</code>.
39  *
40  * <p>
41  * This class is not expected to be subclassed or otherwise accessed
42  * programmatically.
43  *
44  * @since 3.1
45  */

46
47 public final class InfoCenter implements ISearchEngine {
48     private Hashtable JavaDoc tocs;
49
50     public static class Scope implements ISearchScope {
51         String JavaDoc url;
52
53         boolean searchSelected;
54
55         String JavaDoc[] tocs;
56
57         public Scope(String JavaDoc url, boolean searchSelected, String JavaDoc[] tocs) {
58             this.url = url;
59             this.searchSelected = searchSelected;
60             this.tocs = tocs;
61         }
62     }
63
64     private class InfoCenterResult implements ISearchEngineResult {
65         private IHelpResource category;
66
67         private Element node;
68
69         private String JavaDoc baseURL;
70
71         public InfoCenterResult(String JavaDoc baseURL, Element node) {
72             this.baseURL = baseURL;
73             this.node = node;
74             createCategory(node);
75         }
76
77         private void createCategory(Element node) {
78             final String JavaDoc href = node.getAttribute("toc"); //$NON-NLS-1$
79
final String JavaDoc label = node.getAttribute("toclabel"); //$NON-NLS-1$
80
if (href != null && label != null) {
81                 category = (IHelpResource) tocs.get(href);
82                 if (category == null) {
83                     category = new IHelpResource() {
84                         public String JavaDoc getLabel() {
85                             return label;
86                         }
87
88                         public String JavaDoc getHref() {
89                             return href;
90                         }
91                     };
92                     tocs.put(href, category);
93                 }
94             }
95         }
96
97         public String JavaDoc getLabel() {
98             return node.getAttribute("label"); //$NON-NLS-1$
99
}
100
101         public String JavaDoc getDescription() {
102             return null;
103         }
104
105         public IHelpResource getCategory() {
106             return category;
107         }
108
109         public String JavaDoc getHref() {
110             return node.getAttribute("href"); //$NON-NLS-1$
111
}
112
113         public float getScore() {
114             String JavaDoc value = node.getAttribute("score"); //$NON-NLS-1$
115
if (value != null)
116                 return Float.parseFloat(value);
117             return (float) 0.0;
118         }
119
120         public boolean getForceExternalWindow() {
121             return false;
122         }
123
124         public String JavaDoc toAbsoluteHref(String JavaDoc href, boolean frames) {
125             String JavaDoc url = baseURL;
126             if (!url.endsWith("/")) //$NON-NLS-1$
127
url = url + "/"; //$NON-NLS-1$
128
if (frames) {
129                 return url + "topic" + href; //$NON-NLS-1$
130
}
131             return url + "topic" + href + "&noframes=true"; //$NON-NLS-1$ //$NON-NLS-2$
132
}
133     }
134
135     /**
136      * The default constructor.
137      */

138     public InfoCenter() {
139         tocs = new Hashtable JavaDoc();
140     }
141
142     /*
143      * (non-Javadoc)
144      *
145      * @see ISearchEngine#run(String, ISearchScope,
146      * ISearchEngineResultCollector, IProgressMonitor)
147      */

148     public void run(String JavaDoc query, ISearchScope scope,
149             ISearchEngineResultCollector collector, IProgressMonitor monitor)
150             throws CoreException {
151         URL url = createURL(query, (Scope) scope);
152         if (url == null)
153             return;
154         InputStream is = null;
155         tocs.clear();
156         try {
157             URLConnection connection = url.openConnection();
158             monitor.beginTask(HelpBaseResources.InfoCenter_connecting, 5);
159             is = connection.getInputStream();
160             BufferedReader reader = new BufferedReader(new InputStreamReader(
161                     is, "utf-8"));//$NON-NLS-1$
162
monitor.worked(1);
163             load(((Scope) scope).url, reader, collector,
164                     new SubProgressMonitor(monitor, 4));
165             reader.close();
166         } catch (FileNotFoundException e) {
167             reportError(HelpBaseResources.InfoCenter_fileNotFound, e, collector);
168         } catch (IOException e) {
169             reportError(HelpBaseResources.InfoCenter_io, e, collector);
170         } finally {
171             if (is != null) {
172                 try {
173                     is.close();
174                 } catch (IOException e) {
175                 }
176             }
177         }
178     }
179
180     private void reportError(String JavaDoc message, IOException e,
181             ISearchEngineResultCollector collector) {
182         Status status = new Status(IStatus.ERROR, HelpBasePlugin.PLUGIN_ID,
183                 IStatus.OK, message, e);
184         collector.error(status);
185     }
186
187     private void load(String JavaDoc baseURL, Reader r,
188             ISearchEngineResultCollector collector, IProgressMonitor monitor) {
189         Document document = null;
190         try {
191             DocumentBuilder parser = DocumentBuilderFactory.newInstance()
192                     .newDocumentBuilder();
193             if (monitor.isCanceled())
194                 return;
195             monitor.beginTask("", 5); //$NON-NLS-1$
196
monitor.subTask(HelpBaseResources.InfoCenter_searching);
197             document = parser.parse(new InputSource(r));
198             if (monitor.isCanceled())
199                 return;
200
201             // Strip out any comments first
202
Node root = document.getFirstChild();
203             while (root.getNodeType() == Node.COMMENT_NODE) {
204                 document.removeChild(root);
205                 root = document.getFirstChild();
206                 if (monitor.isCanceled())
207                     return;
208             }
209             monitor.worked(1);
210             load(baseURL, document, (Element) root, collector,
211                     new SubProgressMonitor(monitor, 4));
212         } catch (ParserConfigurationException e) {
213             // ignore
214
} catch (IOException e) {
215             // ignore
216
} catch (SAXException e) {
217             // ignore
218
}
219     }
220
221     private void load(String JavaDoc baseURL, Document doc, Element root,
222             ISearchEngineResultCollector collector, IProgressMonitor monitor) {
223         NodeList topics = root.getElementsByTagName("topic"); //$NON-NLS-1$
224
ISearchEngineResult[] results = new ISearchEngineResult[topics
225                 .getLength()];
226         monitor.subTask(HelpBaseResources.InfoCenter_processing);
227         monitor.beginTask("", results.length); //$NON-NLS-1$
228
for (int i = 0; i < topics.getLength(); i++) {
229             Element el = (Element) topics.item(i);
230             if (monitor.isCanceled())
231                 break;
232             results[i] = new InfoCenterResult(baseURL, el);
233             monitor.worked(1);
234         }
235         collector.accept(results);
236     }
237
238     private URL createURL(String JavaDoc query, Scope scope) {
239         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
240         buf.append(scope.url);
241         if (!scope.url.endsWith("/")) //$NON-NLS-1$
242
buf.append("/search?searchWord="); //$NON-NLS-1$
243
else
244             buf.append("search?searchWord="); //$NON-NLS-1$
245
try {
246             buf.append(URLEncoder.encode(query, "UTF-8")); //$NON-NLS-1$
247
} catch (UnsupportedEncodingException e) {
248             buf.append(query);
249         }
250         buf.append("&locale="); //$NON-NLS-1$
251
buf.append(Platform.getNL());
252         if (scope.searchSelected && scope.tocs != null) {
253             buf.append("&scopedSearch=true"); //$NON-NLS-1$
254
for (int i = 0; i < scope.tocs.length; i++) {
255                 String JavaDoc toc;
256                 try {
257                     toc = URLEncoder.encode(scope.tocs[i], "UTF-8"); //$NON-NLS-1$
258
} catch (UnsupportedEncodingException e) {
259                     toc = scope.tocs[i];
260                 }
261                 buf.append("&scope="); //$NON-NLS-1$
262
buf.append(toc);
263             }
264         }
265         try {
266             return new URL(buf.toString());
267         } catch (MalformedURLException e) {
268             return null;
269         }
270     }
271 }
272
Popular Tags