KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)Element.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: Element.java,v 1.1.1.1 2003/09/24 14:59:01 ethauvin Exp $
35  *
36  */

37 package net.thauvin.google.taglibs;
38
39 import net.thauvin.google.TagUtility;
40
41 import javax.servlet.jsp.*;
42
43
44 /**
45  * A custom tag used to retrieve a Google search result element.
46  *
47  * @author Erik C. Thauvin
48  * @created April 25, 2002
49  * @version $Revision: 1.1.1.1 $
50  * @since 1.0
51  */

52 public class Element extends StyleSupport
53 {
54     private SearchResult parentTag = null;
55     private String JavaDoc name = null;
56
57     /**
58      * Sets the search result element name.
59      *
60      * @param name The name of the element.
61      */

62     public final void setName(String JavaDoc name)
63     {
64         this.name = name;
65     }
66
67     /**
68      * doAfterBody method.
69      *
70      * @return EVAL_PAGE
71      * @exception JspException
72      */

73     public int doEndTag()
74                  throws JspException
75     {
76         if (TagUtility.isValidString(name, true))
77         {
78             try
79             {
80                 String JavaDoc property = "";
81
82                 int sep = name.indexOf('-');
83
84                 if (sep != -1)
85                 {
86                     String JavaDoc url =
87                         parentTag.getElementProperty(name.substring(sep + 1));
88                     String JavaDoc body =
89                         parentTag.getElementProperty(name.substring(0, sep));
90
91                     if (body.length() == 0)
92                     {
93                         body = url;
94                     }
95
96                     property =
97                         TagUtility.buildRefLink(url, body, target, style, css);
98                 }
99                 else
100                 {
101                     property = parentTag.getElementProperty(name);
102                 }
103
104                 pageContext.getOut().write(property);
105             }
106             catch (Exception JavaDoc e)
107             {
108                 throw TagUtility.outputError("result", e);
109             }
110         }
111
112         // Reset the values
113
reset();
114
115         return EVAL_PAGE;
116     }
117
118     /**
119      * doStartTag method.
120      *
121      * @return SKIP_BODY.
122      * @exception JspTagException
123      */

124     public int doStartTag()
125                    throws JspTagException
126     {
127         // Find the parent tag
128
parentTag =
129             (SearchResult)findAncestorWithClass(this, SearchResult.class);
130
131         // Is it valid?
132
if (parentTag == null)
133         {
134             throw TagUtility.misplacedError("element", "searchResult");
135         }
136
137         return (SKIP_BODY);
138     }
139
140     /**
141      * Release method.
142      */

143     public void release()
144     {
145         super.release();
146
147         // Reset the parent tag
148
parentTag = null;
149     }
150
151     /**
152      * Reset the values.
153      */

154     protected void reset()
155     {
156         super.reset();
157     }
158 }
159
Popular Tags