KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.UnsupportedEncodingException JavaDoc;
14 import java.net.URLEncoder JavaDoc;
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 /**
25  * This implementation of <code>ISearchEngine</code> interface performs search
26  * by running a query on the remote web site using the provided query URL.
27  * Instances of this engine type are required to supply the URL template with
28  * the query string replaced with the substitution string
29  * <code>{expression}</code>.
30  * <p>
31  * This class is made public in order to be instantiated and parametrized
32  * directly in the extensions. Clients are required to supply the URL template
33  * string as a parameter <code>url</code>.
34  *
35  * <p>
36  * This class is not expected to be subclassed or otherwise accessed
37  * programmatically.
38  *
39  * @since 3.1
40  */

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 JavaDoc urlTemplate;
48
49         public Scope(String JavaDoc urlTemplate) {
50             this.urlTemplate = urlTemplate;
51         }
52
53         public String JavaDoc getURLTemplate() {
54             return urlTemplate;
55         }
56     }
57
58     private static class SearchResult implements ISearchEngineResult {
59         private String JavaDoc query;
60
61         private String JavaDoc urlTemplate;
62
63         public SearchResult(String JavaDoc query, String JavaDoc urlTemplate) {
64             this.query = query;
65             this.urlTemplate = urlTemplate;
66         }
67
68         public String JavaDoc getDescription() {
69             return HelpBaseResources.WebSearch_click;
70         }
71
72         public String JavaDoc getHref() {
73             String JavaDoc href = null;
74             String JavaDoc equery;
75             try {
76                 equery = URLEncoder.encode(query, "UTF-8"); //$NON-NLS-1$
77

78             } catch (UnsupportedEncodingException JavaDoc e) {
79                 equery = query;
80             }
81             href = composeURL(equery, urlTemplate);
82             return href;
83         }
84
85         public String JavaDoc getLabel() {
86             return HelpBaseResources.WebSearch_label;
87         }
88
89         public float getScore() {
90             return 1;
91         }
92
93         /*
94          * (non-Javadoc)
95          *
96          * @see org.eclipse.help.internal.search.federated.ISearchEngineResult#getCategory()
97          */

98         public IHelpResource getCategory() {
99             return null;
100         }
101
102         public boolean getForceExternalWindow() {
103             return true;
104         }
105
106         public String JavaDoc toAbsoluteHref(String JavaDoc href, boolean frames) {
107             return href;
108         }
109     }
110
111     /*
112      * (non-Javadoc)
113      *
114      * @see org.eclipse.help.internal.search.federated.ISearchEngine#run(java.lang.String,
115      * org.eclipse.help.internal.search.ISearchScope,
116      * org.eclipse.help.internal.search.federated.ISearchEngineResultCollector,
117      * org.eclipse.core.runtime.IProgressMonitor)
118      */

119     public void run(String JavaDoc 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 JavaDoc composeURL(String JavaDoc query, String JavaDoc urlTemplate) {
128         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
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                     // look ahead
136
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                     // look ahead
149
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 JavaDoc key = urlTemplate.substring(varStart + 1, i);
158                     String JavaDoc 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 JavaDoc getVariable(String JavaDoc key, String JavaDoc query) {
170         if (key.equals("expression")) //$NON-NLS-1$
171
return query;
172         return key;
173     }
174 }
175
Popular Tags