📄 validator.java
字号:
Node.NamedAttribute na = (Node.NamedAttribute) namedAttrs.getNode(i);
jspAttrs[jspAttrIndex++] = new Node.JspAttribute(na, false);
}
n.setJspAttributes(jspAttrs);
visitBody(n);
}
public void visit(Node.JspOutput n) throws JasperException {
JspUtil.checkAttributes("jsp:output", n, jspOutputAttrs, err);
if (n.getBody() != null) {
err.jspError(n, "jsp.error.jspoutput.nonemptybody");
}
String omitXmlDecl = n.getAttributeValue("omit-xml-declaration");
String doctypeName = n.getAttributeValue("doctype-root-element");
String doctypePublic = n.getAttributeValue("doctype-public");
String doctypeSystem = n.getAttributeValue("doctype-system");
String omitXmlDeclOld = pageInfo.getOmitXmlDecl();
String doctypeNameOld = pageInfo.getDoctypeName();
String doctypePublicOld = pageInfo.getDoctypePublic();
String doctypeSystemOld = pageInfo.getDoctypeSystem();
if (omitXmlDecl != null && omitXmlDeclOld != null &&
!omitXmlDecl.equals(omitXmlDeclOld) ) {
err.jspError(n, "jsp.error.jspoutput.conflict",
"omit-xml-declaration", omitXmlDeclOld, omitXmlDecl);
}
if (doctypeName != null && doctypeNameOld != null &&
!doctypeName.equals(doctypeNameOld) ) {
err.jspError(n, "jsp.error.jspoutput.conflict",
"doctype-root-element", doctypeNameOld, doctypeName);
}
if (doctypePublic != null && doctypePublicOld != null &&
!doctypePublic.equals(doctypePublicOld) ) {
err.jspError(n, "jsp.error.jspoutput.conflict",
"doctype-public", doctypePublicOld, doctypePublic);
}
if (doctypeSystem != null && doctypeSystemOld != null &&
!doctypeSystem.equals(doctypeSystemOld) ) {
err.jspError(n, "jsp.error.jspoutput.conflict",
"doctype-system", doctypeSystemOld, doctypeSystem);
}
if (doctypeName == null && doctypeSystem != null ||
doctypeName != null && doctypeSystem == null) {
err.jspError(n, "jsp.error.jspoutput.doctypenamesystem");
}
if (doctypePublic != null && doctypeSystem == null) {
err.jspError(n, "jsp.error.jspoutput.doctypepulicsystem");
}
if (omitXmlDecl != null) {
pageInfo.setOmitXmlDecl(omitXmlDecl);
}
if (doctypeName != null) {
pageInfo.setDoctypeName(doctypeName);
}
if (doctypeSystem != null) {
pageInfo.setDoctypeSystem(doctypeSystem);
}
if (doctypePublic != null) {
pageInfo.setDoctypePublic(doctypePublic);
}
}
public void visit(Node.InvokeAction n) throws JasperException {
JspUtil.checkAttributes("Invoke", n, invokeAttrs, err);
String scope = n.getTextAttribute ("scope");
JspUtil.checkScope(scope, n, err);
String var = n.getTextAttribute("var");
String varReader = n.getTextAttribute("varReader");
if (scope != null && var == null && varReader == null) {
err.jspError(n, "jsp.error.missing_var_or_varReader");
}
if (var != null && varReader != null) {
err.jspError(n, "jsp.error.var_and_varReader");
}
}
public void visit(Node.DoBodyAction n) throws JasperException {
JspUtil.checkAttributes("DoBody", n, doBodyAttrs, err);
String scope = n.getTextAttribute ("scope");
JspUtil.checkScope(scope, n, err);
String var = n.getTextAttribute("var");
String varReader = n.getTextAttribute("varReader");
if (scope != null && var == null && varReader == null) {
err.jspError(n, "jsp.error.missing_var_or_varReader");
}
if (var != null && varReader != null) {
err.jspError(n, "jsp.error.var_and_varReader");
}
}
/*
* Make sure the given custom action does not have any invalid
* attributes.
*
* A custom action and its declared attributes always belong to the
* same namespace, which is identified by the prefix name of the
* custom tag invocation. For example, in this invocation:
*
* <my:test a="1" b="2" c="3"/>, the action
*
* "test" and its attributes "a", "b", and "c" all belong to the
* namespace identified by the prefix "my". The above invocation would
* be equivalent to:
*
* <my:test my:a="1" my:b="2" my:c="3"/>
*
* An action attribute may have a prefix different from that of the
* action invocation only if the underlying tag handler supports
* dynamic attributes, in which case the attribute with the different
* prefix is considered a dynamic attribute.
*/
private void checkXmlAttributes(Node.CustomTag n,
Node.JspAttribute[] jspAttrs,
Hashtable tagDataAttrs)
throws JasperException {
TagInfo tagInfo = n.getTagInfo();
if (tagInfo == null) {
err.jspError(n, "jsp.error.missing.tagInfo", n.getQName());
}
TagAttributeInfo[] tldAttrs = tagInfo.getAttributes();
Attributes attrs = n.getAttributes();
for (int i=0; attrs != null && i<attrs.getLength(); i++) {
boolean found = false;
for (int j=0; tldAttrs != null && j<tldAttrs.length; j++) {
if (attrs.getLocalName(i).equals(tldAttrs[j].getName())
&& (attrs.getURI(i) == null
|| attrs.getURI(i).length() == 0
|| attrs.getURI(i).equals(n.getURI()))) {
if (tldAttrs[j].canBeRequestTime()) {
Class expectedType = String.class;
try {
String typeStr = tldAttrs[j].getTypeName();
if( tldAttrs[j].isFragment() ) {
expectedType = JspFragment.class;
}
else if( typeStr != null ) {
expectedType = JspUtil.toClass(typeStr,
loader);
}
jspAttrs[i]
= getJspAttribute(attrs.getQName(i),
attrs.getURI(i),
attrs.getLocalName(i),
attrs.getValue(i),
expectedType,
n,
false);
} catch (ClassNotFoundException e) {
err.jspError(n,
"jsp.error.unknown_attribute_type",
tldAttrs[j].getName(),
tldAttrs[j].getTypeName() );
}
} else {
// Attribute does not accept any expressions.
// Make sure its value does not contain any.
if (isExpression(n, attrs.getValue(i))) {
err.jspError(n,
"jsp.error.attribute.custom.non_rt_with_expr",
tldAttrs[j].getName());
}
jspAttrs[i]
= new Node.JspAttribute(attrs.getQName(i),
attrs.getURI(i),
attrs.getLocalName(i),
attrs.getValue(i),
false,
null,
false);
}
if (jspAttrs[i].isExpression()) {
tagDataAttrs.put(attrs.getQName(i),
TagData.REQUEST_TIME_VALUE);
} else {
tagDataAttrs.put(attrs.getQName(i),
attrs.getValue(i));
}
found = true;
break;
}
}
if (!found) {
if (tagInfo.hasDynamicAttributes()) {
jspAttrs[i] = getJspAttribute(attrs.getQName(i),
attrs.getURI(i),
attrs.getLocalName(i),
attrs.getValue(i),
java.lang.Object.class,
n,
true);
} else {
err.jspError(n, "jsp.error.bad_attribute",
attrs.getQName(i), n.getLocalName());
}
}
}
}
/*
* Make sure the given custom action does not have any invalid named
* attributes
*/
private void checkNamedAttributes(Node.CustomTag n,
Node.JspAttribute[] jspAttrs,
int start,
Hashtable tagDataAttrs)
throws JasperException {
TagInfo tagInfo = n.getTagInfo();
if (tagInfo == null) {
err.jspError(n, "jsp.error.missing.tagInfo", n.getQName());
}
TagAttributeInfo[] tldAttrs = tagInfo.getAttributes();
Node.Nodes naNodes = n.getNamedAttributeNodes();
for (int i=0; i<naNodes.size(); i++) {
Node.NamedAttribute na = (Node.NamedAttribute)
naNodes.getNode(i);
boolean found = false;
for (int j=0; j<tldAttrs.length; j++) {
/*
* See above comment about namespace matches. For named
* attributes, we use the prefix instead of URI as the
* match criterion, because in the case of a JSP document,
* we'd have to keep track of which namespaces are in scope
* when parsing a named attribute, in order to determine
* the URI that the prefix of the named attribute's name
* matches to.
*/
String attrPrefix = na.getPrefix();
if (na.getLocalName().equals(tldAttrs[j].getName())
&& (attrPrefix == null || attrPrefix.length() == 0
|| attrPrefix.equals(n.getPrefix()))) {
jspAttrs[start + i] = new Node.JspAttribute(na, false);
NamedAttributeVisitor nav = null;
if (na.getBody() != null) {
nav = new NamedAttributeVisitor();
na.getBody().visit(nav);
}
if (nav != null && nav.hasDynamicContent()) {
tagDataAttrs.put(na.getName(),
TagData.REQUEST_TIME_VALUE);
} else {
tagDataAttrs.put(na.getName(), na.getText());
}
found = true;
break;
}
}
if (!found) {
if (tagInfo.hasDynamicAttributes()) {
jspAttrs[start + i] = new Node.JspAttribute(na, true);
} else {
err.jspError(n, "jsp.error.bad_attribute",
na.getName(), n.getLocalName());
}
}
}
}
/**
* Preprocess attributes that can be expressions. Expression
* delimiters are stripped.
* <p>
* If value is null, checks if there are any
* NamedAttribute subelements in the tree node, and if so,
* constructs a JspAttribute out of a child NamedAttribute node.
*/
private Node.JspAttribute getJspAttribute(String qName,
String uri,
String localName,
String value,
Class expectedType,
Node n,
boolean dynamic)
throws JasperException {
Node.JspAttribute result = null;
// XXX Is it an error to see "%=foo%" in non-Xml page?
// (We won't see "<%=foo%> in xml page because '<' is not a
// valid attribute value in xml).
if (value != null) {
if (n.getRoot().isXmlSyntax() && value.startsWith("%=")) {
result = new Node.JspAttribute(
qName,
uri,
localName,
value.substring(2, value.length()-1),
true,
null,
dynamic);
}
else if(!n.getRoot().isXmlSyntax() && value.startsWith("<%=")) {
result = new Node.JspAttribute(
qName,
uri,
localName,
value.substring(3, value.length()-2),
true,
null,
dynamic);
}
else {
// The attribute can contain expressions but is not a
// scriptlet expression; thus, we want to run it through
// the expression interpreter
// validate expression syntax if string contains
// expression(s)
ELNode.Nodes el = ELParser.parse(value);
if (el.containsEL() && !pageInfo.isELIgnored()) {
validateFunctions(el, n);
JspUtil.validateExpressions(
n.getStart(),
value,
expectedType,
getFunctionMapper(el),
this.err);
result = new Node.JspAttribute(qName, uri, localName,
value, false, el,
dynamic);
} else {
value = value.replace(Constants.ESC, '$');
result = new Node.JspAttribute(qName, uri, localName,
value, false, null,
dynamic);
}
}
}
else {
// Value is null. Check for any NamedAttribute subnodes
// that might contain the value for this attribute.
// Otherwise, the attribute wasn't found so we return null.
Node.NamedAttribute namedAttributeNode =
n.getNamedAttributeNode( qName );
if( namedAttributeNode != null ) {
result = new Node.JspAttribute(namedAttributeNode,
dynamic);
}
}
return result;
}
/*
* Checks to see if the given attribute value represents a runtime or
* EL expression.
*/
private boolean isExpression(Node n, String value) {
if ((n.getRoot().isXmlSyntax() && value.startsWith("%="))
|| (!n.getRoot().isXmlSyntax() && value.startsWith("<%="))
|| (value.indexOf("${") != -1 && !pageInfo.isELIgnored()))
return true;
else
return false;
}
/*
* Throws exception if the value of the attribute with the given
* name in the given node is given as an RT or EL expression, but the
* spec requires a static value.
*/
private void throwErrorIfExpression(Node n, String attrName,
String actionName)
throws JasperException {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -