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 34 public class JstlSqlTLV extends JstlBaseTLV { 35 36 39 private final String SETDATASOURCE = "setDataSource"; 41 private final String QUERY = "query"; 42 private final String UPDATE = "update"; 43 private final String TRANSACTION = "transaction"; 44 private final String PARAM = "param"; 45 private final String DATEPARAM = "dateParam"; 46 47 private final String JSP_TEXT = "jsp:text"; 48 49 private final String SQL = "sql"; 51 private final String DATASOURCE = "dataSource"; 52 53 54 public ValidationMessage [] validate( 57 String prefix, String uri, PageData page) { 58 return super.validate( TYPE_SQL, prefix, uri, page ); 59 } 60 61 62 65 protected DefaultHandler getHandler() { 66 return new Handler (); 67 } 68 69 70 73 74 private class Handler extends DefaultHandler { 75 76 private int depth = 0; 78 private Stack queryDepths = new Stack (); 79 private Stack updateDepths = new Stack (); 80 private Stack transactionDepths = new Stack (); 81 private String lastElementName = null; 82 private boolean bodyNecessary = false; 83 private boolean bodyIllegal = false; 84 85 public void startElement( 87 String ns, String ln, String qn, Attributes a) { 88 89 if (ln == null) 91 ln = getLocalPart(qn); 92 93 if (qn.equals(JSP_TEXT)) 96 return; 97 98 if (bodyIllegal) 100 fail(Resources.getMessage("TLV_ILLEGAL_BODY", lastElementName)); 101 102 Set expAtts; 104 if (qn.startsWith(prefix + ":") 105 && (expAtts = (Set ) config.get(ln)) != null) { 106 for (int i = 0; i < a.getLength(); i++) { 107 String attName = a.getLocalName(i); 108 if (expAtts.contains(attName)) { 109 String vMsg = 110 validateExpression( 111 ln, 112 attName, 113 a.getValue(i)); 114 if (vMsg != null) 115 fail(vMsg); 116 } 117 } 118 } 119 120 if (qn.startsWith(prefix + ":") && !hasNoInvalidScope(a)) 122 fail(Resources.getMessage("TLV_INVALID_ATTRIBUTE", 123 SCOPE, qn, a.getValue(SCOPE))); 124 if (qn.startsWith(prefix + ":") && hasEmptyVar(a)) 125 fail(Resources.getMessage("TLV_EMPTY_VAR", qn)); 126 if (qn.startsWith(prefix + ":") && hasDanglingScope(a) && 127 !qn.startsWith(prefix + ":" + SETDATASOURCE)) 128 fail(Resources.getMessage("TLV_DANGLING_SCOPE", qn)); 129 130 132 144 if ( (isSqlTag(ns, ln, PARAM) || isSqlTag(ns, ln, DATEPARAM)) 145 && (queryDepths.empty() && updateDepths.empty()) ) { 146 fail(Resources.getMessage("SQL_PARAM_OUTSIDE_PARENT")); 147 } 148 149 if (isSqlTag(ns, ln, QUERY)) { 151 queryDepths.push(new Integer (depth)); 152 } 153 if (isSqlTag(ns, ln, UPDATE)) { 155 updateDepths.push(new Integer (depth)); 156 } 157 if (isSqlTag(ns, ln, TRANSACTION)) { 159 transactionDepths.push(new Integer (depth)); 160 } 161 162 bodyIllegal = false; 164 bodyNecessary = false; 165 166 if (isSqlTag(ns, ln, QUERY) || isSqlTag(ns, ln, UPDATE)) { 167 if (!hasAttribute(a, SQL)) { 168 bodyNecessary = true; 169 } 170 if (hasAttribute(a, DATASOURCE) && !transactionDepths.empty()) { 171 fail(Resources.getMessage("ERROR_NESTED_DATASOURCE")); 172 } 173 } 174 175 if (isSqlTag(ns, ln, DATEPARAM)) { 176 bodyIllegal = true; 177 } 178 179 lastElementName = qn; 181 lastElementId = a.getValue("http://java.sun.com/JSP/Page", "id"); 182 183 depth++; 185 } 186 187 public void characters(char[] ch, int start, int length) { 188 189 bodyNecessary = false; 191 String s = new String (ch, start, length).trim(); 193 if (s.equals("")) 194 return; 195 196 if (bodyIllegal) 198 fail(Resources.getMessage("TLV_ILLEGAL_BODY", lastElementName)); 199 } 200 201 public void endElement(String ns, String ln, String qn) { 202 203 if (qn.equals(JSP_TEXT)) 205 return; 206 207 if (bodyNecessary) 209 fail(Resources.getMessage("TLV_MISSING_BODY", 210 lastElementName)); 211 bodyIllegal = false; 213 if (isSqlTag(ns, ln, QUERY)) { 215 queryDepths.pop(); 216 } 217 if (isSqlTag(ns, ln, UPDATE)) { 219 updateDepths.pop(); 220 } 221 if (isSqlTag(ns, ln, TRANSACTION)) { 223 transactionDepths.pop(); 224 } 225 226 depth--; 228 } 229 } 230 } 231 | Popular Tags |