FileDocCategorySizeDatePackage
JspConfig.javaAPI DocGlassfish v2 API19227Fri May 04 22:32:52 BST 2007org.apache.jasper.compiler

JspConfig

public class JspConfig extends Object
Handles the jsp-config element in WEB_INF/web.xml. This is used for specifying the JSP configuration information on a JSP page
author
Kin-man Chung

Fields Summary
private static final String
WEB_XML
private static com.sun.org.apache.commons.logging.Log
log
private org.apache.jasper.Options
options
private Vector
jspProperties
private ServletContext
ctxt
private boolean
initialized
private String
defaultIsXml
private String
defaultIsELIgnored
private String
defaultIsScriptingInvalid
private String
defaultTrimSpaces
private String
defaultPoundAllowed
private JspProperty
defaultJspProperty
Constructors Summary
public JspConfig(ServletContext ctxt, org.apache.jasper.Options options)


    /* SJSAS 6384538
    public JspConfig(ServletContext ctxt) {
    */
    // START SJSAS 6384538
         
    // END SJSAS 6384538
	this.ctxt = ctxt;
        // START SJSAS 6384538
        this.options = options;
        // END SJSAS 6384538
    
Methods Summary
public JspPropertyfindJspProperty(java.lang.String uri)
Find a property that best matches the supplied resource.

param
uri the resource supplied.
return
a JspProperty indicating the best match, or some default.


	init();

	// JSP Configuration settings do not apply to tag files	    
	if (jspProperties == null || uri.endsWith(".tag")
	        || uri.endsWith(".tagx")) {
	    return defaultJspProperty;
	}

	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);
	}

	Vector includePreludes = new Vector();
	Vector includeCodas = new Vector();

	JspPropertyGroup isXmlMatch = null;
	JspPropertyGroup elIgnoredMatch = null;
	JspPropertyGroup scriptingInvalidMatch = null;
	JspPropertyGroup trimSpacesMatch = null;
	JspPropertyGroup poundAllowedMatch = null;
	JspPropertyGroup pageEncodingMatch = null;

	Iterator iter = jspProperties.iterator();
	while (iter.hasNext()) {

	    JspPropertyGroup jpg = (JspPropertyGroup) 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(isXmlMatch, jpg);
             }
             if (jp.isELIgnored() != null) {
                 elIgnoredMatch = selectProperty(elIgnoredMatch, jpg);
             }
             if (jp.isScriptingInvalid() != null) {
                 scriptingInvalidMatch =
                     selectProperty(scriptingInvalidMatch, jpg);
             }
             if (jp.getPageEncoding() != null) {
                 pageEncodingMatch = selectProperty(pageEncodingMatch, jpg);
             }
             if (jp.getTrimSpaces() != null) {
                 trimSpacesMatch = selectProperty(trimSpacesMatch, jpg);
             }
             if (jp.getPoundAllowed() != null) {
                 poundAllowedMatch = selectProperty(poundAllowedMatch, jpg);
             }
	}


	String isXml = defaultIsXml;
	String isELIgnored = defaultIsELIgnored;
	String isScriptingInvalid = defaultIsScriptingInvalid;
        String trimSpaces = defaultTrimSpaces;
        String poundAllowed = defaultPoundAllowed;
	String pageEncoding = null;

	if (isXmlMatch != null) {
	    isXml = isXmlMatch.getJspProperty().isXml();
	}
	if (elIgnoredMatch != null) {
	    isELIgnored = elIgnoredMatch.getJspProperty().isELIgnored();
	}
	if (scriptingInvalidMatch != null) {
	    isScriptingInvalid =
		scriptingInvalidMatch.getJspProperty().isScriptingInvalid();
	}
	if (trimSpacesMatch != null) {
	    trimSpaces = trimSpacesMatch.getJspProperty().getTrimSpaces();
	}
	if (poundAllowedMatch != null) {
	    poundAllowed = poundAllowedMatch.getJspProperty().getPoundAllowed();
	}
	if (pageEncodingMatch != null) {
	    pageEncoding = pageEncodingMatch.getJspProperty().getPageEncoding();
	}

	return new JspProperty(isXml, isELIgnored, isScriptingInvalid,
                               trimSpaces, poundAllowed,
			       pageEncoding, includePreludes, includeCodas);
    
private voidinit()


	if (!initialized) {
            /* GlassFish 740
            processWebDotXml(ctxt);
            */
            // START GlassFish 740
            jspProperties = (Vector) ctxt.getAttribute(
                Constants.JSP_PROPERTY_GROUPS_CONTEXT_ATTRIBUTE);
            if (jspProperties == null) {
                processWebDotXml(ctxt);
            }

            String version = (String) ctxt.getAttribute(
                Constants.WEB_XML_VERSION_CONTEXT_ATTRIBUTE);
            if (version != null) {
                if (Double.valueOf(version).doubleValue() < 2.4) {
                    defaultIsELIgnored = "true";
                }
            }
            // END GlassFish 740

	    defaultJspProperty = new JspProperty(defaultIsXml,
						 defaultIsELIgnored,
						 defaultIsScriptingInvalid,
                                                 defaultTrimSpaces,
                                                 defaultPoundAllowed,
                                                 null, null, null);
	    initialized = true;
	}
    
