1 16 17 package org.apache.taglibs.standard.tlv; 18 19 import java.util.Set ; 20 import java.util.Stack ; 21 22 import javax.servlet.jsp.tagext.PageData ; 23 import javax.servlet.jsp.tagext.ValidationMessage ; 24 25 import org.apache.taglibs.standard.resources.Resources; 26 import org.xml.sax.Attributes ; 27 import org.xml.sax.helpers.DefaultHandler ; 28 29 42 public class JstlFmtTLV extends JstlBaseTLV { 43 44 47 67 68 69 72 private final String SETLOCALE = "setLocale"; 74 private final String SETBUNDLE = "setBundle"; 75 private final String SETTIMEZONE = "setTimeZone"; 76 private final String BUNDLE = "bundle"; 77 private final String MESSAGE = "message"; 78 private final String MESSAGE_PARAM = "param"; 79 private final String FORMAT_NUMBER = "formatNumber"; 80 private final String PARSE_NUMBER = "parseNumber"; 81 private final String PARSE_DATE = "parseDate"; 82 private final String JSP_TEXT = "jsp:text"; 84 85 private final String EVAL = "evaluator"; 87 private final String MESSAGE_KEY = "key"; 88 private final String BUNDLE_PREFIX = "prefix"; 89 private final String VALUE = "value"; 90 91 92 public ValidationMessage [] validate( 95 String prefix, String uri, PageData page) { 96 return super.validate( TYPE_FMT, prefix, uri, page ); 97 } 98 99 100 103 protected DefaultHandler getHandler() { 104 return new Handler (); 105 } 106 107 108 111 112 private class Handler extends DefaultHandler { 113 114 private int depth = 0; 116 private Stack messageDepths = new Stack (); 117 private String lastElementName = null; 118 private boolean bodyNecessary = false; 119 private boolean bodyIllegal = false; 120 121 public void startElement( 123 String ns, String ln, String qn, Attributes a) { 124 125 if (ln == null) 127 ln = getLocalPart(qn); 128 129 if (qn.equals(JSP_TEXT)) 132 return; 133 134 if (bodyIllegal) 136 fail(Resources.getMessage("TLV_ILLEGAL_BODY", 137 lastElementName)); 138 139 Set expAtts; 141 if (qn.startsWith(prefix + ":") 142 && (expAtts = (Set ) config.get(ln)) != null) { 143 for (int i = 0; i < a.getLength(); i++) { 144 String attName = a.getLocalName(i); 145 if (expAtts.contains(attName)) { 146 String vMsg = 147 validateExpression( 148 ln, 149 attName, 150 a.getValue(i)); 151 if (vMsg != null) 152 fail(vMsg); 153 } 154 } 155 } 156 157 if (qn.startsWith(prefix + ":") && !hasNoInvalidScope(a)) 159 fail(Resources.getMessage("TLV_INVALID_ATTRIBUTE", 160 SCOPE, qn, a.getValue(SCOPE))); 161 if (qn.startsWith(prefix + ":") && hasEmptyVar(a)) 162 fail(Resources.getMessage("TLV_EMPTY_VAR", qn)); 163 if (qn.startsWith(prefix + ":") 164 && !isFmtTag(ns, ln, SETLOCALE) 165 && !isFmtTag(ns, ln, SETBUNDLE) 166 && !isFmtTag(ns, ln, SETTIMEZONE) 167 && hasDanglingScope(a)) 168 fail(Resources.getMessage("TLV_DANGLING_SCOPE", qn)); 169 170 181 if (isFmtTag(ns, ln, MESSAGE_PARAM) && messageDepths.empty()) { 182 fail(Resources.getMessage("PARAM_OUTSIDE_MESSAGE")); 183 } 184 185 187 if (isFmtTag(ns, ln, MESSAGE)) { 189 messageDepths.push(new Integer (depth)); 190 } 191 192 bodyIllegal = false; 194 bodyNecessary = false; 195 if (isFmtTag(ns, ln, MESSAGE_PARAM) 196 || isFmtTag(ns, ln, FORMAT_NUMBER) 197 || isFmtTag(ns, ln, PARSE_NUMBER) 198 || isFmtTag(ns, ln, PARSE_DATE)) { 199 if (hasAttribute(a, VALUE)) 200 bodyIllegal = true; 201 else 202 bodyNecessary = true; 203 } else if (isFmtTag(ns, ln, MESSAGE) 204 && !hasAttribute(a, MESSAGE_KEY)) { 205 bodyNecessary = true; 206 } else if (isFmtTag(ns, ln, BUNDLE) 207 && hasAttribute(a, BUNDLE_PREFIX)) { 208 bodyNecessary = true; 209 } 210 211 lastElementName = qn; 213 lastElementId = a.getValue(JSP, "id"); 214 215 depth++; 217 } 218 219 public void characters(char[] ch, int start, int length) { 220 221 bodyNecessary = false; 223 String s = new String (ch, start, length).trim(); 225 if (s.equals("")) 226 return; 227 228 if (bodyIllegal) 230 fail(Resources.getMessage("TLV_ILLEGAL_BODY", 231 lastElementName)); 232 } 233 234 public void endElement(String ns, String ln, String qn) { 235 236 if (qn.equals(JSP_TEXT)) 238 return; 239 240 if (bodyNecessary) 242 fail(Resources.getMessage("TLV_MISSING_BODY", 243 lastElementName)); 244 bodyIllegal = false; 246 if (isFmtTag(ns, ln, MESSAGE)) { 248 messageDepths.pop(); 249 } 250 251 depth--; 253 } 254 } 255 } 256 | Popular Tags |