📄 validator.java
字号:
new JspUtil.ValidAttribute("doctype-system") };
/*
* Constructor
*/
ValidateVisitor(Compiler compiler) {
this.pageInfo = compiler.getPageInfo();
this.err = compiler.getErrorDispatcher();
this.tagInfo = compiler.getCompilationContext().getTagInfo();
this.loader = compiler.getCompilationContext().getClassLoader();
}
public void visit(Node.JspRoot n) throws JasperException {
JspUtil.checkAttributes("Jsp:root", n,
jspRootAttrs, err);
String version = n.getTextAttribute("version");
if (!version.equals("1.2") && !version.equals("2.0")) {
err.jspError(n, "jsp.error.jsproot.version.invalid", version);
}
visitBody(n);
}
public void visit(Node.IncludeDirective n) throws JasperException {
JspUtil.checkAttributes("Include directive", n,
includeDirectiveAttrs, err);
visitBody(n);
}
public void visit(Node.TaglibDirective n) throws JasperException {
JspUtil.checkAttributes("Taglib directive", n,
taglibDirectiveAttrs, err);
// Either 'uri' or 'tagdir' attribute must be specified
String uri = n.getAttributeValue("uri");
String tagdir = n.getAttributeValue("tagdir");
if (uri == null && tagdir == null) {
err.jspError(n, "jsp.error.taglibDirective.missing.location");
}
if (uri != null && tagdir != null) {
err.jspError(n, "jsp.error.taglibDirective.both_uri_and_tagdir");
}
}
public void visit(Node.ParamAction n) throws JasperException {
JspUtil.checkAttributes("Param action", n,
paramActionAttrs, err);
// make sure the value of the 'name' attribute is not a
// request-time expression
throwErrorIfExpression(n, "name", "jsp:param");
n.setValue(getJspAttribute("value", null, null,
n.getAttributeValue("value"),
java.lang.String.class,
n, false));
visitBody(n);
}
public void visit(Node.ParamsAction n) throws JasperException {
// Make sure we've got at least one nested jsp:param
Node.Nodes subElems = n.getBody();
if (subElems == null) {
err.jspError(n, "jsp.error.params.emptyBody");
}
visitBody(n);
}
public void visit(Node.IncludeAction n) throws JasperException {
JspUtil.checkAttributes("Include action", n,
includeActionAttrs, err);
n.setPage(getJspAttribute("page", null, null,
n.getAttributeValue("page"),
java.lang.String.class, n, false));
visitBody(n);
};
public void visit(Node.ForwardAction n) throws JasperException {
JspUtil.checkAttributes("Forward", n,
forwardActionAttrs, err);
n.setPage(getJspAttribute("page", null, null,
n.getAttributeValue("page"),
java.lang.String.class, n, false));
visitBody(n);
}
public void visit(Node.GetProperty n) throws JasperException {
JspUtil.checkAttributes("GetProperty", n,
getPropertyAttrs, err);
}
public void visit(Node.SetProperty n) throws JasperException {
JspUtil.checkAttributes("SetProperty", n,
setPropertyAttrs, err);
String name = n.getTextAttribute("name");
String property = n.getTextAttribute("property");
String param = n.getTextAttribute("param");
String value = n.getAttributeValue("value");
n.setValue(getJspAttribute("value", null, null, value,
java.lang.Object.class, n, false));
boolean valueSpecified = n.getValue() != null;
if ("*".equals(property)) {
if (param != null || valueSpecified)
err.jspError(n, "jsp.error.setProperty.invalid");
} else if (param != null && valueSpecified) {
err.jspError(n, "jsp.error.setProperty.invalid");
}
visitBody(n);
}
public void visit(Node.UseBean n) throws JasperException {
JspUtil.checkAttributes("UseBean", n,
useBeanAttrs, err);
String name = n.getTextAttribute ("id");
String scope = n.getTextAttribute ("scope");
JspUtil.checkScope(scope, n, err);
String className = n.getTextAttribute ("class");
String type = n.getTextAttribute ("type");
BeanRepository beanInfo = pageInfo.getBeanRepository();
if (className == null && type == null)
err.jspError(n, "jsp.error.usebean.missingType");
if (beanInfo.checkVariable(name))
err.jspError(n, "jsp.error.usebean.duplicate");
if ("session".equals(scope) && !pageInfo.isSession())
err.jspError(n, "jsp.error.usebean.noSession");
Node.JspAttribute jattr
= getJspAttribute("beanName", null, null,
n.getAttributeValue("beanName"),
java.lang.String.class, n, false);
n.setBeanName(jattr);
if (className != null && jattr != null)
err.jspError(n, "jsp.error.usebean.notBoth");
if (className == null)
className = type;
beanInfo.addBean(n, name, className, scope);
visitBody(n);
}
public void visit(Node.PlugIn n) throws JasperException {
JspUtil.checkAttributes("Plugin", n, plugInAttrs, err);
throwErrorIfExpression(n, "type", "jsp:plugin");
throwErrorIfExpression(n, "code", "jsp:plugin");
throwErrorIfExpression(n, "codebase", "jsp:plugin");
throwErrorIfExpression(n, "align", "jsp:plugin");
throwErrorIfExpression(n, "archive", "jsp:plugin");
throwErrorIfExpression(n, "hspace", "jsp:plugin");
throwErrorIfExpression(n, "jreversion", "jsp:plugin");
throwErrorIfExpression(n, "name", "jsp:plugin");
throwErrorIfExpression(n, "vspace", "jsp:plugin");
throwErrorIfExpression(n, "nspluginurl", "jsp:plugin");
throwErrorIfExpression(n, "iepluginurl", "jsp:plugin");
String type = n.getTextAttribute("type");
if (type == null)
err.jspError(n, "jsp.error.plugin.notype");
if (!type.equals("bean") && !type.equals("applet"))
err.jspError(n, "jsp.error.plugin.badtype");
if (n.getTextAttribute("code") == null)
err.jspError(n, "jsp.error.plugin.nocode");
Node.JspAttribute width
= getJspAttribute("width", null, null,
n.getAttributeValue("width"),
java.lang.String.class, n, false);
n.setWidth( width );
Node.JspAttribute height
= getJspAttribute("height", null, null,
n.getAttributeValue("height"),
java.lang.String.class, n, false);
n.setHeight( height );
visitBody(n);
}
public void visit(Node.NamedAttribute n) throws JasperException {
JspUtil.checkAttributes("Attribute", n,
attributeAttrs, err);
visitBody(n);
}
public void visit(Node.JspBody n) throws JasperException {
visitBody(n);
}
public void visit(Node.Declaration n) throws JasperException {
if (pageInfo.isScriptingInvalid()) {
err.jspError(n.getStart(), "jsp.error.no.scriptlets");
}
}
public void visit(Node.Expression n) throws JasperException {
if (pageInfo.isScriptingInvalid()) {
err.jspError(n.getStart(), "jsp.error.no.scriptlets");
}
}
public void visit(Node.Scriptlet n) throws JasperException {
if (pageInfo.isScriptingInvalid()) {
err.jspError(n.getStart(), "jsp.error.no.scriptlets");
}
}
public void visit(Node.ELExpression n) throws JasperException {
if ( !pageInfo.isELIgnored() ) {
String expressions = "${" + new String(n.getText()) + "}";
ELNode.Nodes el = ELParser.parse(expressions);
validateFunctions(el, n);
JspUtil.validateExpressions(
n.getStart(),
expressions,
java.lang.String.class, // XXX - Should template text
// always evaluate to String?
getFunctionMapper(el),
err);
n.setEL(el);
}
}
public void visit(Node.UninterpretedTag n) throws JasperException {
if (n.getNamedAttributeNodes().size() != 0) {
err.jspError(n, "jsp.error.namedAttribute.invalidUse");
}
Attributes attrs = n.getAttributes();
if (attrs != null) {
int attrSize = attrs.getLength();
Node.JspAttribute[] jspAttrs = new Node.JspAttribute[attrSize];
for (int i=0; i < attrSize; i++) {
jspAttrs[i] = getJspAttribute(attrs.getQName(i),
attrs.getURI(i),
attrs.getLocalName(i),
attrs.getValue(i),
java.lang.Object.class,
n,
false);
}
n.setJspAttributes(jspAttrs);
}
visitBody(n);
}
public void visit(Node.CustomTag n) throws JasperException {
TagInfo tagInfo = n.getTagInfo();
if (tagInfo == null) {
err.jspError(n, "jsp.error.missing.tagInfo", n.getQName());
}
/*
* The bodyconet of a SimpleTag cannot be JSP.
*/
if (n.implementsSimpleTag() &&
tagInfo.getBodyContent().equalsIgnoreCase(TagInfo.BODY_CONTENT_JSP)) {
err.jspError(n, "jsp.error.simpletag.badbodycontent",
tagInfo.getTagClassName());
}
/*
* If the tag handler declares in the TLD that it supports dynamic
* attributes, it also must implement the DynamicAttributes
* interface.
*/
if (tagInfo.hasDynamicAttributes()
&& !n.implementsDynamicAttributes()) {
err.jspError(n, "jsp.error.dynamic.attributes.not.implemented",
n.getQName());
}
/*
* Make sure all required attributes are present, either as
* attributes or named attributes (<jsp:attribute>).
* Also make sure that the same attribute is not specified in
* both attributes or named attributes.
*/
TagAttributeInfo[] tldAttrs = tagInfo.getAttributes();
String customActionUri = n.getURI();
Attributes attrs = n.getAttributes();
int attrsSize = (attrs == null) ? 0 : attrs.getLength();
for (int i=0; i<tldAttrs.length; i++) {
String attr = null;
if (attrs != null) {
attr = attrs.getValue(tldAttrs[i].getName());
if (attr == null) {
attr = attrs.getValue(customActionUri,
tldAttrs[i].getName());
}
}
Node.NamedAttribute na =
n.getNamedAttributeNode(tldAttrs[i].getName());
if (tldAttrs[i].isRequired() && attr == null && na == null) {
err.jspError(n, "jsp.error.missing_attribute",
tldAttrs[i].getName(), n.getLocalName());
}
if (attr != null && na != null) {
err.jspError(n, "jsp.error.duplicate.name.jspattribute",
tldAttrs[i].getName());
}
}
Node.Nodes naNodes = n.getNamedAttributeNodes();
int jspAttrsSize = naNodes.size() + attrsSize;
Node.JspAttribute[] jspAttrs = null;
if (jspAttrsSize > 0) {
jspAttrs = new Node.JspAttribute[jspAttrsSize];
}
Hashtable tagDataAttrs = new Hashtable(attrsSize);
checkXmlAttributes(n, jspAttrs, tagDataAttrs);
checkNamedAttributes(n, jspAttrs, attrsSize, tagDataAttrs);
TagData tagData = new TagData(tagDataAttrs);
// JSP.C1: It is a (translation time) error for an action that
// has one or more variable subelements to have a TagExtraInfo
// class that returns a non-null object.
TagExtraInfo tei = tagInfo.getTagExtraInfo();
if (tei != null
&& tei.getVariableInfo(tagData) != null
&& tei.getVariableInfo(tagData).length > 0
&& tagInfo.getTagVariableInfos().length > 0) {
err.jspError("jsp.error.non_null_tei_and_var_subelems",
n.getQName());
}
n.setTagData(tagData);
n.setJspAttributes(jspAttrs);
visitBody(n);
}
public void visit(Node.JspElement n) throws JasperException {
Attributes attrs = n.getAttributes();
if (attrs == null) {
err.jspError(n, "jsp.error.jspelement.missing.name");
}
int xmlAttrLen = attrs.getLength();
Node.Nodes namedAttrs = n.getNamedAttributeNodes();
// XML-style 'name' attribute, which is mandatory, must not be
// included in JspAttribute array
int jspAttrSize = xmlAttrLen-1 + namedAttrs.size();
Node.JspAttribute[] jspAttrs = new Node.JspAttribute[jspAttrSize];
int jspAttrIndex = 0;
// Process XML-style attributes
for (int i=0; i<xmlAttrLen; i++) {
if ("name".equals(attrs.getLocalName(i))) {
n.setNameAttribute(getJspAttribute(attrs.getQName(i),
attrs.getURI(i),
attrs.getLocalName(i),
attrs.getValue(i),
java.lang.String.class,
n,
false));
} else {
if (jspAttrIndex<jspAttrSize) {
jspAttrs[jspAttrIndex++]
= getJspAttribute(attrs.getQName(i),
attrs.getURI(i),
attrs.getLocalName(i),
attrs.getValue(i),
java.lang.Object.class,
n,
false);
}
}
}
if (n.getNameAttribute() == null) {
err.jspError(n, "jsp.error.jspelement.missing.name");
}
// Process named attributes
for (int i=0; i<namedAttrs.size(); i++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -