KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > servlet > jsp > jstl > tlv > PermittedTaglibsTLV


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package javax.servlet.jsp.jstl.tlv;
18
19 import java.io.IOException JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Set JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
23
24 import javax.servlet.jsp.tagext.PageData JavaDoc;
25 import javax.servlet.jsp.tagext.TagLibraryValidator JavaDoc;
26 import javax.servlet.jsp.tagext.ValidationMessage JavaDoc;
27 import javax.xml.parsers.ParserConfigurationException JavaDoc;
28 import javax.xml.parsers.SAXParser JavaDoc;
29 import javax.xml.parsers.SAXParserFactory JavaDoc;
30
31 import org.xml.sax.Attributes JavaDoc;
32 import org.xml.sax.SAXException JavaDoc;
33 import org.xml.sax.helpers.DefaultHandler JavaDoc;
34
35 /**
36  * <p>A TagLibraryValidator class to allow a TLD to restrict what
37  * taglibs (in addition to itself) may be imported on a page where it's
38  * used.</p>
39  *
40  * <p>This TLV supports the following initialization parameter:</p>
41  * <ul>
42  * <li><b>permittedTaglibs</b>: A whitespace-separated list of URIs corresponding
43  * to tag libraries permitted to be imported on the page in addition to the tag
44  * library that references PermittedTaglibsTLV (which is allowed implicitly).
45  * </ul>
46  *
47  * @author Shawn Bayern
48  */

49 public class PermittedTaglibsTLV extends TagLibraryValidator JavaDoc {
50
51     //*********************************************************************
52
// Constants
53

54     // parameter names
55
private final String JavaDoc PERMITTED_TAGLIBS_PARAM = "permittedTaglibs";
56
57     // URI for "<jsp:root>" element
58
private final String JavaDoc JSP_ROOT_URI = "http://java.sun.com/JSP/Page";
59
60     // local name of "<jsp:root>" element
61
private final String JavaDoc JSP_ROOT_NAME = "root";
62
63     // QName for "<jsp:root>" element
64
private final String JavaDoc JSP_ROOT_QN = "jsp:root";
65
66
67     //*********************************************************************
68
// Validation and configuration state (protected)
69

70     private Set JavaDoc permittedTaglibs; // what URIs are allowed?
71
private boolean failed; // did the page fail?
72
private String JavaDoc uri; // our taglib's URI
73

74     //*********************************************************************
75
// Constructor and lifecycle management
76

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     //*********************************************************************
93
// Validation entry point
94

95     public synchronized ValidationMessage JavaDoc[] validate(
96         String JavaDoc prefix, String JavaDoc uri, PageData JavaDoc page) {
97     try {
98
99         // initialize
100
this.uri = uri;
101         permittedTaglibs = readConfiguration();
102
103         // get a handler
104
DefaultHandler JavaDoc h = new PermittedTaglibsHandler();
105
106         // parse the page
107
SAXParserFactory JavaDoc f = SAXParserFactory.newInstance();
108         f.setValidating(true);
109         SAXParser JavaDoc 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 JavaDoc ex) {
120         return vmFromString(ex.toString());
121     } catch (ParserConfigurationException JavaDoc ex) {
122         return vmFromString(ex.toString());
123     } catch (IOException JavaDoc ex) {
124         return vmFromString(ex.toString());
125     }
126     }
127
128
129     //*********************************************************************
130
// Utility functions
131

132     /** Returns Set of permitted taglibs, based on configuration data. */
133     private Set JavaDoc readConfiguration() {
134
135     // initialize the Set
136
Set JavaDoc s = new HashSet JavaDoc();
137
138     // get the space-separated list of taglibs
139
String JavaDoc uris = (String JavaDoc) getInitParameters().get(PERMITTED_TAGLIBS_PARAM);
140
141         // separate the list into individual uris and store them
142
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(uris);
143         while (st.hasMoreTokens())
144         s.add(st.nextToken());
145
146     // return the new Set
147
return s;
148
149     }
150
151     // constructs a ValidationMessage[] from a single String and no ID
152
private ValidationMessage JavaDoc[] vmFromString(String JavaDoc message) {
153     return new ValidationMessage JavaDoc[] {
154         new ValidationMessage JavaDoc(null, message)
155     };
156     }
157
158
159     //*********************************************************************
160
// SAX handler
161

162     /** The handler that provides the base of our implementation. */
163     private class PermittedTaglibsHandler extends DefaultHandler JavaDoc {
164
165         // if the element is <jsp:root>, check its "xmlns:" attributes
166
public void startElement(
167                 String JavaDoc ns, String JavaDoc ln, String JavaDoc qn, Attributes JavaDoc a) {
168
169         // ignore all but <jsp:root>
170
if (!qn.equals(JSP_ROOT_QN) &&
171                 (!ns.equals(JSP_ROOT_URI) || !ln.equals(JSP_ROOT_NAME)))
172         return;
173
174         // for <jsp:root>, check the attributes
175
for (int i = 0; i < a.getLength(); i++) {
176         String JavaDoc name = a.getQName(i);
177
178         // ignore non-namespace attributes, and xmlns:jsp
179
if (!name.startsWith("xmlns:") || name.equals("xmlns:jsp"))
180             continue;
181
182         String JavaDoc value = a.getValue(i);
183         // ignore our own namespace declaration
184
if (value.equals(uri))
185             continue;
186
187         // otherwise, ensure that 'value' is in 'permittedTaglibs' set
188
if (!permittedTaglibs.contains(value))
189             failed = true;
190         }
191     }
192     }
193
194 }
195
Popular Tags