KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)SearchResult.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: SearchResult.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.jsp.*;
43 import javax.servlet.jsp.tagext.*;
44
45
46 /**
47  * A custom tag used to parse and loop through Google search results.
48  *
49  * @author Erik C. Thauvin
50  * @created April 25, 2002
51  * @version $Revision: 1.1.1.1 $
52  * @since 1.0
53  */

54 public class SearchResult extends BodyTagSupport
55 {
56     private GoogleSearchBean bean = null;
57     private StringBuffer JavaDoc output = null;
58     private int loopCount = 0;
59
60     /**
61      * doAfterBody method.
62      *
63      * @return EVAL_BODY_TAG or SKIP_BODY.
64      * @exception JspTagException
65      */

66     public int doAfterBody()
67                     throws JspTagException
68     {
69         // Get the body content
70
final String JavaDoc body = bodyContent.getString();
71
72         // Append the body to the ouput
73
output.append(body);
74
75         try
76         {
77             // Clear the body content
78
bodyContent.clear();
79         }
80         catch (java.io.IOException JavaDoc e)
81         {
82             throw TagUtility.nestedException("An error occurred while clearing the content of the 'searchResult' tag.",
83                                              e);
84         }
85
86         // Increment the loop count, if there are more elements
87
if (++loopCount < bean.getResultElementsCount())
88         {
89             return EVAL_BODY_TAG;
90         }
91
92         return SKIP_BODY;
93     }
94
95     /**
96      * doEndTag method.
97      *
98      * @return EVAL_PAGE.
99      * @exception JspTagException
100      */

101     public int doEndTag()
102                  throws JspTagException
103     {
104         // Is there anything to ouput?
105
if (output.length() > 0)
106         {
107             try
108             {
109                 // Write the output into the tag body
110
bodyContent.getEnclosingWriter().write(output.toString());
111             }
112             catch (java.io.IOException JavaDoc e)
113             {
114                 throw TagUtility.outputError("searchResult", e);
115             }
116         }
117
118         // Reset the values
119
reset();
120
121         return EVAL_PAGE;
122     }
123
124     /**
125      * doStartTag method.
126      *
127      * @return EVAL_BODY_TAG.
128      * @exception JspException
129      */

130     public int doStartTag()
131                    throws JspException
132     {
133         output = new StringBuffer JavaDoc();
134
135         // Reset the loopCount
136
loopCount = 0;
137
138         // Get the Google bean
139
bean = TagUtility.getGoogleSearchBean(pageContext);
140
141         // Are there any elements?
142
if ((bean != null) && (bean.getResultElementsCount() > 0))
143         {
144             return EVAL_BODY_TAG;
145         }
146
147         return SKIP_BODY;
148     }
149
150     /**
151      * Release method.
152      */

153     public void release()
154     {
155         super.release();
156
157         // Reset the bean
158
bean = null;
159
160         // Reset the output
161
output = null;
162
163         // Reset the values
164
reset();
165     }
166
167     /**
168      * Returns the current search result element specified property.
169      *
170      * @param name The property name.
171      * @return The property value.
172      */

173     protected String JavaDoc getElementProperty(String JavaDoc name)
174     {
175         if (bean != null)
176         {
177             return bean.getResultElementProperty(loopCount, name);
178         }
179
180         return "";
181     }
182
183     /**
184      * Reset the values.
185      */

186     protected void reset()
187     {
188         // Reset the loop count value
189
loopCount = 0;
190     }
191 }
192
Popular Tags