Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2012 Red Hat, Inc., and individual contributors
   * as indicated by the @author tags.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.apache.jasper.compiler;
 
 
 
Handles the jsp-config element in WEB_INF/web.xml. This is used for specifying the JSP configuration information on a JSP page

Author(s):
Kin-man Chung
Remy Maucherat
 
 
 public class JspConfig {
 
     // Logger
     private Logger log = Logger.getLogger(JspConfig.class);
 
     private ArrayList<JspPropertyGroupjspProperties = null;
     private ServletContext ctxt;
     private boolean initialized = false;
 
     private String defaultIsXml = null;		// unspecified
     private String defaultIsELIgnored = null;	// unspecified
     private String defaultIsScriptingInvalid = null;
     private String defaultDeferedSyntaxAllowedAsLiteral = null;
     private String defaultTrimDirectiveWhitespaces = null;
     private JspProperty defaultJspProperty;
     private String defaultDefaultContentType = null;
     private String defaultBuffer = null;
     private String defaultErrorOnUndeclaredNamespace = null;
 
     public JspConfig(ServletContext ctxt) {
         this. = ctxt;
     }
 
     private void init() throws JasperException {
 
         if (!) {
             synchronized (this) {
                 if (!) {
                     HashMap<Stringorg.apache.catalina.deploy.JspPropertyGroupjspPropertyGroups =
                         (HashMap<Stringorg.apache.catalina.deploy.JspPropertyGroup>) 
                         .getAttribute(.);
 
                     String versionString = (String.getAttribute(.);
                     double version = 2.3;
                     if (versionString != null) {
                         try {
                             version =  Double.parseDouble(versionString);
                         } catch (NumberFormatException e) {
                         }
                     }
                     if (version < 2.4) {
                          = "true";
                          = "true";
                     }
                     if (version < 2.5) {
                          = "true";
                     }
 
                      = new ArrayList<JspPropertyGroup>();
                     Iterator<StringurlPatternIterator = jspPropertyGroups.keySet().iterator();
                     while (urlPatternIterator.hasNext()) {
                         String urlPattern = urlPatternIterator.next();
                         org.apache.catalina.deploy.JspPropertyGroup jspPropertyGroup =
                             jspPropertyGroups.get(urlPattern);
 
                         String path = null;
                         String extension = null;
 
                         if (urlPattern.indexOf('*') < 0) {
                             // Exact match
                            path = urlPattern;
                        } else {
                            int i = urlPattern.lastIndexOf('/');
                            String file;
                            if (i >= 0) {
                                path = urlPattern.substring(0,i+1);
                                file = urlPattern.substring(i+1);
                            } else {
                                file = urlPattern;
                            }
                            // pattern must be "*", or of the form "*.jsp"
                            if (file.equals("*")) {
                                extension = "*";
                            } else if (file.startsWith("*.")) {
                                extension = file.substring(file.indexOf('.')+1);
                            }
                            // The url patterns are reconstructed as the follwoing:
                            // path != null, extension == null:  / or /foo/bar.ext
                            // path == null, extension != null:  *.ext
                            // path != null, extension == "*":   /foo/*
                            boolean isStar = "*".equals(extension);
                            if ((path == null && (extension == null || isStar))
                                    || (path != null && !isStar)) {
                                ..invalidJspPropertyGroupsUrlPattern(urlPattern);
                                continue;
                            }
                        }
                        JspProperty property = new JspProperty(jspPropertyGroup.getIsXml(),
                                jspPropertyGroup.getElIgnored(),
                                jspPropertyGroup.getScriptingInvalid(),
                                jspPropertyGroup.getPageEncoding(),
                                jspPropertyGroup.getIncludePreludes(),
                                jspPropertyGroup.getIncludeCodas(),
                                jspPropertyGroup.getDeferredSyntaxAllowedAsLiteral(),
                                jspPropertyGroup.getTrimDirectiveWhitespaces(),
                                jspPropertyGroup.getDefaultContentType(),
                                jspPropertyGroup.getBuffer(),
                                jspPropertyGroup.getErrorOnUndeclaredNamespace());
                        JspPropertyGroup propertyGroup =
                            new JspPropertyGroup(pathextensionproperty);
                        .add(propertyGroup);
                    }
                     = new JspProperty(,
                            ,
                            ,
                            nullnullnull
                            ,
                            );
                     = true;
                }
            }
        }
    }

    
Select the property group that has more restrictive url-pattern. In case of tie, select the first.
            JspPropertyGroup curr) {
        if (prev == null) {
            return curr;
        }
        if (prev.getExtension() == null) {
            // exact match
            return prev;
        }
        if (curr.getExtension() == null) {
            // exact match
            return curr;
        }
        String prevPath = prev.getPath();
        String currPath = curr.getPath();
        if (prevPath == null && currPath == null) {
            // Both specifies a *.ext, keep the first one
            return prev;
        }
        if (prevPath == null && currPath != null) {
            return curr;
        }
        if (prevPath != null && currPath == null) {
            return prev;
        }
        if (prevPath.length() >= currPath.length()) {
            return prev;
        }
        return curr;
    }


    
Find a property that best matches the supplied resource.

Parameters:
uri the resource supplied.
Returns:
a JspProperty indicating the best match, or some default.
    public JspProperty findJspProperty(String urithrows JasperException {
        init();
        // JSP Configuration settings do not apply to tag files	    
        if ( == null || uri.endsWith(".tag")
                || uri.endsWith(".tagx")) {
            return ;
        }
        String uriPath = null;
        int index = uri.lastIndexOf('/');
        if (index >=0 ) {
            uriPath = uri.substring(0, index+1);
        }
        String uriExtension = null;
        index = uri.lastIndexOf('.');
        if (index >=0) {
            uriExtension = uri.substring(index+1);
        }
        ArrayList<StringincludePreludes = new ArrayList<String>();
        ArrayList<StringincludeCodas = new ArrayList<String>();
        JspPropertyGroup isXmlMatch = null;
        JspPropertyGroup elIgnoredMatch = null;
        JspPropertyGroup scriptingInvalidMatch = null;
        JspPropertyGroup pageEncodingMatch = null;
        JspPropertyGroup deferedSyntaxAllowedAsLiteralMatch = null;
        JspPropertyGroup trimDirectiveWhitespacesMatch = null;
        JspPropertyGroup defaultContentTypeMatch = null;
        JspPropertyGroup bufferMatch = null;
        JspPropertyGroup errorOnUndeclaredNamespaceMatch = null;
        Iterator<JspPropertyGroupiter = .iterator();
        while (iter.hasNext()) {
            JspPropertyGroup jpg = iter.next();
            JspProperty jp = jpg.getJspProperty();
            // (arrays will be the same length)
            String extension = jpg.getExtension();
            String path = jpg.getPath();
            if (extension == null) {
                // exact match pattern: /a/foo.jsp
                if (!uri.equals(path)) {
                    // not matched;
                    continue;
                }
            } else {
                // Matching patterns *.ext or /p/*
                if (path != null && uriPath != null &&
                        ! uriPath.startsWith(path)) {
                    // not matched
                    continue;
                }
                if (!extension.equals("*") &&
                        !extension.equals(uriExtension)) {
                    // not matched
                    continue;
                }
            }
            // We have a match
            // Add include-preludes and include-codas
            if (jp.getIncludePrelude() != null) {
                includePreludes.addAll(jp.getIncludePrelude());
            }
            if (jp.getIncludeCoda() != null) {
                includeCodas.addAll(jp.getIncludeCoda());
            }
            // If there is a previous match for the same property, remember
            // the one that is more restrictive.
            if (jp.isXml() != null) {
                isXmlMatch = selectProperty(isXmlMatchjpg);
            }
            if (jp.isELIgnored() != null) {
                elIgnoredMatch = selectProperty(elIgnoredMatchjpg);
            }
            if (jp.isScriptingInvalid() != null) {
                scriptingInvalidMatch =
                    selectProperty(scriptingInvalidMatchjpg);
            }
            if (jp.getPageEncoding() != null) {
                pageEncodingMatch = selectProperty(pageEncodingMatchjpg);
            }
            if (jp.isDeferedSyntaxAllowedAsLiteral() != null) {
                deferedSyntaxAllowedAsLiteralMatch =
                    selectProperty(deferedSyntaxAllowedAsLiteralMatchjpg);
            }
            if (jp.isTrimDirectiveWhitespaces() != null) {
                trimDirectiveWhitespacesMatch =
                    selectProperty(trimDirectiveWhitespacesMatchjpg);
            }
            if (jp.getDefaultContentType() != null) {
                defaultContentTypeMatch =
                    selectProperty(defaultContentTypeMatchjpg);
            }
            if (jp.getBuffer() != null) {
                bufferMatch = selectProperty(bufferMatchjpg);
            }
            if (jp.isErrorOnUndeclaredNamespace() != null) {
                errorOnUndeclaredNamespaceMatch =
                    selectProperty(errorOnUndeclaredNamespaceMatchjpg);
            }
        }
        String isXml = ;
        String isELIgnored = ;
        String isScriptingInvalid = ;
        String pageEncoding = null;
        String isDeferedSyntaxAllowedAsLiteral = ;
        String isTrimDirectiveWhitespaces = ;
        String defaultContentType = ;
        String buffer = ;
        String isErrorOnUnderclaredNamespace = ;
        if (isXmlMatch != null) {
            isXml = isXmlMatch.getJspProperty().isXml();
        }
        if (elIgnoredMatch != null) {
            isELIgnored = elIgnoredMatch.getJspProperty().isELIgnored();
        }
        if (scriptingInvalidMatch != null) {
            isScriptingInvalid =
                scriptingInvalidMatch.getJspProperty().isScriptingInvalid();
        }
        if (pageEncodingMatch != null) {
            pageEncoding = pageEncodingMatch.getJspProperty().getPageEncoding();
        }
        if (deferedSyntaxAllowedAsLiteralMatch != null) {
            isDeferedSyntaxAllowedAsLiteral =
                deferedSyntaxAllowedAsLiteralMatch.getJspProperty().isDeferedSyntaxAllowedAsLiteral();
        }
        if (trimDirectiveWhitespacesMatch != null) {
            isTrimDirectiveWhitespaces =
                trimDirectiveWhitespacesMatch.getJspProperty().isTrimDirectiveWhitespaces();
        }
        if (defaultContentTypeMatch != null) {
            defaultContentType =
                defaultContentTypeMatch.getJspProperty().getDefaultContentType();
        }
        if (bufferMatch != null) {
            buffer = bufferMatch.getJspProperty().getBuffer();
        }
        if (errorOnUndeclaredNamespaceMatch != null) {
            isErrorOnUnderclaredNamespace = 
                errorOnUndeclaredNamespaceMatch.getJspProperty().isErrorOnUndeclaredNamespace();
        }
        return new JspProperty(isXmlisELIgnoredisScriptingInvalid,
                pageEncodingincludePreludesincludeCodas
                isDeferedSyntaxAllowedAsLiteralisTrimDirectiveWhitespaces,
                defaultContentTypebufferisErrorOnUnderclaredNamespace);
    }

    
To find out if an uri matches an url pattern in jsp config. If so, then the uri is a JSP page. This is used primarily for jspc.
    public boolean isJspPage(String urithrows JasperException {
        init();
        if ( == null) {
            return false;
        }
        String uriPath = null;
        int index = uri.lastIndexOf('/');
        if (index >=0 ) {
            uriPath = uri.substring(0, index+1);
        }
        String uriExtension = null;
        index = uri.lastIndexOf('.');
        if (index >=0) {
            uriExtension = uri.substring(index+1);
        }
        Iterator iter = .iterator();
        while (iter.hasNext()) {
            JspPropertyGroup jpg = (JspPropertyGroupiter.next();
            JspProperty jp = jpg.getJspProperty();
            String extension = jpg.getExtension();
            String path = jpg.getPath();
            if (extension == null) {
                if (uri.equals(path)) {
                    // There is an exact match
                    return true;
                }
            } else {
                if ((path == null || path.equals(uriPath)) &&
                        (extension.equals("*") || extension.equals(uriExtension))) {
                    // Matches *, *.ext, /p/*, or /p/*.ext
                    return true;
                }
            }
        }
        return false;
    }
    static class JspPropertyGroup {
        private String path;
        private String extension;
        private JspProperty jspProperty;
        JspPropertyGroup(String pathString extension,
                JspProperty jspProperty) {
            this. = path;
            this. = extension;
            this. = jspProperty;
        }
        public String getPath() {
            return ;
        }
        public String getExtension() {
            return ;
        }
        public JspProperty getJspProperty() {
            return ;
        }
    }
    static public class JspProperty {
        private String isXml;
        private String elIgnored;
        private String scriptingInvalid;
        private String pageEncoding;
        private ArrayList<StringincludePrelude;
        private ArrayList<StringincludeCoda;
        private String deferedSyntaxAllowedAsLiteral;
        private String trimDirectiveWhitespaces;
        private String defaultContentType;
        private String buffer;
        private String errorOnUndeclaredNamespace;
        public JspProperty(String isXmlString elIgnored,
                String scriptingInvalidString pageEncoding,
                ArrayList<StringincludePreludeArrayList<StringincludeCoda,
                String deferedSyntaxAllowedAsLiteral
                String trimDirectiveWhitespacesString defaultContentType,
                String bufferString errorOnUndeclaredNamespace) {
            this. = isXml;
            this. = elIgnored;
            this. = scriptingInvalid;
            this. = pageEncoding;
            this. = includePrelude;
            this. = includeCoda;
            this. = deferedSyntaxAllowedAsLiteral;
            this. = trimDirectiveWhitespaces;
            this. = defaultContentType;
            this. = buffer;
            this. = errorOnUndeclaredNamespace;
        }
        public String isXml() {
            return ;
        }
        public String isELIgnored() {
            return ;
        }
        public String isScriptingInvalid() {
            return ;
        }
        public String getPageEncoding() {
            return ;
        }
        public ArrayList<StringgetIncludePrelude() {
            return ;
        }
        public ArrayList<StringgetIncludeCoda() {
            return ;
        }
        
        public String isDeferedSyntaxAllowedAsLiteral() {
            return ;
        }
        
        public String isTrimDirectiveWhitespaces() {
            return ;
        }
        
        public String getDefaultContentType() {
            return ;
        }
        
        public String getBuffer() {
            return ;
        }
        
        public String isErrorOnUndeclaredNamespace() {
            return ;
        }
    }
New to GrepCode? Check out our FAQ X