1 18 19 20 package org.apache.struts.taglib.logic; 21 22 23 import javax.servlet.http.Cookie ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.jsp.JspException ; 26 import org.apache.struts.taglib.TagUtils; 27 28 29 35 36 public class MatchTag extends ConditionalTagBase { 37 38 39 41 42 46 protected String location = 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 57 61 protected String value = null; 62 63 public String getValue() { 64 return (this.value); 65 } 66 67 public void setValue(String value) { 68 this.value = value; 69 } 70 71 72 74 75 78 public void release() { 79 80 super.release(); 81 location = null; 82 value = null; 83 84 } 85 86 87 89 90 98 protected boolean condition() throws JspException { 99 100 return (condition(true)); 101 102 } 103 104 105 115 protected boolean condition(boolean desired) throws JspException { 116 117 String variable = null; 119 if (cookie != null) { 120 Cookie cookies[] = 121 ((HttpServletRequest ) pageContext.getRequest()). 122 getCookies(); 123 if (cookies == null) 124 cookies = new Cookie [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 ) pageContext.getRequest()). 134 getHeader(header); 135 } else if (name != null) { 136 Object 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 e = new JspException 144 (messages.getMessage("logic.selector")); 145 TagUtils.getInstance().saveException(pageContext, e); 146 throw e; 147 } 148 if (variable == null) { 149 JspException e = new JspException 150 (messages.getMessage("logic.variable", value)); 151 TagUtils.getInstance().saveException(pageContext, e); 152 throw e; 153 } 154 155 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 e = new JspException 165 (messages.getMessage("logic.location", location)); 166 TagUtils.getInstance().saveException(pageContext, e); 167 throw e; 168 } 169 170 return (matched == desired); 172 173 } 174 175 176 } 177 | Popular Tags |