001: /*
002: * $Id: MatchTag.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.taglib.logic;
022:
023: import org.apache.struts.taglib.TagUtils;
024:
025: import javax.servlet.http.Cookie;
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.jsp.JspException;
028:
029: /**
030: * Evalute the nested body content of this tag if the specified value is a
031: * substring of the specified variable.
032: *
033: * @version $Rev: 471754 $ $Date: 2004-10-16 12:38:42 -0400 (Sat, 16 Oct 2004)
034: * $
035: */
036: public class MatchTag extends ConditionalTagBase {
037: // ------------------------------------------------------------- Properties
038:
039: /**
040: * The location where the match must exist (<code>start</code> or
041: * <code>end</code>), or <code>null</code> for anywhere.
042: */
043: protected String location = null;
044:
045: /**
046: * The value to which the variable specified by other attributes of this
047: * tag will be matched.
048: */
049: protected String value = null;
050:
051: public String getLocation() {
052: return (this .location);
053: }
054:
055: public void setLocation(String location) {
056: this .location = location;
057: }
058:
059: public String getValue() {
060: return (this .value);
061: }
062:
063: public void setValue(String value) {
064: this .value = value;
065: }
066:
067: // --------------------------------------------------------- Public Methods
068:
069: /**
070: * Release all allocated resources.
071: */
072: public void release() {
073: super .release();
074: location = null;
075: value = null;
076: }
077:
078: // ------------------------------------------------------ Protected Methods
079:
080: /**
081: * Evaluate the condition that is being tested by this particular tag, and
082: * return <code>true</code> if the nested body content of this tag should
083: * be evaluated, or <code>false</code> if it should be skipped. This
084: * method must be implemented by concrete subclasses.
085: *
086: * @throws JspException if a JSP exception occurs
087: */
088: protected boolean condition() throws JspException {
089: return (condition(true));
090: }
091:
092: /**
093: * Evaluate the condition that is being tested by this particular tag, and
094: * return <code>true</code> if the nested body content of this tag should
095: * be evaluated, or <code>false</code> if it should be skipped. This
096: * method must be implemented by concrete subclasses.
097: *
098: * @param desired Desired value for a true result
099: * @throws JspException if a JSP exception occurs
100: */
101: protected boolean condition(boolean desired) throws JspException {
102: // Acquire the specified variable
103: String variable = null;
104:
105: if (cookie != null) {
106: Cookie[] cookies = ((HttpServletRequest) pageContext
107: .getRequest()).getCookies();
108:
109: if (cookies == null) {
110: cookies = new Cookie[0];
111: }
112:
113: for (int i = 0; i < cookies.length; i++) {
114: if (cookie.equals(cookies[i].getName())) {
115: variable = cookies[i].getValue();
116:
117: break;
118: }
119: }
120: } else if (header != null) {
121: variable = ((HttpServletRequest) pageContext.getRequest())
122: .getHeader(header);
123: } else if (name != null) {
124: Object value = TagUtils.getInstance().lookup(pageContext,
125: name, property, scope);
126:
127: if (value != null) {
128: variable = value.toString();
129: }
130: } else if (parameter != null) {
131: variable = pageContext.getRequest().getParameter(parameter);
132: } else {
133: JspException e = new JspException(messages
134: .getMessage("logic.selector"));
135:
136: TagUtils.getInstance().saveException(pageContext, e);
137: throw e;
138: }
139:
140: if (variable == null) {
141: JspException e = new JspException(messages.getMessage(
142: "logic.variable", value));
143:
144: TagUtils.getInstance().saveException(pageContext, e);
145: throw e;
146: }
147:
148: // Perform the comparison requested by the location attribute
149: boolean matched = false;
150:
151: if (location == null) {
152: matched = (variable.indexOf(value) >= 0);
153: } else if (location.equals("start")) {
154: matched = variable.startsWith(value);
155: } else if (location.equals("end")) {
156: matched = variable.endsWith(value);
157: } else {
158: JspException e = new JspException(messages.getMessage(
159: "logic.location", location));
160:
161: TagUtils.getInstance().saveException(pageContext, e);
162: throw e;
163: }
164:
165: // Return the final result
166: return (matched == desired);
167: }
168: }
|