📄 parser.java
字号:
/* * The Apache Software License, Version 1.1 * * Copyright (c) 1999 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */ package org.apache.jasper.compiler;import java.util.Vector;import java.util.Hashtable;import java.util.Enumeration;import java.io.CharArrayWriter;import org.apache.jasper.JasperException;import org.apache.jasper.Constants;import javax.servlet.jsp.tagext.TagLibraryInfo;import javax.servlet.jsp.tagext.TagInfo;import org.apache.tomcat.logging.Logger;/** * The class that parses the JSP input and calls the right methods on * the code generator backend. * * @author Anil K. Vijendran * @author Rajiv Mordani */public class Parser { /** * The input source we read from... */ private JspReader reader; /** * The backend that is notified of constructs recognized in the input... */ private ParseEventListener listener; /* * Char buffer for HTML data */ CharArrayWriter caw; /* * Marker for start and end of the tempate data. */ Mark tmplStart; Mark tmplStop; /* * Name of the current file. * Useful to preserve the line number information in * case of an include. */ String currentFile; public interface Action { void execute(Mark start, Mark stop) throws JasperException; } public Parser(JspReader reader, final ParseEventListener lnr) { this.reader = reader; this.listener = new DelegatingListener(lnr, new Action() { public void execute(Mark start, Mark stop) throws JasperException { Parser.this.flushCharData(start, stop); } }); this.caw = new CharArrayWriter(); this.currentFile = reader.mark().getFile(); } static final Vector coreElements = new Vector(); /* * JSP directives */ static final class Directive implements CoreElement { private static final String OPEN_DIRECTIVE = "<%@"; private static final String CLOSE_DIRECTIVE = "%>"; static final String[] directives = { "page", "include", "taglib" }; private static final JspUtil.ValidAttribute[] pageDvalidAttrs = { new JspUtil.ValidAttribute ("language"), new JspUtil.ValidAttribute ("extends"), new JspUtil.ValidAttribute ("import"), new JspUtil.ValidAttribute ("session"), new JspUtil.ValidAttribute ("buffer"), new JspUtil.ValidAttribute ("autoFlush"), new JspUtil.ValidAttribute ("isThreadSafe"), new JspUtil.ValidAttribute ("info"), new JspUtil.ValidAttribute ("errorPage"), new JspUtil.ValidAttribute ("isErrorPage"), new JspUtil.ValidAttribute ("contentType") }; private static final JspUtil.ValidAttribute[] includeDvalidAttrs = { new JspUtil.ValidAttribute ("file", true) }; private static final JspUtil.ValidAttribute[] tagDvalidAttrs = { new JspUtil.ValidAttribute ("uri", true), new JspUtil.ValidAttribute ("prefix", true) }; public boolean accept(ParseEventListener listener, JspReader reader, Parser parser) throws JasperException { String close; String open; if (reader.matches(OPEN_DIRECTIVE)) { open = OPEN_DIRECTIVE; close = CLOSE_DIRECTIVE; } else return false; Mark start = reader.mark(); reader.advance(open.length()); reader.skipSpaces(); // Check which directive it is. String match = null; for(int i = 0; i < directives.length; i++) if (reader.matches(directives[i])) { match = directives[i]; break; } if (match == null) throw new ParseException(reader.mark(), Constants.getString("jsp.error.invalid.directive")); reader.advance(match.length()); // Parse the attr-val pairs. Hashtable attrs = reader.parseTagAttributes(); if (match.equals ("page")) JspUtil.checkAttributes ("Page directive", attrs, pageDvalidAttrs, start); else if (match.equals("include")) JspUtil.checkAttributes ("Include directive", attrs, includeDvalidAttrs, start); else if (match.equals("taglib")) JspUtil.checkAttributes ("Taglib directive", attrs, tagDvalidAttrs, start); // Match close. reader.skipSpaces(); if (!reader.matches(close)) throw new ParseException(reader.mark(), Constants.getString("jsp.error.unterminated", new Object[] { open })); else reader.advance(close.length()); Mark stop = reader.mark(); listener.setTemplateInfo(parser.tmplStart, parser.tmplStop); listener.handleDirective(match, start, stop, attrs); return true; } } static { coreElements.addElement(new Directive()); } /* * Include action */ static final class Include implements CoreElement { private static final String OPEN_INCLUDE = "<jsp:include"; private static final String CLOSE_INCLUDE_NO_BODY = "/>"; private static final String CLOSE_INCLUDE_BODY = ">"; private static final String CLOSE_INCLUDE = "</jsp:include>"; private static final String OPEN_INDIVIDUAL_PARAM = "<jsp:param"; private static final String CLOSE_INDIVIDUAL_PARAM = "/>"; private static final JspUtil.ValidAttribute[] validAttributes = { new JspUtil.ValidAttribute("page", true), new JspUtil.ValidAttribute("flush") }; public boolean accept(ParseEventListener listener, JspReader reader, Parser parser) throws JasperException { if (reader.matches(OPEN_INCLUDE)) { Hashtable param = new Hashtable(); Mark start = reader.mark(); reader.advance(OPEN_INCLUDE.length()); Hashtable attrs = reader.parseTagAttributes(); JspUtil.checkAttributes ("Include", attrs, validAttributes, start); reader.skipSpaces(); if (!reader.matches(CLOSE_INCLUDE_NO_BODY)) { if (!reader.matches(CLOSE_INCLUDE_BODY)) throw new ParseException(reader.mark(), Constants.getString ("jsp.error.unterminated", new Object[] { OPEN_INCLUDE })); reader.advance(CLOSE_INCLUDE_BODY.length()); reader.skipSpaces(); if (!reader.matches(CLOSE_INCLUDE)) { // Parse the params. reader.skipSpaces(); if (!reader.matches (OPEN_INDIVIDUAL_PARAM)) throw new ParseException (reader.mark(), Constants.getString ("jsp.error.paramexpected")); //Parse zero or more param tags. while (reader.matches(OPEN_INDIVIDUAL_PARAM)) { reader.parsePluginParamTag(param); reader.skipSpaces (); if (!reader.matches (CLOSE_INDIVIDUAL_PARAM)) throw new ParseException (reader.mark(), Constants.getString ("jsp.error.unterminated", new Object[] {OPEN_INDIVIDUAL_PARAM})); reader.advance (CLOSE_INDIVIDUAL_PARAM.length ()); reader.skipSpaces(); } } if (!reader.matches(CLOSE_INCLUDE)) throw new ParseException(reader.mark(), Constants.getString ("jsp.error.unterminated", new Object[] { OPEN_INCLUDE })); reader.advance(CLOSE_INCLUDE.length()); } else reader.advance(CLOSE_INCLUDE_NO_BODY.length()); Mark stop = reader.mark(); listener.setTemplateInfo(parser.tmplStart, parser.tmplStop); listener.handleInclude(start, stop, attrs, param); return true; } else return false; } } static { coreElements.addElement(new Include()); } /* * Forward action */ static final class Forward implements CoreElement { private static final String OPEN_FORWARD = "<jsp:forward"; private static final String CLOSE_FORWARD_NO_BODY = "/>"; private static final String CLOSE_FORWARD_BODY = ">"; private static final String CLOSE_FORWARD = "</jsp:forward>"; private static final String OPEN_INDIVIDUAL_PARAM = "<jsp:param"; private static final String CLOSE_INDIVIDUAL_PARAM = "/>"; private static final JspUtil.ValidAttribute[] validAttributes = { new JspUtil.ValidAttribute("page", true) }; public boolean accept(ParseEventListener listener, JspReader reader, Parser parser) throws JasperException { if (reader.matches(OPEN_FORWARD)) { Mark start = reader.mark(); reader.advance(OPEN_FORWARD.length()); Hashtable attrs = reader.parseTagAttributes(); Hashtable param = new Hashtable(); JspUtil.checkAttributes ("Forward", attrs, validAttributes, start); reader.skipSpaces(); if (!reader.matches(CLOSE_FORWARD_NO_BODY)) { if (!reader.matches(CLOSE_FORWARD_BODY)) throw new ParseException(reader.mark(), Constants.getString ("jsp.error.unterminated", new Object[] { OPEN_FORWARD })); reader.advance(CLOSE_FORWARD_BODY.length()); reader.skipSpaces(); if (!reader.matches(CLOSE_FORWARD)) { // Parse the params. reader.skipSpaces(); if (!reader.matches (OPEN_INDIVIDUAL_PARAM)) throw new ParseException (reader.mark(), Constants.getString ("jsp.error.paramexpected")); // Parse zero or more param tags. while (reader.matches(OPEN_INDIVIDUAL_PARAM)) { //Borrow plugin's parse function. reader.parsePluginParamTag(param); reader.skipSpaces(); if (!reader.matches (CLOSE_INDIVIDUAL_PARAM)) throw new ParseException (reader.mark(), Constants.getString ("jsp.error.unterminated", new Object[] {OPEN_INDIVIDUAL_PARAM})); reader.advance (CLOSE_INDIVIDUAL_PARAM.length ()); reader.skipSpaces(); } } if (!reader.matches(CLOSE_FORWARD))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -