KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > raptus > owxv3 > libtags > GroupVarsTag


1 /*
2  * eAdmin/OWX
3  * Copyright (C) 1996-2003 OWX-Project Team <owx-team@gmx.net>
4  */

5
6 package com.raptus.owxv3.libtags;
7
8 import java.util.Hashtable JavaDoc;
9
10 import javax.servlet.http.*;
11 import javax.servlet.jsp.*;
12 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
13
14 import com.raptus.owxv3.*;
15
16 /**
17  * <hr>
18  * <table width="100%" border="0">
19  * <tr>
20  * <td width="24%"><b>Filename</b></td><td width="76%">GroupVarsTag.java</td>
21  * </tr>
22  * <tr>
23  * <td width="24%"><b>Author</b></td><td width="76%">Guy Zürcher</td>
24  * </tr>
25  * <tr>
26  * <td width="24%"><b>Date</b></td><td width="76%">25th of June 2003</td>
27  * </tr>
28  * </table>
29  * <hr>
30  * <table width="100%" border="0">
31  * <tr>
32  * <td width="24%"><b>Date / Author</b></td><td width="76%"><b>Changes</b></td>
33  * </tr>
34  * </table>
35  * <hr>
36  */

37 public class GroupVarsTag extends TagSupport JavaDoc
38 {
39     private final String JavaDoc GROUP_VARS_KEY = "com.raptus.owxv3.GROUP_VARS_KEY";
40
41     /**
42      * All "var"s are grouped.
43      */

44     protected String JavaDoc group;
45     public void setGroup(String JavaDoc s) { group = s; }
46     public String JavaDoc getGroup() { return group; }
47
48     /**
49      * Unike ID
50      */

51     protected String JavaDoc var;
52     public void setVar(String JavaDoc s) { var = s; }
53     public String JavaDoc getVar() { return var; }
54
55     /**
56      * Is the "value" of an active "var" or the default, if the
57      * "var" is not active.
58      */

59     protected String JavaDoc value;
60     public void setValue(String JavaDoc s) { value = s; }
61     public String JavaDoc getValue() { return value; }
62
63     /**
64      * Determines what to do.
65      *
66      * Possible values are:
67      * "activate" drop active and set "var" inside "group" to "value" as new active
68      * "getvalue" return value of "var" in session if active, "value" if not
69      * "equals" compare value of "var" if active, with "value". If true JSP Body is used.
70      * "notequals" compare value of "var" if active, with "value". If false JSP Body is used.
71      */

72     protected String JavaDoc action;
73     public void setAction(String JavaDoc s) { action = s; }
74     public String JavaDoc getAction() { return action; }
75     
76     /**
77      * process logic
78      */

79     public int doStartTag() throws JspException
80     {
81         HttpServletRequest req=(HttpServletRequest) pageContext.getRequest();
82         HttpSession session=req.getSession();
83         Hashtable JavaDoc gvlist = getValidGVList(session);
84         boolean considerBody = false;
85         
86         JspWriter out = pageContext.getOut();
87         try
88         {
89             if(action.equalsIgnoreCase("activate"))
90             {
91                 considerBody = processActivate(gvlist);
92             }
93             else if(action.equalsIgnoreCase("getvalue"))
94             {
95                 out.print(processGetValue(gvlist));
96             }
97             else if(action.equalsIgnoreCase("equal"))
98             {
99                 considerBody = processEqual(gvlist, false);
100             }
101             else if(action.equalsIgnoreCase("notequal"))
102             {
103                 considerBody = processEqual(gvlist, true);
104             }
105         }
106         catch(Exception JavaDoc e) {
107             LoggingManager.log("Exception while processing logic." + e.getMessage(), this);
108         }
109         // include the body if required
110
if(considerBody == true)
111         {
112             return (EVAL_BODY_INCLUDE);
113         }
114         else
115         {
116             return (SKIP_BODY);
117         }
118
119     }
120
121     /**
122      * Evaluate the remainder of the current page normally.
123      *
124      * @exception JspException if a JSP exception occurs
125      */

126     public int doEndTag() throws JspException
127     {
128         return (EVAL_PAGE);
129     }
130     
131     /**
132      * Returns an initialized and usable list of grouped vars
133      *
134      * @author gzuercher
135      */

136     protected Hashtable JavaDoc getValidGVList(HttpSession session)
137     {
138         Hashtable JavaDoc gvlist = (Hashtable JavaDoc) session.getAttribute(GROUP_VARS_KEY);
139         if(gvlist == null)
140         {
141             gvlist = new Hashtable JavaDoc();
142             session.setAttribute(GROUP_VARS_KEY, gvlist);
143         }
144
145         return(gvlist);
146     }
147     
148     /**
149      * drop active and set "var" inside "group" to "value" as new active
150      *
151      * @author gzuercher
152      */

153     protected boolean processActivate(Hashtable JavaDoc gvlist)
154     {
155         PairOfObjects po = new PairOfObjects(var, value);
156         gvlist.put(group, po);
157         
158         return false;
159     }
160     
161     /**
162      * return value of "var" in session if active, "value" if not
163      *
164      * @author gzuercher
165      */

166     protected String JavaDoc processGetValue(Hashtable JavaDoc gvlist)
167     {
168         PairOfObjects po = (PairOfObjects) gvlist.get(group); // group
169
if(po != null && ((String JavaDoc) po.getObjectOne()).equals(var)) // var
170
return (String JavaDoc) po.getObjectTwo(); // value
171

172         return value;
173     }
174     
175     /**
176      * compare value of "var" if active, with "value".
177      * The "invers" var determines, if true JSP Body is used.
178      *
179      * @author gzuercher
180      */

181     protected boolean processEqual(Hashtable JavaDoc gvlist, boolean invers)
182     {
183         PairOfObjects po = (PairOfObjects) gvlist.get(group);
184         if(po != null &&
185            ((String JavaDoc) po.getObjectOne()).equals(var) &&
186            ((String JavaDoc) po.getObjectTwo()).equals(value) )
187         {
188             return !invers;
189         }
190         
191         return false;
192     }
193     
194 }//end class
195
Popular Tags