1 11 package org.eclipse.help.internal.search; 12 13 import java.io.UnsupportedEncodingException ; 14 import java.net.URLEncoder ; 15 16 import org.eclipse.core.runtime.*; 17 import org.eclipse.help.IHelpResource; 18 import org.eclipse.help.internal.base.HelpBaseResources; 19 import org.eclipse.help.search.ISearchEngine; 20 import org.eclipse.help.search.ISearchEngineResult; 21 import org.eclipse.help.search.ISearchEngineResultCollector; 22 import org.eclipse.help.search.ISearchScope; 23 24 41 public final class WebSearch implements ISearchEngine { 42 private static final char C_START = '{'; 43 44 private static final char C_STOP = '}'; 45 46 public static class Scope implements ISearchScope { 47 private String urlTemplate; 48 49 public Scope(String urlTemplate) { 50 this.urlTemplate = urlTemplate; 51 } 52 53 public String getURLTemplate() { 54 return urlTemplate; 55 } 56 } 57 58 private static class SearchResult implements ISearchEngineResult { 59 private String query; 60 61 private String urlTemplate; 62 63 public SearchResult(String query, String urlTemplate) { 64 this.query = query; 65 this.urlTemplate = urlTemplate; 66 } 67 68 public String getDescription() { 69 return HelpBaseResources.WebSearch_click; 70 } 71 72 public String getHref() { 73 String href = null; 74 String equery; 75 try { 76 equery = URLEncoder.encode(query, "UTF-8"); 78 } catch (UnsupportedEncodingException e) { 79 equery = query; 80 } 81 href = composeURL(equery, urlTemplate); 82 return href; 83 } 84 85 public String getLabel() { 86 return HelpBaseResources.WebSearch_label; 87 } 88 89 public float getScore() { 90 return 1; 91 } 92 93 98 public IHelpResource getCategory() { 99 return null; 100 } 101 102 public boolean getForceExternalWindow() { 103 return true; 104 } 105 106 public String toAbsoluteHref(String href, boolean frames) { 107 return href; 108 } 109 } 110 111 119 public void run(String query, ISearchScope scope, 120 ISearchEngineResultCollector collector, IProgressMonitor monitor) 121 throws CoreException { 122 123 collector 124 .accept(new SearchResult(query, ((Scope) scope).getURLTemplate())); 125 } 126 127 private static String composeURL(String query, String urlTemplate) { 128 StringBuffer result = new StringBuffer (); 129 boolean inSubstitution = false; 130 int varStart = -1; 131 for (int i = 0; i < urlTemplate.length(); i++) { 132 char c = urlTemplate.charAt(i); 133 if (c == C_START && !inSubstitution) { 134 if (i < urlTemplate.length() - 1) { 135 char c2 = urlTemplate.charAt(i + 1); 137 if (c2 == C_START) { 138 result.append(c); 139 i++; 140 continue; 141 } 142 } 143 inSubstitution = true; 144 varStart = i; 145 continue; 146 } else if (c == C_STOP && inSubstitution) { 147 if (i < urlTemplate.length() - 1) { 148 char c2 = urlTemplate.charAt(i + 1); 150 if (c2 == C_STOP) { 151 result.append(c); 152 i++; 153 continue; 154 } 155 } 156 if (varStart != -1) { 157 String key = urlTemplate.substring(varStart + 1, i); 158 String value = getVariable(key, query); 159 result.append(value); 160 } 161 inSubstitution = false; 162 } else if (!inSubstitution) { 163 result.append(c); 164 } 165 } 166 return result.toString(); 167 } 168 169 private static String getVariable(String key, String query) { 170 if (key.equals("expression")) return query; 172 return key; 173 } 174 } 175 | Popular Tags |