KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > jwi > jgallery > tags > IfTag


1 package de.jwi.jgallery.tags;
2
3 /*
4  * jGallery - Java web application to display image galleries
5  *
6  * Copyright (C) 2004 Juergen Weber
7  *
8  * This file is part of jGallery.
9  *
10  * jGallery is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * jGallery is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with jGallery; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston
23  */

24
25
26 import java.util.Stack JavaDoc;
27
28 import javax.servlet.ServletRequest JavaDoc;
29 import javax.servlet.jsp.JspException JavaDoc;
30 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
31
32 /**
33  * @author Juergen Weber
34  * Source file created on 18.02.2004
35  *
36  */

37 public class IfTag extends BodyTagSupport JavaDoc
38 {
39
40     private String JavaDoc exists = null;
41
42     private String JavaDoc test = null;
43
44     boolean expressionResult;
45
46     public void setExists(String JavaDoc exists)
47     {
48         this.exists = exists;
49     }
50
51     public void setTest(String JavaDoc test)
52     {
53         this.test = test;
54     }
55
56     private void pushResult(boolean result)
57     {
58         ServletRequest JavaDoc request = pageContext.getRequest();
59         Stack JavaDoc stack = (Stack JavaDoc) request.getAttribute("ifThenStack");
60         if (null == stack)
61         {
62             stack = new Stack JavaDoc();
63             request.setAttribute("ifThenStack", stack);
64         }
65         stack.push(new Boolean JavaDoc(result));
66     }
67
68     public int doStartTag() throws JspException JavaDoc
69     {
70         if (null != test)
71         {
72             if ("true".equals(test))
73             {
74                 pushResult(true);
75                 return EVAL_BODY_INCLUDE;
76             }
77             else
78             {
79                 pushResult(false);
80                 return SKIP_BODY;
81             }
82         }
83         else if (null != exists)
84         {
85             if (exists.length()>0 )
86             {
87                 pushResult(true);
88                 return EVAL_BODY_INCLUDE;
89             }
90             else
91             {
92                 pushResult(false);
93                 return SKIP_BODY;
94             }
95             
96         }
97         else
98         {
99             throw new JspException JavaDoc("illegal attribute for tag 'if'");
100         }
101     }
102 }
103
Popular Tags