KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)SearchResultSupport.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: SearchResultSupport.java,v 1.1.1.1 2003/09/24 14:59:03 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.http.HttpServletRequest JavaDoc;
43 import javax.servlet.http.HttpUtils JavaDoc;
44 import javax.servlet.jsp.JspException JavaDoc;
45
46
47 /**
48  * Implements methods used to retrieve Google Search Result properties.
49  *
50  * @author Erik C. Thauvin
51  * @created April 26, 2002
52  * @version $Revision: 1.1.1.1 $
53  * @since 1.0
54  */

55 public abstract class SearchResultSupport extends StyleSupport
56 {
57     /**
58      * The Google search bean.
59      */

60     protected GoogleSearchBean bean = null;
61
62     /**
63      * doEndTag method.
64      *
65      * @return EVAL_PAGE
66      * @exception JspException
67      */

68     public int doEndTag()
69                  throws JspException JavaDoc
70     {
71         try
72         {
73             // Is the result valid?
74
if ((bean != null) && (bean.isValidResult()))
75             {
76                 // Output the property
77
pageContext.getOut().write(getResultProperty());
78             }
79         }
80         catch (Exception JavaDoc e)
81         {
82             throw TagUtility.outputError(getTagName(), e);
83         }
84
85         // Reset the values
86
reset();
87
88         return EVAL_PAGE;
89     }
90
91     /**
92      * doStartTag method.
93      *
94      * @return EVAL_BODY_TAG or SKIP_BODY.
95      * @exception JspException
96      */

97     public int doStartTag()
98                    throws JspException JavaDoc
99     {
100         // Get the Google bean
101
bean = TagUtility.getGoogleSearchBean(pageContext);
102
103         // Is the result valid?
104
if ((bean != null) && (bean.isValidResult()))
105         {
106             return EVAL_BODY_TAG;
107         }
108
109         return SKIP_BODY;
110     }
111
112     /**
113      * Release method.
114      */

115     public void release()
116     {
117         super.release();
118
119         // Reset the bean
120
bean = null;
121
122         // Reset the values
123
reset();
124     }
125
126     /**
127      * Returns the result of the build action and must be implemented by classes
128      * which extend SearchResultSupport.
129      * <p>
130      * For example:
131      * <p>
132      * <blockquote>return "Some Value";</blockquote>
133      *
134      * @return The property name.
135      */

136     protected abstract String JavaDoc getPropertyName();
137
138     /**
139      * Returns the value of specified search result property.
140      *
141      * @return The property value.
142      */

143     protected String JavaDoc getResultProperty()
144     {
145         String JavaDoc name = getPropertyName();
146         String JavaDoc value = bean.getResultProperty(name);
147
148         if ((TagUtility.isValidString(value))
149                 && (name.equals(GoogleSearchBean.NEXT_KEYWORD)
150                 || name.equals(GoogleSearchBean.PREVIOUS_KEYWORD)))
151         {
152             HttpServletRequest JavaDoc request =
153                 (HttpServletRequest JavaDoc)pageContext.getRequest();
154
155             String JavaDoc url =
156                 HttpUtils.getRequestURL(request).toString() + '?'
157                 + TagUtility.START_PARAM + '=' + value + '&'
158                 + TagUtility.requestParamsToUrl(request, TagUtility.START_PARAM);
159
160             return (TagUtility.buildRefLink(url,
161                                             TagUtility.getTagBody(bodyContent),
162                                             target, style, css));
163         }
164         else
165         {
166             return value;
167         }
168     }
169
170     /**
171      * Returns the name of the tag extending SearchResultSupport.
172      * <p>
173      * For example:
174      * <p>
175      * <blockquote> void getErrorString() { return "MyTag"; }</blockquote>
176      *
177      * @return The tag name.
178      */

179     protected abstract String JavaDoc getTagName();
180
181     /**
182      * Reset the values.
183      */

184     protected void reset()
185     {
186         super.reset();
187     }
188 }
189
Popular Tags