1 16 17 package javax.servlet.jsp.jstl.tlv; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.util.Map ; 22 23 import javax.servlet.jsp.tagext.PageData ; 24 import javax.servlet.jsp.tagext.TagLibraryValidator ; 25 import javax.servlet.jsp.tagext.ValidationMessage ; 26 import javax.xml.parsers.ParserConfigurationException ; 27 import javax.xml.parsers.SAXParser ; 28 import javax.xml.parsers.SAXParserFactory ; 29 30 import org.xml.sax.Attributes ; 31 import org.xml.sax.SAXException ; 32 import org.xml.sax.helpers.DefaultHandler ; 33 34 56 public class ScriptFreeTLV extends TagLibraryValidator { 57 private boolean allowDeclarations = false; 58 private boolean allowScriptlets = false; 59 private boolean allowExpressions = false; 60 private boolean allowRTExpressions = false; 61 private SAXParserFactory factory; 62 63 68 public ScriptFreeTLV () { 69 factory = SAXParserFactory.newInstance(); 70 factory.setValidating(false); 71 factory.setNamespaceAware(true); 72 } 73 74 79 public void setInitParameters (Map initParms) { 80 super.setInitParameters(initParms); 81 String declarationsParm = (String ) initParms.get("allowDeclarations"); 82 String scriptletsParm = (String ) initParms.get("allowScriptlets"); 83 String expressionsParm = (String ) initParms.get("allowExpressions"); 84 String rtExpressionsParm = (String ) initParms.get("allowRTExpressions"); 85 86 allowDeclarations = "true".equalsIgnoreCase(declarationsParm); 87 allowScriptlets = "true".equalsIgnoreCase(scriptletsParm); 88 allowExpressions = "true".equalsIgnoreCase(expressionsParm); 89 allowRTExpressions = "true".equalsIgnoreCase(rtExpressionsParm); 90 } 91 92 103 public ValidationMessage [] validate 104 (String prefix, String uri, PageData page) { 105 InputStream in = null; 106 SAXParser parser; 107 MyContentHandler handler = new MyContentHandler(); 108 try { 109 synchronized (factory) { 110 parser = factory.newSAXParser(); 111 } 112 in = page.getInputStream(); 113 parser.parse(in, handler); 114 } 115 catch (ParserConfigurationException e) { 116 return vmFromString(e.toString()); 117 } 118 catch (SAXException e) { 119 return vmFromString(e.toString()); 120 } 121 catch (IOException e) { 122 return vmFromString(e.toString()); 123 } 124 finally { 125 if (in != null) try { in.close(); } catch (IOException e) {} 126 } 127 return handler.reportResults(); 128 } 129 130 135 private class MyContentHandler extends DefaultHandler { 136 private int declarationCount = 0; 137 private int scriptletCount = 0; 138 private int expressionCount = 0; 139 private int rtExpressionCount = 0; 140 141 152 public void startElement (String namespaceUri, 153 String localName, String qualifiedName, 154 Attributes atts) { 155 if ((! allowDeclarations) 156 && qualifiedName.equals("jsp:declaration")) 157 ++declarationCount; 158 else if ((! allowScriptlets) 159 && qualifiedName.equals("jsp:scriptlet")) 160 ++scriptletCount; 161 else if ((! allowExpressions) 162 && qualifiedName.equals("jsp:expression")) 163 ++expressionCount; 164 if (! allowRTExpressions) countRTExpressions(atts); 165 } 166 167 174 private void countRTExpressions (Attributes atts) { 175 int stop = atts.getLength(); 176 for (int i = 0; i < stop; ++i) { 177 String attval = atts.getValue(i); 178 if (attval.startsWith("%=") && attval.endsWith("%")) 179 ++rtExpressionCount; 180 } 181 } 182 183 197 public ValidationMessage [] reportResults () { 198 if (declarationCount + scriptletCount + expressionCount 199 + rtExpressionCount > 0) { 200 StringBuffer results = new StringBuffer ("JSP page contains "); 201 boolean first = true; 202 if (declarationCount > 0) { 203 results.append(Integer.toString(declarationCount)); 204 results.append(" declaration"); 205 if (declarationCount > 1) results.append('s'); 206 first = false; 207 } 208 if (scriptletCount > 0) { 209 if (! first) results.append(", "); 210 results.append(Integer.toString(scriptletCount)); 211 results.append(" scriptlet"); 212 if (scriptletCount > 1) results.append('s'); 213 first = false; 214 } 215 if (expressionCount > 0) { 216 if (! first) results.append(", "); 217 results.append(Integer.toString(expressionCount)); 218 results.append(" expression"); 219 if (expressionCount > 1) results.append('s'); 220 first = false; 221 } 222 if (rtExpressionCount > 0) { 223 if (! first) results.append(", "); 224 results.append(Integer.toString(rtExpressionCount)); 225 results.append(" request-time attribute value"); 226 if (rtExpressionCount > 1) results.append('s'); 227 first = false; 228 } 229 results.append("."); 230 return vmFromString(results.toString()); 231 } else { 232 return null; 233 } 234 } 235 } 236 237 238 private static ValidationMessage [] vmFromString(String message) { 240 return new ValidationMessage [] { 241 new ValidationMessage (null, message) 242 }; 243 } 244 245 } 246 | Popular Tags |