KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > jag > taglib > VariableTag


1 /* Copyright (C) 2003 Finalist IT Group
2  *
3  * This file is part of JAG - the Java J2EE Application Generator
4  *
5  * JAG is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * JAG is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with JAG; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */

17
18 package com.finalist.jag.taglib;
19
20
21 import com.finalist.jag.*;
22
23 import java.util.*;
24
25
26 /**
27  * This tag allows you to specify the value of a run-time 'variable' by evaluating other JAG tags, and then use that
28  * variable as a parameter to further tags. For example you can have a <code>&lt;jag:equals&gt;</code> tag where you check if
29  * a property is equal to some <b>static</b> string, but if you want to check if it's equal to the run-time output of a
30  * <code>&lt;jag:write&gt;</code> tag, you're screwed!
31  * <p>
32  * Using this tag, you could prepare a variable with the output of the <code>&lt;jag:write&gt;</code> tag, and then use
33  * that variable in the <code>&lt;jag:equals&gt;</code> tag.
34  * <p>
35  * The <code>list</code> parameter may be set to "true" to enable the variable to be built up from a list of values.
36  * For example, the following would output the body content only if the property 'beanProperty' of bean 'someBean'
37  * was equal to "one" or "two":
38  * <p><code>
39  * &lt;jag:variable name="myVariable" list="true"&gt;one&lt;/jag:variable&gt;<br>
40  * &lt;jag:variable name="myVariable" list="true"&gt;two&lt;/jag:variable&gt;<br>
41  * &lt;jag:equal name="someBean" property="beanProperty" variable="myVariable"&gt;<br>
42  * ..body content..<br>
43  * &lt;/jag:equal%gt;
44  *
45  * @author Michael O'Connor - Finalist IT Group
46  */

47 public class VariableTag extends TagBodySupport {
48
49    /** Field name */
50    private String JavaDoc name = null;
51
52    private String JavaDoc list;
53
54
55    /**
56     * Method getName
57     *
58     *
59     * @return
60     *
61     */

62    public String JavaDoc getName() {
63       return (this.name);
64    }
65
66    /**
67     * Method setName
68     *
69     *
70     * @param name
71     *
72     */

73    public void setName(String JavaDoc name) {
74       this.name = name;
75    }
76
77    public String JavaDoc getList() {
78       return list;
79    }
80
81    public void setList(String JavaDoc list) {
82       this.list = list;
83    }
84
85
86    /**
87     * Method doStartTag
88     *
89     *
90     * @return
91     *
92     * @throws JagException
93     *
94     */

95    public int doStartTag() throws JagException {
96       return (EVAL_PAGE);
97    }
98
99    /**
100     * A closing <code>&lt;/jag:variable&gt;</code> tag is processed twice; first time the body text contains the
101     * <code>&lt;jag:variable&gt;</code> start and end tags, along with the stuff we want to evaluate - so we strip
102     * away the <code>&lt;jag:variable&gt;</code> start and end tags and resubmit for processing.
103     * The second time, the body text contains the evaluated body - so we put this on the PageSession and we're done.
104     *
105     * @return a return value from TagConstants.
106     *
107     * @throws JagException
108     *
109     */

110    public int doAfterBodyTag() throws JagException {
111       Object JavaDoc result;
112       String JavaDoc body = getBodyText();
113       int opentagPos = body.indexOf("jag:variable");
114       int closetagPos = body.indexOf("/jag:variable");
115       if (opentagPos != -1 && closetagPos != -1) {
116          //the body text contains the jag:variable start and end tags, go reprocess!
117
body = body.substring(opentagPos + 2, closetagPos - 1);
118          setBodyText(body);
119          return EVAL_BODY_TAG;
120
121       } else if (opentagPos != -1 && closetagPos == -1) {
122          //this is the case of something like <jag:variable name="check" />,
123
//where there is no end tag and no body content to evaluate - so just set a token value.
124
result = "true";
125
126       } else {
127          Set previous = null;
128          if (isList()) {
129             previous = ((Set) getPageContext().getAttribute(name));
130             if (previous == null) {
131                previous = new HashSet();
132             }
133             previous.add(body.trim());
134          }
135
136          if (previous == null) {
137             result = body.trim();
138          } else {
139             result = previous;
140          }
141       }
142       getPageContext().setAttribute(name, result);
143       return SKIP_CLEAR_BODY;
144    }
145
146    private boolean isList() {
147       return list != null && "true".equalsIgnoreCase(list.trim());
148    }
149 }
Popular Tags