KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > thauvin > google > taglibs > Search


1 /*
2  * @(#)Search.java
3  *
4  * Copyright (c) 2002-2003, Erik C. Thauvin (erik@thauvin.net)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met:
10  *
11  * Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  *
14  * Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * Neither the name of the author nor the names of its contributors may be
19  * used to endorse or promote products derived from this software without
20  * specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
23  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
26  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * $Id: Search.java,v 1.7 2003/10/18 01:51:29 ethauvin Exp $
35  *
36  */

37 package net.thauvin.google.taglibs;
38
39 import net.thauvin.google.GoogleSearchBean;
40 import net.thauvin.google.TagUtility;
41
42 import javax.servlet.jsp.JspException JavaDoc;
43 import javax.servlet.jsp.PageContext JavaDoc;
44
45 /**
46  * A custom tag used to perform Google searches.
47  *
48  * @author Erik C. Thauvin
49  * @created April 25, 2002
50  * @version $Revision: 1.7 $, $Date: 2003/10/18 01:51:29 $
51  * @since 1.0
52  */

53 public class Search extends QuerySupport
54 {
55     private GoogleSearchBean bean = null;
56     private boolean cache = GoogleSearchBean.DEFAULT_CACHE;
57     private boolean filter = GoogleSearchBean.DEFAULT_FILTER;
58     private String JavaDoc lr = GoogleSearchBean.DEFAULT_LR;
59     private int maxResults = GoogleSearchBean.DEFAULT_MAX_RESULTS;
60     private String JavaDoc restrict = GoogleSearchBean.DEFAULT_RESTRICT;
61     private boolean safeSearch = GoogleSearchBean.DEFAULT_SAFE_SEARCH;
62     private String JavaDoc site = GoogleSearchBean.DEFAULT_SITE;
63     private int start = GoogleSearchBean.DEFAULT_START;
64     private String JavaDoc type = GoogleSearchBean.DEFAULT_TYPE;
65
66     /**
67      * Sets the cache attribute.
68      *
69      * @param cache The new attribute value.
70      */

71     public final void setCache(String JavaDoc cache)
72     {
73         this.cache = Boolean.valueOf(cache).booleanValue();
74     }
75
76     /**
77      * Sets the (related-query) filter attribute.
78      *
79      * @param filter The new attribute value.
80      */

81     public final void setFilter(String JavaDoc filter)
82     {
83         this.filter = Boolean.valueOf(filter).booleanValue();
84     }
85
86     /**
87      * Sets the lr (language restrict) attribute.
88      *
89      * @param lr The new attribute value.
90      */

91     public final void setLr(String JavaDoc lr)
92     {
93         this.lr = lr;
94     }
95
96     /**
97      * Sets the maximum number of results to be returned.
98      *
99      * @param maxResults The new attribute value.
100      */

101     public final void setMaxResults(String JavaDoc maxResults)
102     {
103         try
104         {
105             this.maxResults = Integer.valueOf(maxResults).intValue();
106         }
107         catch (NumberFormatException JavaDoc e)
108         {
109             ;// Do nothing
110
}
111     }
112
113     /**
114      * Sets the restrict attribute.
115      *
116      * @param restrict The new restrict attribute.
117      */

118     public final void setRestrict(String JavaDoc restrict)
119     {
120         this.restrict = restrict;
121     }
122
123     /**
124      * Sets the safeSearch attribute.
125      *
126      * @param safeSearch The new attribute value.
127      */

128     public final void setSafeSearch(String JavaDoc safeSearch)
129     {
130         this.safeSearch = Boolean.valueOf(safeSearch).booleanValue();
131     }
132
133     /**
134      * Sets the site attribute.
135      *
136      * @param site The new attribute value.
137      */

138     public final void setSite(String JavaDoc site)
139     {
140         this.site = site;
141     }
142
143     /**
144      * Sets the start attribute.
145      *
146      * @param start The new attribute value.
147      */

148     public final void setStart(String JavaDoc start)
149     {
150         try
151         {
152             this.start = Integer.valueOf(start).intValue();
153         }
154         catch (NumberFormatException JavaDoc e)
155         {
156             ;// Do nothing
157
}
158     }
159
160     /**
161      * Sets the (file) type attribute.
162      *
163      * @param type The new attribute value.
164      * @since 1.0.1
165      */

166     public final void setType(String JavaDoc type)
167     {
168         this.type = type;
169     }
170
171     /**
172      * Returns the cache attribute.
173      *
174      * @return The attribute value.
175      */

176     private final boolean getCache()
177     {
178         return getBoolParam(TagUtility.CACHE_PARAM, cache);
179     }
180
181     /**
182      * Returns the (related-query) filter attribute.
183      *
184      * @return The attribute value.
185      */

186     private final boolean getFilter()
187     {
188         return getBoolParam(TagUtility.FILTER_PARAM, filter);
189     }
190
191     /**
192      * Returns the lr (language restrict) attribute.
193      *
194      * @return The attribute value.
195      */

196     private final String JavaDoc getLr()
197     {
198         return getStringParam(TagUtility.LR_PARAM, lr);
199     }
200
201     /**
202      * Returns the maximum number of results to be returned.
203      *
204      * @return The attribute value.
205      */

206     private final int getMaxResults()
207     {
208         return getIntParam(TagUtility.MAX_RESULTS_PARAM, maxResults);
209     }
210
211     /**
212      * Returns the restrict attribute.
213      *
214      * @return The attribute value.
215      */

216     private final String JavaDoc getRestrict()
217     {
218         return getStringParam(TagUtility.RESTRICT_PARAM, restrict);
219     }
220
221     /**
222      * Returns the safeSearch attribute.
223      *
224      * @return The attribute value.
225      */

226     private final boolean getSafeSearch()
227     {
228         return getBoolParam(TagUtility.SAFE_SEARCH_PARAM, safeSearch);
229     }
230
231     /**
232      * Returns the site attribute.
233      *
234      * @return The attribute value.
235      */

236     private final String JavaDoc getSite()
237     {
238         String JavaDoc site = getStringParam(TagUtility.SITE_PARAM, this.site);
239
240         if (site.length() > 0)
241         {
242             return ("site:" + site + ' ');
243         }
244
245         return "";
246     }
247
248     /**
249      * Returns the start attribute.
250      *
251      * @return The attribute value.
252      */

253     private final int getStart()
254     {
255         return getIntParam(TagUtility.START_PARAM, start);
256     }
257
258     /**
259      * Returns the (file) type attribute.
260      *
261      * @return The attribute value
262      */

263     private final String JavaDoc getType()
264     {
265         String JavaDoc type = getStringParam(TagUtility.TYPE_PARAM, this.type);
266
267         if (type.length() > 0)
268         {
269             return (" filetype:" + type);
270         }
271
272         return "";
273     }
274
275     /**
276      * Converts a request parameter to a boolean.
277      *
278      * @param paramName The parameter name.
279      * @param defaultValue The default value to use if the parameter is empty.
280      * @return The boolean value.
281      */

282     private boolean getBoolParam(String JavaDoc paramName, boolean defaultValue)
283     {
284         String JavaDoc param =
285                 TagUtility.getParameter(pageContext.getRequest(), paramName);
286
287         if (TagUtility.isValidString(param, true))
288         {
289             return Boolean.valueOf(param).booleanValue();
290         }
291
292         return defaultValue;
293     }
294
295     /**
296      * Converts a request parameter to an int.
297      *
298      * @param paramName The parameter name.
299      * @param defaultValue The default value to use if the parameter is empty.
300      * @return The int value.
301      */

302     private int getIntParam(String JavaDoc paramName, int defaultValue)
303     {
304         String JavaDoc param =
305                 TagUtility.getParameter(pageContext.getRequest(), paramName);
306
307         if (TagUtility.isValidString(param, true))
308         {
309             try
310             {
311                 return Integer.valueOf(param).intValue();
312             }
313             catch (NumberFormatException JavaDoc e)
314             {
315                 ;// Do nothing
316
}
317         }
318
319         return defaultValue;
320     }
321
322     /**
323      * Converts a request parameter to a string.
324      *
325      * @param paramName The parameter name.
326      * @param defaultValue The default value to use if the parameter is empty.
327      * @return The string value.
328      */

329     private String JavaDoc getStringParam(String JavaDoc paramName, String JavaDoc defaultValue)
330     {
331         String JavaDoc param =
332                 TagUtility.getParameter(pageContext.getRequest(), paramName);
333
334         if (TagUtility.isValidString(param, true))
335         {
336             return param;
337         }
338
339         return defaultValue;
340     }
341
342     /**
343      * doEndTag method.
344      *
345      * @return EVAL_PAGE
346      * @exception JspException
347      */

348     public int doEndTag()
349         throws JspException JavaDoc
350     {
351         final String JavaDoc query = getQuery();
352
353         if (TagUtility.isValidString(query, true))
354         {
355             try
356             {
357                 bean.setProxyServer(pageContext.getServletContext()
358                         .getInitParameter(TagUtility.GOOGLE_PROXY_HOST),
359                         pageContext.getServletContext()
360                         .getInitParameter(TagUtility.GOOGLE_PROXY_PORT),
361                         pageContext.getServletContext()
362                         .getInitParameter(TagUtility.GOOGLE_PROXY_USERNAME),
363                         pageContext.getServletContext()
364                         .getInitParameter(TagUtility.GOOGLE_PROXY_PASSWORD));
365
366                 bean.setKeywords(getQuery());
367                 
368                 bean.getGoogleSearch(getKey(), getSite() + getQuery() + getType(),
369                         getStart(), getMaxResults(), getFilter(),
370                         getRestrict(), getSafeSearch(), getLr());
371             }
372             catch (Exception JavaDoc e)
373             {
374                 throw TagUtility.outputError("search", e);
375             }
376         }
377         else if (!getCache())
378         {
379             bean.reset();
380         }
381
382         // Reset the values
383
reset();
384
385         return EVAL_PAGE;
386     }
387
388     /**
389      * doStartTag method.
390      *
391      * @return EVAL_BODY_TAG.
392      * @exception JspException
393      */

394     public int doStartTag()
395         throws JspException JavaDoc
396     {
397         // Get the Google bean
398
bean = TagUtility.getGoogleSearchBean(pageContext);
399
400         // Create a new bean if it doesn't exists
401
if (bean == null)
402         {
403             try
404             {
405                 bean = new GoogleSearchBean();
406
407                 // Set the bean as named session attribute
408
pageContext.setAttribute(TagUtility.GOOGLE_SEARCH_BEAN, bean,
409                         PageContext.SESSION_SCOPE);
410             }
411             catch (Exception JavaDoc e)
412             {
413                 throw new JspException JavaDoc("An unknown error ocurred while creating the Google search bean.");
414             }
415         }
416
417         return EVAL_BODY_TAG;
418     }
419
420     /**
421      * Release method.
422      */

423     public void release()
424     {
425         super.release();
426
427         // Reset all attributes
428
start = GoogleSearchBean.DEFAULT_START;
429         maxResults = GoogleSearchBean.DEFAULT_MAX_RESULTS;
430         filter = GoogleSearchBean.DEFAULT_FILTER;
431         safeSearch = GoogleSearchBean.DEFAULT_SAFE_SEARCH;
432         restrict = GoogleSearchBean.DEFAULT_RESTRICT;
433         lr = GoogleSearchBean.DEFAULT_LR;
434         site = GoogleSearchBean.DEFAULT_SITE;
435         cache = GoogleSearchBean.DEFAULT_CACHE;
436         type = GoogleSearchBean.DEFAULT_TYPE;
437
438         // Reset the bean
439
bean = null;
440
441         // Reset the values
442
reset();
443     }
444
445     /**
446      * Reset the values.
447      */

448     protected void reset()
449     {
450         super.reset();
451     }
452 }
453
Popular Tags