KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jasper > compiler > JspConfig


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

17
18 package org.apache.jasper.compiler;
19
20 import java.io.InputStream JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Vector JavaDoc;
23 import java.net.URL JavaDoc;
24
25 import javax.servlet.ServletContext JavaDoc;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.jasper.JasperException;
30 import org.apache.jasper.xmlparser.ParserUtils;
31 import org.apache.jasper.xmlparser.TreeNode;
32 import org.xml.sax.InputSource JavaDoc;
33
34 /**
35  * Handles the jsp-config element in WEB_INF/web.xml. This is used
36  * for specifying the JSP configuration information on a JSP page
37  *
38  * @author Kin-man Chung
39  * @author Remy Maucherat
40  */

41
42 public class JspConfig {
43
44     private static final String JavaDoc WEB_XML = "/WEB-INF/web.xml";
45
46     // Logger
47
private Log log = LogFactory.getLog(JspConfig.class);
48
49     private Vector JavaDoc jspProperties = null;
50     private ServletContext JavaDoc ctxt;
51     private boolean initialized = false;
52
53     private String JavaDoc defaultIsXml = null; // unspecified
54
private String JavaDoc defaultIsELIgnored = null; // unspecified
55
private String JavaDoc defaultIsScriptingInvalid = null;
56     private String JavaDoc defaultDeferedSyntaxAllowedAsLiteral = null;
57     private String JavaDoc defaultTrimDirectiveWhitespaces = null;
58     private JspProperty defaultJspProperty;
59
60     public JspConfig(ServletContext JavaDoc ctxt) {
61         this.ctxt = ctxt;
62     }
63
64     private double getVersion(TreeNode webApp) {
65         String JavaDoc v = webApp.findAttribute("version");
66         if (v != null) {
67             try {
68                 return Double.parseDouble(v);
69             } catch (NumberFormatException JavaDoc e) {
70             }
71         }
72         return 2.3;
73     }
74
75     private void processWebDotXml(ServletContext JavaDoc ctxt) throws JasperException {
76
77         InputStream JavaDoc is = null;
78
79         try {
80             URL JavaDoc uri = ctxt.getResource(WEB_XML);
81             if (uri == null) {
82                 // no web.xml
83
return;
84             }
85
86             is = uri.openStream();
87             InputSource JavaDoc ip = new InputSource JavaDoc(is);
88             ip.setSystemId(uri.toExternalForm());
89
90             ParserUtils pu = new ParserUtils();
91             TreeNode webApp = pu.parseXMLDocument(WEB_XML, ip);
92
93             if (webApp == null
94                     || getVersion(webApp) < 2.4) {
95                 defaultIsELIgnored = "true";
96                 return;
97             }
98             TreeNode jspConfig = webApp.findChild("jsp-config");
99             if (jspConfig == null) {
100                 return;
101             }
102
103             jspProperties = new Vector JavaDoc();
104             Iterator JavaDoc jspPropertyList = jspConfig.findChildren("jsp-property-group");
105             while (jspPropertyList.hasNext()) {
106
107                 TreeNode element = (TreeNode) jspPropertyList.next();
108                 Iterator JavaDoc list = element.findChildren();
109
110                 Vector JavaDoc urlPatterns = new Vector JavaDoc();
111                 String JavaDoc pageEncoding = null;
112                 String JavaDoc scriptingInvalid = null;
113                 String JavaDoc elIgnored = null;
114                 String JavaDoc isXml = null;
115                 Vector JavaDoc includePrelude = new Vector JavaDoc();
116                 Vector JavaDoc includeCoda = new Vector JavaDoc();
117                 String JavaDoc deferredSyntaxAllowedAsLiteral = null;
118                 String JavaDoc trimDirectiveWhitespaces = null;
119
120                 while (list.hasNext()) {
121
122                     element = (TreeNode) list.next();
123                     String JavaDoc tname = element.getName();
124
125                     if ("url-pattern".equals(tname))
126                         urlPatterns.addElement( element.getBody() );
127                     else if ("page-encoding".equals(tname))
128                         pageEncoding = element.getBody();
129                     else if ("is-xml".equals(tname))
130                         isXml = element.getBody();
131                     else if ("el-ignored".equals(tname))
132                         elIgnored = element.getBody();
133                     else if ("scripting-invalid".equals(tname))
134                         scriptingInvalid = element.getBody();
135                     else if ("include-prelude".equals(tname))
136                         includePrelude.addElement(element.getBody());
137                     else if ("include-coda".equals(tname))
138                         includeCoda.addElement(element.getBody());
139                     else if ("deferred-syntax-allowed-as-literal".equals(tname))
140                         deferredSyntaxAllowedAsLiteral = element.getBody();
141                     else if ("trim-directive-whitespaces".equals(tname))
142                         trimDirectiveWhitespaces = element.getBody();
143                 }
144
145                 if (urlPatterns.size() == 0) {
146                     continue;
147                 }
148
149                 // Add one JspPropertyGroup for each URL Pattern. This makes
150
// the matching logic easier.
151
for( int p = 0; p < urlPatterns.size(); p++ ) {
152                     String JavaDoc urlPattern = (String JavaDoc)urlPatterns.elementAt( p );
153                     String JavaDoc path = null;
154                     String JavaDoc extension = null;
155
156                     if (urlPattern.indexOf('*') < 0) {
157                         // Exact match
158
path = urlPattern;
159                     } else {
160                         int i = urlPattern.lastIndexOf('/');
161                         String JavaDoc file;
162                         if (i >= 0) {
163                             path = urlPattern.substring(0,i+1);
164                             file = urlPattern.substring(i+1);
165                         } else {
166                             file = urlPattern;
167                         }
168
169                         // pattern must be "*", or of the form "*.jsp"
170
if (file.equals("*")) {
171                             extension = "*";
172                         } else if (file.startsWith("*.")) {
173                             extension = file.substring(file.indexOf('.')+1);
174                         }
175
176                         // The url patterns are reconstructed as the follwoing:
177
// path != null, extension == null: / or /foo/bar.ext
178
// path == null, extension != null: *.ext
179
// path != null, extension == "*": /foo/*
180
boolean isStar = "*".equals(extension);
181                         if ((path == null && (extension == null || isStar))
182                                 || (path != null && !isStar)) {
183                             if (log.isWarnEnabled()) {
184                                 log.warn(Localizer.getMessage(
185                                         "jsp.warning.bad.urlpattern.propertygroup",
186                                         urlPattern));
187                             }
188                             continue;
189                         }
190                     }
191
192                     JspProperty property = new JspProperty(isXml,
193                             elIgnored,
194                             scriptingInvalid,
195                             pageEncoding,
196                             includePrelude,
197                             includeCoda,
198                             deferredSyntaxAllowedAsLiteral,
199                             trimDirectiveWhitespaces);
200                     JspPropertyGroup propertyGroup =
201                         new JspPropertyGroup(path, extension, property);
202
203                     jspProperties.addElement(propertyGroup);
204                 }
205             }
206         } catch (Exception JavaDoc ex) {
207             throw new JasperException(ex);
208         } finally {
209             if (is != null) {
210                 try {
211                     is.close();
212                 } catch (Throwable JavaDoc t) {}
213             }
214         }
215     }
216
217     private void init() throws JasperException {
218
219         if (!initialized) {
220             processWebDotXml(ctxt);
221             defaultJspProperty = new JspProperty(defaultIsXml,
222                     defaultIsELIgnored,
223                     defaultIsScriptingInvalid,
224                     null, null, null, defaultDeferedSyntaxAllowedAsLiteral,
225                     defaultTrimDirectiveWhitespaces);
226             initialized = true;
227         }
228     }
229
230     /**
231      * Select the property group that has more restrictive url-pattern.
232      * In case of tie, select the first.
233      */

234     private JspPropertyGroup selectProperty(JspPropertyGroup prev,
235             JspPropertyGroup curr) {
236         if (prev == null) {
237             return curr;
238         }
239         if (prev.getExtension() == null) {
240             // exact match
241
return prev;
242         }
243         if (curr.getExtension() == null) {
244             // exact match
245
return curr;
246         }
247         String JavaDoc prevPath = prev.getPath();
248         String JavaDoc currPath = curr.getPath();
249         if (prevPath == null && currPath == null) {
250             // Both specifies a *.ext, keep the first one
251
return prev;
252         }
253         if (prevPath == null && currPath != null) {
254             return curr;
255         }
256         if (prevPath != null && currPath == null) {
257             return prev;
258         }
259         if (prevPath.length() >= currPath.length()) {
260             return prev;
261         }
262         return curr;
263     }
264
265
266     /**
267      * Find a property that best matches the supplied resource.
268      * @param uri the resource supplied.
269      * @return a JspProperty indicating the best match, or some default.
270      */

271     public JspProperty findJspProperty(String JavaDoc uri) throws JasperException {
272
273         init();
274
275         // JSP Configuration settings do not apply to tag files
276
if (jspProperties == null || uri.endsWith(".tag")
277                 || uri.endsWith(".tagx")) {
278             return defaultJspProperty;
279         }
280
281         String JavaDoc uriPath = null;
282         int index = uri.lastIndexOf('/');
283         if (index >=0 ) {
284             uriPath = uri.substring(0, index+1);
285         }
286         String JavaDoc uriExtension = null;
287         index = uri.lastIndexOf('.');
288         if (index >=0) {
289             uriExtension = uri.substring(index+1);
290         }
291
292         Vector JavaDoc includePreludes = new Vector JavaDoc();
293         Vector JavaDoc includeCodas = new Vector JavaDoc();
294
295         JspPropertyGroup isXmlMatch = null;
296         JspPropertyGroup elIgnoredMatch = null;
297         JspPropertyGroup scriptingInvalidMatch = null;
298         JspPropertyGroup pageEncodingMatch = null;
299         JspPropertyGroup deferedSyntaxAllowedAsLiteralMatch = null;
300         JspPropertyGroup trimDirectiveWhitespacesMatch = null;
301
302         Iterator JavaDoc iter = jspProperties.iterator();
303         while (iter.hasNext()) {
304
305             JspPropertyGroup jpg = (JspPropertyGroup) iter.next();
306             JspProperty jp = jpg.getJspProperty();
307
308             // (arrays will be the same length)
309
String JavaDoc extension = jpg.getExtension();
310             String JavaDoc path = jpg.getPath();
311
312             if (extension == null) {
313                 // exact match pattern: /a/foo.jsp
314
if (!uri.equals(path)) {
315                     // not matched;
316
continue;
317                 }
318             } else {
319                 // Matching patterns *.ext or /p/*
320
if (path != null && uriPath != null &&
321                         ! uriPath.startsWith(path)) {
322                     // not matched
323
continue;
324                 }
325                 if (!extension.equals("*") &&
326                         !extension.equals(uriExtension)) {
327                     // not matched
328
continue;
329                 }
330             }
331             // We have a match
332
// Add include-preludes and include-codas
333
if (jp.getIncludePrelude() != null) {
334                 includePreludes.addAll(jp.getIncludePrelude());
335             }
336             if (jp.getIncludeCoda() != null) {
337                 includeCodas.addAll(jp.getIncludeCoda());
338             }
339
340             // If there is a previous match for the same property, remember
341
// the one that is more restrictive.
342
if (jp.isXml() != null) {
343                 isXmlMatch = selectProperty(isXmlMatch, jpg);
344             }
345             if (jp.isELIgnored() != null) {
346                 elIgnoredMatch = selectProperty(elIgnoredMatch, jpg);
347             }
348             if (jp.isScriptingInvalid() != null) {
349                 scriptingInvalidMatch =
350                     selectProperty(scriptingInvalidMatch, jpg);
351             }
352             if (jp.getPageEncoding() != null) {
353                 pageEncodingMatch = selectProperty(pageEncodingMatch, jpg);
354             }
355             if (jp.isDeferedSyntaxAllowedAsLiteral() != null) {
356                 deferedSyntaxAllowedAsLiteralMatch =
357                     selectProperty(deferedSyntaxAllowedAsLiteralMatch, jpg);
358             }
359             if (jp.isTrimDirectiveWhitespaces() != null) {
360                 trimDirectiveWhitespacesMatch =
361                     selectProperty(trimDirectiveWhitespacesMatch, jpg);
362             }
363         }
364
365
366         String JavaDoc isXml = defaultIsXml;
367         String JavaDoc isELIgnored = defaultIsELIgnored;
368         String JavaDoc isScriptingInvalid = defaultIsScriptingInvalid;
369         String JavaDoc pageEncoding = null;
370         String JavaDoc isDeferedSyntaxAllowedAsLiteral = defaultDeferedSyntaxAllowedAsLiteral;
371         String JavaDoc isTrimDirectiveWhitespaces = defaultTrimDirectiveWhitespaces;
372
373         if (isXmlMatch != null) {
374             isXml = isXmlMatch.getJspProperty().isXml();
375         }
376         if (elIgnoredMatch != null) {
377             isELIgnored = elIgnoredMatch.getJspProperty().isELIgnored();
378         }
379         if (scriptingInvalidMatch != null) {
380             isScriptingInvalid =
381                 scriptingInvalidMatch.getJspProperty().isScriptingInvalid();
382         }
383         if (pageEncodingMatch != null) {
384             pageEncoding = pageEncodingMatch.getJspProperty().getPageEncoding();
385         }
386         if (deferedSyntaxAllowedAsLiteralMatch != null) {
387             isDeferedSyntaxAllowedAsLiteral =
388                 deferedSyntaxAllowedAsLiteralMatch.getJspProperty().isDeferedSyntaxAllowedAsLiteral();
389         }
390         if (trimDirectiveWhitespacesMatch != null) {
391             isTrimDirectiveWhitespaces =
392                 trimDirectiveWhitespacesMatch.getJspProperty().isTrimDirectiveWhitespaces();
393         }
394
395         return new JspProperty(isXml, isELIgnored, isScriptingInvalid,
396                 pageEncoding, includePreludes, includeCodas,
397                 isDeferedSyntaxAllowedAsLiteral, isTrimDirectiveWhitespaces);
398     }
399
400     /**
401      * To find out if an uri matches an url pattern in jsp config. If so,
402      * then the uri is a JSP page. This is used primarily for jspc.
403      */

404     public boolean isJspPage(String JavaDoc uri) throws JasperException {
405
406         init();
407         if (jspProperties == null) {
408             return false;
409         }
410
411         String JavaDoc uriPath = null;
412         int index = uri.lastIndexOf('/');
413         if (index >=0 ) {
414             uriPath = uri.substring(0, index+1);
415         }
416         String JavaDoc uriExtension = null;
417         index = uri.lastIndexOf('.');
418         if (index >=0) {
419             uriExtension = uri.substring(index+1);
420         }
421
422         Iterator JavaDoc iter = jspProperties.iterator();
423         while (iter.hasNext()) {
424
425             JspPropertyGroup jpg = (JspPropertyGroup) iter.next();
426             JspProperty jp = jpg.getJspProperty();
427
428             String JavaDoc extension = jpg.getExtension();
429             String JavaDoc path = jpg.getPath();
430
431             if (extension == null) {
432                 if (uri.equals(path)) {
433                     // There is an exact match
434
return true;
435                 }
436             } else {
437                 if ((path == null || path.equals(uriPath)) &&
438                         (extension.equals("*") || extension.equals(uriExtension))) {
439                     // Matches *, *.ext, /p/*, or /p/*.ext
440
return true;
441                 }
442             }
443         }
444         return false;
445     }
446
447     static class JspPropertyGroup {
448         private String JavaDoc path;
449         private String JavaDoc extension;
450         private JspProperty jspProperty;
451
452         JspPropertyGroup(String JavaDoc path, String JavaDoc extension,
453                 JspProperty jspProperty) {
454             this.path = path;
455             this.extension = extension;
456             this.jspProperty = jspProperty;
457         }
458
459         public String JavaDoc getPath() {
460             return path;
461         }
462
463         public String JavaDoc getExtension() {
464             return extension;
465         }
466
467         public JspProperty getJspProperty() {
468             return jspProperty;
469         }
470     }
471
472     static public class JspProperty {
473
474         private String JavaDoc isXml;
475         private String JavaDoc elIgnored;
476         private String JavaDoc scriptingInvalid;
477         private String JavaDoc pageEncoding;
478         private Vector JavaDoc includePrelude;
479         private Vector JavaDoc includeCoda;
480         private String JavaDoc deferedSyntaxAllowedAsLiteral;
481         private String JavaDoc trimDirectiveWhitespaces;
482
483         public JspProperty(String JavaDoc isXml, String JavaDoc elIgnored,
484                 String JavaDoc scriptingInvalid, String JavaDoc pageEncoding,
485                 Vector JavaDoc includePrelude, Vector JavaDoc includeCoda,
486                 String JavaDoc deferedSyntaxAllowedAsLiteral,
487                 String JavaDoc trimDirectiveWhitespaces) {
488
489             this.isXml = isXml;
490             this.elIgnored = elIgnored;
491             this.scriptingInvalid = scriptingInvalid;
492             this.pageEncoding = pageEncoding;
493             this.includePrelude = includePrelude;
494             this.includeCoda = includeCoda;
495             this.deferedSyntaxAllowedAsLiteral = deferedSyntaxAllowedAsLiteral;
496             this.trimDirectiveWhitespaces = trimDirectiveWhitespaces;
497         }
498
499         public String JavaDoc isXml() {
500             return isXml;
501         }
502
503         public String JavaDoc isELIgnored() {
504             return elIgnored;
505         }
506
507         public String JavaDoc isScriptingInvalid() {
508             return scriptingInvalid;
509         }
510
511         public String JavaDoc getPageEncoding() {
512             return pageEncoding;
513         }
514
515         public Vector JavaDoc getIncludePrelude() {
516             return includePrelude;
517         }
518
519         public Vector JavaDoc getIncludeCoda() {
520             return includeCoda;
521         }
522         
523         public String JavaDoc isDeferedSyntaxAllowedAsLiteral() {
524             return deferedSyntaxAllowedAsLiteral;
525         }
526         
527         public String JavaDoc isTrimDirectiveWhitespaces() {
528             return trimDirectiveWhitespaces;
529         }
530     }
531 }
532
Popular Tags