parser.java
来自「精通tomcat书籍原代码,希望大家共同学习」· Java 代码 · 共 1,859 行 · 第 1/5 页
JAVA
1,859 行
/*
* Copyright 1999,2004 The Apache Software Foundation.
*
* 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;
import java.io.CharArrayWriter;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import javax.servlet.jsp.tagext.TagAttributeInfo;
import javax.servlet.jsp.tagext.TagFileInfo;
import javax.servlet.jsp.tagext.TagInfo;
import javax.servlet.jsp.tagext.TagLibraryInfo;
import org.apache.jasper.Constants;
import org.apache.jasper.JasperException;
import org.apache.jasper.JspCompilationContext;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.AttributesImpl;
/**
* This class implements a parser for a JSP page (non-xml view).
* JSP page grammar is included here for reference. The token '#'
* that appears in the production indicates the current input token
* location in the production.
*
* @author Kin-man Chung
* @author Shawn Bayern
* @author Mark Roth
*/
class Parser implements TagConstants {
private ParserController parserController;
private JspCompilationContext ctxt;
private JspReader reader;
private String currentFile;
private Mark start;
private ErrorDispatcher err;
private int scriptlessCount;
private boolean isTagFile;
private boolean directivesOnly;
private URL jarFileUrl;
private PageInfo pageInfo;
// Virtual body content types, to make parsing a little easier.
// These are not accessible from outside the parser.
private static final String JAVAX_BODY_CONTENT_PARAM =
"JAVAX_BODY_CONTENT_PARAM";
private static final String JAVAX_BODY_CONTENT_PLUGIN =
"JAVAX_BODY_CONTENT_PLUGIN";
private static final String JAVAX_BODY_CONTENT_TEMPLATE_TEXT =
"JAVAX_BODY_CONTENT_TEMPLATE_TEXT";
/**
* The constructor
*/
private Parser(ParserController pc, JspReader reader, boolean isTagFile,
boolean directivesOnly, URL jarFileUrl) {
this.parserController = pc;
this.ctxt = pc.getJspCompilationContext();
this.pageInfo = pc.getCompiler().getPageInfo();
this.err = pc.getCompiler().getErrorDispatcher();
this.reader = reader;
this.currentFile = reader.mark().getFile();
this.scriptlessCount = 0;
this.isTagFile = isTagFile;
this.directivesOnly = directivesOnly;
this.jarFileUrl = jarFileUrl;
start = reader.mark();
}
/**
* The main entry for Parser
*
* @param pc The ParseController, use for getting other objects in compiler
* and for parsing included pages
* @param reader To read the page
* @param parent The parent node to this page, null for top level page
* @return list of nodes representing the parsed page
*/
public static Node.Nodes parse(ParserController pc,
JspReader reader,
Node parent,
boolean isTagFile,
boolean directivesOnly,
URL jarFileUrl,
String pageEnc,
String jspConfigPageEnc,
boolean isDefaultPageEncoding)
throws JasperException {
Parser parser = new Parser(pc, reader, isTagFile, directivesOnly,
jarFileUrl);
Node.Root root = new Node.Root(reader.mark(), parent, false);
root.setPageEncoding(pageEnc);
root.setJspConfigPageEncoding(jspConfigPageEnc);
root.setIsDefaultPageEncoding(isDefaultPageEncoding);
if (directivesOnly) {
parser.parseTagFileDirectives(root);
return new Node.Nodes(root);
}
// For the Top level page, add inlcude-prelude and include-coda
PageInfo pageInfo = pc.getCompiler().getPageInfo();
if (parent == null) {
parser.addInclude(root, pageInfo.getIncludePrelude());
}
while (reader.hasMoreInput()) {
parser.parseElements(root);
}
if (parent == null) {
parser.addInclude(root, pageInfo.getIncludeCoda());
}
Node.Nodes page = new Node.Nodes(root);
return page;
}
/**
* Attributes ::= (S Attribute)* S?
*/
Attributes parseAttributes() throws JasperException {
AttributesImpl attrs = new AttributesImpl();
reader.skipSpaces();
while (parseAttribute(attrs))
reader.skipSpaces();
return attrs;
}
/**
* Parse Attributes for a reader, provided for external use
*/
public static Attributes parseAttributes(ParserController pc,
JspReader reader)
throws JasperException {
Parser tmpParser = new Parser(pc, reader, false, false, null);
return tmpParser.parseAttributes();
}
/**
* Attribute ::= Name S? Eq S?
* ( '"<%=' RTAttributeValueDouble
* | '"' AttributeValueDouble
* | "'<%=" RTAttributeValueSingle
* | "'" AttributeValueSingle
* }
* Note: JSP and XML spec does not allow while spaces around Eq. It is
* added to be backward compatible with Tomcat, and with other xml parsers.
*/
private boolean parseAttribute(AttributesImpl attrs)
throws JasperException {
// Get the qualified name
String qName = parseName();
if (qName == null)
return false;
// Determine prefix and local name components
String localName = qName;
String uri = "";
int index = qName.indexOf(':');
if (index != -1) {
String prefix = qName.substring(0, index);
uri = pageInfo.getURI(prefix);
if (uri == null) {
err.jspError(reader.mark(),
"jsp.error.attribute.invalidPrefix", prefix);
}
localName = qName.substring(index+1);
}
reader.skipSpaces();
if (!reader.matches("="))
err.jspError(reader.mark(), "jsp.error.attribute.noequal");
reader.skipSpaces();
char quote = (char) reader.nextChar();
if (quote != '\'' && quote != '"')
err.jspError(reader.mark(), "jsp.error.attribute.noquote");
String watchString = "";
if (reader.matches("<%="))
watchString = "%>";
watchString = watchString + quote;
String attrValue = parseAttributeValue(watchString);
attrs.addAttribute(uri, localName, qName, "CDATA", attrValue);
return true;
}
/**
* Name ::= (Letter | '_' | ':') (Letter | Digit | '.' | '_' | '-' | ':')*
*/
private String parseName() throws JasperException {
char ch = (char)reader.peekChar();
if (Character.isLetter(ch) || ch == '_' || ch == ':') {
StringBuffer buf = new StringBuffer();
buf.append(ch);
reader.nextChar();
ch = (char)reader.peekChar();
while (Character.isLetter(ch) || Character.isDigit(ch) ||
ch == '.' || ch == '_' || ch == '-' || ch == ':') {
buf.append(ch);
reader.nextChar();
ch = (char) reader.peekChar();
}
return buf.toString();
}
return null;
}
/**
* AttributeValueDouble ::= (QuotedChar - '"')*
* ('"' | <TRANSLATION_ERROR>)
* RTAttributeValueDouble ::= ((QuotedChar - '"')* - ((QuotedChar-'"')'%>"')
* ('%>"' | TRANSLATION_ERROR)
*/
private String parseAttributeValue(String watch) throws JasperException {
Mark start = reader.mark();
Mark stop = reader.skipUntilIgnoreEsc(watch);
if (stop == null) {
err.jspError(start, "jsp.error.attribute.unterminated", watch);
}
String ret = parseQuoted(reader.getText(start, stop));
if (watch.length() == 1) // quote
return ret;
// putback delimiter '<%=' and '%>', since they are needed if the
// attribute does not allow RTexpression.
return "<%=" + ret + "%>";
}
/**
* QuotedChar ::= '''
* | '"'
* | '\\'
* | '\"'
* | "\'"
* | '\>'
* | '\$'
* | Char
*/
private String parseQuoted(String tx) {
StringBuffer buf = new StringBuffer();
int size = tx.length();
int i = 0;
while (i < size) {
char ch = tx.charAt(i);
if (ch == '&') {
if (i+5 < size && tx.charAt(i+1) == 'a'
&& tx.charAt(i+2) == 'p' && tx.charAt(i+3) == 'o'
&& tx.charAt(i+4) == 's' && tx.charAt(i+5) == ';') {
buf.append('\'');
i += 6;
} else if (i+5 < size && tx.charAt(i+1) == 'q'
&& tx.charAt(i+2) == 'u' && tx.charAt(i+3) == 'o'
&& tx.charAt(i+4) == 't' && tx.charAt(i+5) == ';') {
buf.append('"');
i += 6;
} else {
buf.append(ch);
++i;
}
} else if (ch == '\\' && i+1 < size) {
ch = tx.charAt(i+1);
if (ch == '\\' || ch == '\"' || ch == '\'' || ch == '>') {
buf.append(ch);
i += 2;
} else if (ch == '$') {
// Replace "\$" with some special char. XXX hack!
buf.append(Constants.ESC);
i += 2;
} else {
buf.append('\\');
++i;
}
} else {
buf.append(ch);
++i;
}
}
return buf.toString();
}
private String parseScriptText(String tx) {
CharArrayWriter cw = new CharArrayWriter();
int size = tx.length();
int i = 0;
while (i < size) {
char ch = tx.charAt(i);
if (i+2 < size && ch == '%' && tx.charAt(i+1) == '\\'
&& tx.charAt(i+2) == '>') {
cw.write('%');
cw.write('>');
i += 3;
} else {
cw.write(ch);
++i;
}
}
cw.close();
return cw.toString();
}
/*
* Invokes parserController to parse the included page
*/
private void processIncludeDirective(String file, Node parent)
throws JasperException {
if (file == null) {
return;
}
try {
parserController.parse(file, parent, jarFileUrl);
} catch (FileNotFoundException ex) {
err.jspError(start, "jsp.error.file.not.found", file);
} catch (Exception ex) {
err.jspError(start, ex.getMessage());
}
}
/*
* Parses a page directive with the following syntax:
* PageDirective ::= ( S Attribute)*
*/
private void parsePageDirective(Node parent) throws JasperException {
Attributes attrs = parseAttributes();
Node.PageDirective n = new Node.PageDirective(attrs, start, parent);
/*
* A page directive may contain multiple 'import' attributes, each of
* which consists of a comma-separated list of package names.
* Store each list with the node, where it is parsed.
*/
for (int i = 0; i < attrs.getLength(); i++) {
if ("import".equals(attrs.getQName(i))) {
n.addImport(attrs.getValue(i));
}
}
}
/*
* Parses an include directive with the following syntax:
* IncludeDirective ::= ( S Attribute)*
*/
private void parseIncludeDirective(Node parent) throws JasperException {
Attributes attrs = parseAttributes();
// Included file expanded here
Node includeNode = new Node.IncludeDirective(attrs, start, parent);
processIncludeDirective(attrs.getValue("file"), includeNode);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?