1 16 17 package javax.servlet.jsp.jstl.tlv; 18 19 import java.io.IOException ; 20 import java.util.HashSet ; 21 import java.util.Set ; 22 import java.util.StringTokenizer ; 23 24 import javax.servlet.jsp.tagext.PageData ; 25 import javax.servlet.jsp.tagext.TagLibraryValidator ; 26 import javax.servlet.jsp.tagext.ValidationMessage ; 27 import javax.xml.parsers.ParserConfigurationException ; 28 import javax.xml.parsers.SAXParser ; 29 import javax.xml.parsers.SAXParserFactory ; 30 31 import org.xml.sax.Attributes ; 32 import org.xml.sax.SAXException ; 33 import org.xml.sax.helpers.DefaultHandler ; 34 35 49 public class PermittedTaglibsTLV extends TagLibraryValidator { 50 51 54 private final String PERMITTED_TAGLIBS_PARAM = "permittedTaglibs"; 56 57 private final String JSP_ROOT_URI = "http://java.sun.com/JSP/Page"; 59 60 private final String JSP_ROOT_NAME = "root"; 62 63 private final String JSP_ROOT_QN = "jsp:root"; 65 66 67 70 private Set permittedTaglibs; private boolean failed; private String uri; 74 77 public PermittedTaglibsTLV() { 78 super(); 79 init(); 80 } 81 82 private void init() { 83 permittedTaglibs = null; 84 } 85 86 public void release() { 87 super.release(); 88 init(); 89 } 90 91 92 95 public synchronized ValidationMessage [] validate( 96 String prefix, String uri, PageData page) { 97 try { 98 99 this.uri = uri; 101 permittedTaglibs = readConfiguration(); 102 103 DefaultHandler h = new PermittedTaglibsHandler(); 105 106 SAXParserFactory f = SAXParserFactory.newInstance(); 108 f.setValidating(true); 109 SAXParser p = f.newSAXParser(); 110 p.parse(page.getInputStream(), h); 111 112 if (failed) 113 return vmFromString( 114 "taglib " + prefix + " (" + uri + ") allows only the " 115 + "following taglibs to be imported: " + permittedTaglibs); 116 else 117 return null; 118 119 } catch (SAXException ex) { 120 return vmFromString(ex.toString()); 121 } catch (ParserConfigurationException ex) { 122 return vmFromString(ex.toString()); 123 } catch (IOException ex) { 124 return vmFromString(ex.toString()); 125 } 126 } 127 128 129 132 133 private Set readConfiguration() { 134 135 Set s = new HashSet (); 137 138 String uris = (String ) getInitParameters().get(PERMITTED_TAGLIBS_PARAM); 140 141 StringTokenizer st = new StringTokenizer (uris); 143 while (st.hasMoreTokens()) 144 s.add(st.nextToken()); 145 146 return s; 148 149 } 150 151 private ValidationMessage [] vmFromString(String message) { 153 return new ValidationMessage [] { 154 new ValidationMessage (null, message) 155 }; 156 } 157 158 159 162 163 private class PermittedTaglibsHandler extends DefaultHandler { 164 165 public void startElement( 167 String ns, String ln, String qn, Attributes a) { 168 169 if (!qn.equals(JSP_ROOT_QN) && 171 (!ns.equals(JSP_ROOT_URI) || !ln.equals(JSP_ROOT_NAME))) 172 return; 173 174 for (int i = 0; i < a.getLength(); i++) { 176 String name = a.getQName(i); 177 178 if (!name.startsWith("xmlns:") || name.equals("xmlns:jsp")) 180 continue; 181 182 String value = a.getValue(i); 183 if (value.equals(uri)) 185 continue; 186 187 if (!permittedTaglibs.contains(value)) 189 failed = true; 190 } 191 } 192 } 193 194 } 195 | Popular Tags |