1 11 package org.eclipse.help.internal.search; 12 13 import java.io.*; 14 import java.net.*; 15 import java.util.Hashtable ; 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 46 47 public final class InfoCenter implements ISearchEngine { 48 private Hashtable tocs; 49 50 public static class Scope implements ISearchScope { 51 String url; 52 53 boolean searchSelected; 54 55 String [] tocs; 56 57 public Scope(String url, boolean searchSelected, String [] 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 baseURL; 70 71 public InfoCenterResult(String 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 href = node.getAttribute("toc"); final String label = node.getAttribute("toclabel"); if (href != null && label != null) { 81 category = (IHelpResource) tocs.get(href); 82 if (category == null) { 83 category = new IHelpResource() { 84 public String getLabel() { 85 return label; 86 } 87 88 public String getHref() { 89 return href; 90 } 91 }; 92 tocs.put(href, category); 93 } 94 } 95 } 96 97 public String getLabel() { 98 return node.getAttribute("label"); } 100 101 public String getDescription() { 102 return null; 103 } 104 105 public IHelpResource getCategory() { 106 return category; 107 } 108 109 public String getHref() { 110 return node.getAttribute("href"); } 112 113 public float getScore() { 114 String value = node.getAttribute("score"); 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 toAbsoluteHref(String href, boolean frames) { 125 String url = baseURL; 126 if (!url.endsWith("/")) url = url + "/"; if (frames) { 129 return url + "topic" + href; } 131 return url + "topic" + href + "&noframes=true"; } 133 } 134 135 138 public InfoCenter() { 139 tocs = new Hashtable (); 140 } 141 142 148 public void run(String 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")); 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 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 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); monitor.subTask(HelpBaseResources.InfoCenter_searching); 197 document = parser.parse(new InputSource(r)); 198 if (monitor.isCanceled()) 199 return; 200 201 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 } catch (IOException e) { 215 } catch (SAXException e) { 217 } 219 } 220 221 private void load(String baseURL, Document doc, Element root, 222 ISearchEngineResultCollector collector, IProgressMonitor monitor) { 223 NodeList topics = root.getElementsByTagName("topic"); ISearchEngineResult[] results = new ISearchEngineResult[topics 225 .getLength()]; 226 monitor.subTask(HelpBaseResources.InfoCenter_processing); 227 monitor.beginTask("", results.length); 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 query, Scope scope) { 239 StringBuffer buf = new StringBuffer (); 240 buf.append(scope.url); 241 if (!scope.url.endsWith("/")) buf.append("/search?searchWord="); else 244 buf.append("search?searchWord="); try { 246 buf.append(URLEncoder.encode(query, "UTF-8")); } catch (UnsupportedEncodingException e) { 248 buf.append(query); 249 } 250 buf.append("&locale="); buf.append(Platform.getNL()); 252 if (scope.searchSelected && scope.tocs != null) { 253 buf.append("&scopedSearch=true"); for (int i = 0; i < scope.tocs.length; i++) { 255 String toc; 256 try { 257 toc = URLEncoder.encode(scope.tocs[i], "UTF-8"); } catch (UnsupportedEncodingException e) { 259 toc = scope.tocs[i]; 260 } 261 buf.append("&scope="); 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 |