KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > taglib > logic > MatchTag


1 /*
2  * $Id: MatchTag.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19
20 package org.apache.struts.taglib.logic;
21
22
23 import javax.servlet.http.Cookie JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.jsp.JspException JavaDoc;
26 import org.apache.struts.taglib.TagUtils;
27
28
29 /**
30  * Evalute the nested body content of this tag if the specified value
31  * is a substring of the specified variable.
32  *
33  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
34  */

35
36 public class MatchTag extends ConditionalTagBase {
37
38
39     // ------------------------------------------------------------- Properties
40

41
42     /**
43      * The location where the match must exist (<code>start</code> or
44      * <code>end</code>), or <code>null</code> for anywhere.
45      */

46     protected String JavaDoc location = null;
47
48     public String JavaDoc getLocation() {
49         return (this.location);
50     }
51
52     public void setLocation(String JavaDoc location) {
53         this.location = location;
54     }
55
56
57     /**
58      * The value to which the variable specified by other attributes of this
59      * tag will be matched.
60      */

61     protected String JavaDoc value = null;
62
63     public String JavaDoc getValue() {
64         return (this.value);
65     }
66
67     public void setValue(String JavaDoc value) {
68         this.value = value;
69     }
70
71
72     // --------------------------------------------------------- Public Methods
73

74
75     /**
76      * Release all allocated resources.
77      */

78     public void release() {
79
80         super.release();
81         location = null;
82         value = null;
83
84     }
85
86
87     // ------------------------------------------------------ Protected Methods
88

89
90     /**
91      * Evaluate the condition that is being tested by this particular tag,
92      * and return <code>true</code> if the nested body content of this tag
93      * should be evaluated, or <code>false</code> if it should be skipped.
94      * This method must be implemented by concrete subclasses.
95      *
96      * @exception JspException if a JSP exception occurs
97      */

98     protected boolean condition() throws JspException JavaDoc {
99
100         return (condition(true));
101
102     }
103
104
105     /**
106      * Evaluate the condition that is being tested by this particular tag,
107      * and return <code>true</code> if the nested body content of this tag
108      * should be evaluated, or <code>false</code> if it should be skipped.
109      * This method must be implemented by concrete subclasses.
110      *
111      * @param desired Desired value for a true result
112      *
113      * @exception JspException if a JSP exception occurs
114      */

115     protected boolean condition(boolean desired) throws JspException JavaDoc {
116
117         // Acquire the specified variable
118
String JavaDoc variable = null;
119         if (cookie != null) {
120             Cookie JavaDoc cookies[] =
121                 ((HttpServletRequest JavaDoc) pageContext.getRequest()).
122                 getCookies();
123             if (cookies == null)
124                 cookies = new Cookie JavaDoc[0];
125             for (int i = 0; i < cookies.length; i++) {
126                 if (cookie.equals(cookies[i].getName())) {
127                     variable = cookies[i].getValue();
128                     break;
129                 }
130             }
131         } else if (header != null) {
132             variable =
133                 ((HttpServletRequest JavaDoc) pageContext.getRequest()).
134                 getHeader(header);
135         } else if (name != null) {
136             Object JavaDoc value =
137                 TagUtils.getInstance().lookup(pageContext, name, property, scope);
138             if (value != null)
139                 variable = value.toString();
140         } else if (parameter != null) {
141             variable = pageContext.getRequest().getParameter(parameter);
142         } else {
143             JspException JavaDoc e = new JspException JavaDoc
144                 (messages.getMessage("logic.selector"));
145             TagUtils.getInstance().saveException(pageContext, e);
146             throw e;
147         }
148         if (variable == null) {
149             JspException JavaDoc e = new JspException JavaDoc
150                 (messages.getMessage("logic.variable", value));
151             TagUtils.getInstance().saveException(pageContext, e);
152             throw e;
153         }
154
155         // Perform the comparison requested by the location attribute
156
boolean matched = false;
157         if (location == null) {
158             matched = (variable.indexOf(value) >= 0);
159         } else if (location.equals("start")) {
160             matched = variable.startsWith(value);
161         } else if (location.equals("end")) {
162             matched = variable.endsWith(value);
163         } else {
164             JspException JavaDoc e = new JspException JavaDoc
165                 (messages.getMessage("logic.location", location));
166             TagUtils.getInstance().saveException(pageContext, e);
167             throw e;
168         }
169
170         // Return the final result
171
return (matched == desired);
172
173     }
174
175
176 }
177
Popular Tags