public booleanisJspPage(java.lang.String uri)
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.


        init();
        if (jspProperties == 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 = jspProperties.iterator();
        while (iter.hasNext()) {

            JspPropertyGroup jpg = (JspPropertyGroup) iter.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;
    
public static voidmakeJspPropertyGroups(java.util.Vector jspProperties, java.util.Vector urlPatterns, java.lang.String isXml, java.lang.String elIgnored, java.lang.String scriptingInvalid, java.lang.String trimSpaces, java.lang.String poundAllowed, java.lang.String pageEncoding, java.util.Vector includePrelude, java.util.Vector includeCoda)
Creates a JspPropertyGroup for each url pattern in the given urlPatterns, and adds it to the given jspProperties. This simplifies the matching logic.


        if (urlPatterns == null || urlPatterns.size() == 0) {
            return;
        }

        for (int p = 0; p < urlPatterns.size(); p++) {
            String urlPattern = (String)urlPatterns.elementAt( p );
            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 following:
                // 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)) {
                    if (log.isWarnEnabled()) {
                        log.warn(Localizer.getMessage(
                            "jsp.warning.bad.urlpattern.propertygroup",
                            urlPattern));
                    }
                    continue;
                }
             }
 
             JspProperty property = new JspProperty(isXml,
                                                    elIgnored,
                                                    scriptingInvalid,
                                                    trimSpaces,
                                                    poundAllowed,
                                                    pageEncoding,
                                                    includePrelude,
                                                    includeCoda);
             JspPropertyGroup propertyGroup =
                 new JspPropertyGroup(path, extension, property);

             jspProperties.addElement(propertyGroup);
        }
    
private voidprocessWebDotXml(javax.servlet.ServletContext ctxt)


        InputStream is = null;

        try {
            URL uri = ctxt.getResource(WEB_XML);
            if (uri == null) {
                // no web.xml
                return;
            }

            is = uri.openStream();
            InputSource ip = new InputSource(is);
            ip.setSystemId(uri.toExternalForm()); 

            ParserUtils pu = new ParserUtils();
            /* SJSAS 6384538
            TreeNode webApp = pu.parseXMLDocument(WEB_XML, ip);
            */
            // START SJSAS 6384538
            TreeNode webApp = pu.parseXMLDocument(WEB_XML, ip,
                                                  options.isValidationEnabled());
            // END SJSAS 6384538
            if (webApp == null ||
                webApp.findAttribute("version") == null ||
                Double.valueOf(webApp.findAttribute("version")).doubleValue() < 2.4) {
                defaultIsELIgnored = "true";
                return;
            }

            TreeNode jspConfig = webApp.findChild("jsp-config");
            if (jspConfig == null) {
                return;
            }

            jspProperties = new Vector();
            Iterator jspPropertyList = jspConfig.findChildren("jsp-property-group");
            while (jspPropertyList.hasNext()) {

                TreeNode element = (TreeNode) jspPropertyList.next();
                Iterator list = element.findChildren();

                Vector urlPatterns = new Vector();
                String pageEncoding = null;
                String scriptingInvalid = null;
                String elIgnored = null;
                String isXml = null;
                Vector includePrelude = new Vector();
                Vector includeCoda = new Vector();
                String trimSpaces = null;
                String poundAllowed = null;

                while (list.hasNext()) {

                    element = (TreeNode) list.next();
                    String tname = element.getName();

                    if ("url-pattern".equals(tname))
                        urlPatterns.addElement( element.getBody() );
                    else if ("page-encoding".equals(tname))
                        pageEncoding = element.getBody();
                    else if ("is-xml".equals(tname))
                        isXml = element.getBody();
                    else if ("el-ignored".equals(tname))
                        elIgnored = element.getBody();
                    else if ("scripting-invalid".equals(tname))
                        scriptingInvalid = element.getBody();
                    else if ("include-prelude".equals(tname))
                        includePrelude.addElement(element.getBody());
                    else if ("include-coda".equals(tname))
                        includeCoda.addElement(element.getBody());
                    else if ("trim-directive-whitespaces".equals(tname))
                        trimSpaces = element.getBody();
                    else if ("deferred-syntax-allowed-as-literal".equals(tname))
                        poundAllowed = element.getBody();
                }

                if (urlPatterns.size() == 0) {
                    continue;
                }
 
                makeJspPropertyGroups(jspProperties,
                                      urlPatterns, 
                                      isXml,
                                      elIgnored,
                                      scriptingInvalid,
                                      trimSpaces,
                                      poundAllowed,
                                      pageEncoding,
                                      includePrelude,
                                      includeCoda);
            }
        } catch (Exception ex) {
            throw new JasperException(ex);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (Throwable t) {}
            }
        }
    
private JspPropertyGroupselectProperty(JspPropertyGroup prev, JspPropertyGroup curr)
Select the property group that has more restrictive url-pattern. In case of tie, select the first.

        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;