1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts.taglib.logic;
19
20 import org.apache.struts.taglib.TagUtils;
21
22 import javax.servlet.http.Cookie;
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.jsp.JspException;
25
26 /***
27 * Evalute the nested body content of this tag if the specified value is a
28 * substring of the specified variable.
29 *
30 * @version $Rev: 376842 $ $Date: 2004-10-16 12:38:42 -0400 (Sat, 16 Oct 2004)
31 * $
32 */
33 public class MatchTag extends ConditionalTagBase {
34
35
36 /***
37 * The location where the match must exist (<code>start</code> or
38 * <code>end</code>), or <code>null</code> for anywhere.
39 */
40 protected String location = null;
41
42 /***
43 * The value to which the variable specified by other attributes of this
44 * tag will be matched.
45 */
46 protected String value = null;
47
48 public String getLocation() {
49 return (this.location);
50 }
51
52 public void setLocation(String location) {
53 this.location = location;
54 }
55
56 public String getValue() {
57 return (this.value);
58 }
59
60 public void setValue(String value) {
61 this.value = value;
62 }
63
64
65
66 /***
67 * Release all allocated resources.
68 */
69 public void release() {
70 super.release();
71 location = null;
72 value = null;
73 }
74
75
76
77 /***
78 * Evaluate the condition that is being tested by this particular tag, and
79 * return <code>true</code> if the nested body content of this tag should
80 * be evaluated, or <code>false</code> if it should be skipped. This
81 * method must be implemented by concrete subclasses.
82 *
83 * @throws JspException if a JSP exception occurs
84 */
85 protected boolean condition()
86 throws JspException {
87 return (condition(true));
88 }
89
90 /***
91 * Evaluate the condition that is being tested by this particular tag, and
92 * return <code>true</code> if the nested body content of this tag should
93 * be evaluated, or <code>false</code> if it should be skipped. This
94 * method must be implemented by concrete subclasses.
95 *
96 * @param desired Desired value for a true result
97 * @throws JspException if a JSP exception occurs
98 */
99 protected boolean condition(boolean desired)
100 throws JspException {
101
102 String variable = null;
103
104 if (cookie != null) {
105 Cookie[] cookies =
106 ((HttpServletRequest) pageContext.getRequest()).getCookies();
107
108 if (cookies == null) {
109 cookies = new Cookie[0];
110 }
111
112 for (int i = 0; i < cookies.length; i++) {
113 if (cookie.equals(cookies[i].getName())) {
114 variable = cookies[i].getValue();
115
116 break;
117 }
118 }
119 } else if (header != null) {
120 variable =
121 ((HttpServletRequest) pageContext.getRequest()).getHeader(header);
122 } else if (name != null) {
123 Object value =
124 TagUtils.getInstance().lookup(pageContext, name, property, scope);
125
126 if (value != null) {
127 variable = value.toString();
128 }
129 } else if (parameter != null) {
130 variable = pageContext.getRequest().getParameter(parameter);
131 } else {
132 JspException e =
133 new JspException(messages.getMessage("logic.selector"));
134
135 TagUtils.getInstance().saveException(pageContext, e);
136 throw e;
137 }
138
139 if (variable == null) {
140 JspException e =
141 new JspException(messages.getMessage("logic.variable", value));
142
143 TagUtils.getInstance().saveException(pageContext, e);
144 throw e;
145 }
146
147
148 boolean matched = false;
149
150 if (location == null) {
151 matched = (variable.indexOf(value) >= 0);
152 } else if (location.equals("start")) {
153 matched = variable.startsWith(value);
154 } else if (location.equals("end")) {
155 matched = variable.endsWith(value);
156 } else {
157 JspException e =
158 new JspException(messages.getMessage("logic.location", location));
159
160 TagUtils.getInstance().saveException(pageContext, e);
161 throw e;
162 }
163
164
165 return (matched == desired);
166 }
167 }