From b2069445098a7dd0f1b41b1894aff548247ea22e Mon Sep 17 00:00:00 2001 From: markt Date: Mon, 30 Aug 2010 22:32:08 +0000 Subject: [PATCH] Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=49799 The new omit< attribute for elements now supports the use of expressions and expression language. git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@991011 13f79535-47bb-0310-9956-ffa450edef68 --- java/org/apache/jasper/compiler/Generator.java | 28 ++++++++++----- java/org/apache/jasper/compiler/Node.java | 12 +++---- java/org/apache/jasper/compiler/Validator.java | 2 ++ test/org/apache/jasper/compiler/TestGenerator.java | 38 +++++++++++++++++++- test/webapp-3.0/bug49799.jsp | 40 ++++++++++++++++++++++ webapps/docs/changelog.xml | 5 +++ 6 files changed, 109 insertions(+), 16 deletions(-) create mode 100644 test/webapp-3.0/bug49799.jsp diff --git a/java/org/apache/jasper/compiler/Generator.java b/java/org/apache/jasper/compiler/Generator.java index b38477715..8b25f5e09 100644 --- a/java/org/apache/jasper/compiler/Generator.java +++ b/java/org/apache/jasper/compiler/Generator.java @@ -1861,20 +1861,30 @@ class Generator { Hashtable map = new Hashtable(); Node.JspAttribute[] attrs = n.getJspAttributes(); for (int i = 0; attrs != null && i < attrs.length; i++) { - String attrStr = null; + String value = null; + String nvp = null; if (attrs[i].isNamedAttribute()) { - if (attrs[i].getNamedAttributeNode().isOmit()) { - // Skip this attribute - JSP.5-7 + NamedAttribute attr = attrs[i].getNamedAttributeNode(); + Node.JspAttribute omitAttr = attr.getOmit(); + String omit = attributeValue(omitAttr, false, boolean.class); + if ("true".equals(omit)) { continue; } - attrStr = generateNamedAttributeValue(attrs[i] - .getNamedAttributeNode()); + value = generateNamedAttributeValue( + attrs[i].getNamedAttributeNode()); + if ("false".equals(omit)) { + nvp = " + \" " + attrs[i].getName() + "=\\\"\" + " + + value + " + \"\\\"\""; + } else { + nvp = " + (Boolean.valueOf(" + omit + ")?\"\":\" " + attrs[i].getName() + + "=\\\"\" + " + value + " + \"\\\"\")"; + } } else { - attrStr = attributeValue(attrs[i], false, Object.class); + value = attributeValue(attrs[i], false, Object.class); + nvp = " + \" " + attrs[i].getName() + "=\\\"\" + " + + value + " + \"\\\"\""; } - String s = " + \" " + attrs[i].getName() + "=\\\"\" + " - + attrStr + " + \"\\\"\""; - map.put(attrs[i].getName(), s); + map.put(attrs[i].getName(), nvp); } // Write begin tag, using XML-style 'name' attribute as the diff --git a/java/org/apache/jasper/compiler/Node.java b/java/org/apache/jasper/compiler/Node.java index 7ce8376b1..77721ed24 100644 --- a/java/org/apache/jasper/compiler/Node.java +++ b/java/org/apache/jasper/compiler/Node.java @@ -1866,7 +1866,7 @@ abstract class Node implements TagConstants { // True if this attribute should be omitted from the output if // used with a , otherwise false - private boolean omit = false; + private JspAttribute omit; private ChildInfo childInfo; @@ -1890,10 +1890,6 @@ abstract class Node implements TagConstants { // (if null or true, leave default of true) trim = false; } - if ("true".equals(this.getAttributeValue("omit"))) { - // (if null or false, leave default of false) - omit = true; - } childInfo = new ChildInfo(); name = this.getAttributeValue("name"); if (name != null) { @@ -1933,7 +1929,11 @@ abstract class Node implements TagConstants { return trim; } - public boolean isOmit() { + public void setOmit(JspAttribute omit) { + this.omit = omit; + } + + public JspAttribute getOmit() { return omit; } diff --git a/java/org/apache/jasper/compiler/Validator.java b/java/org/apache/jasper/compiler/Validator.java index 8af6d6496..6d68d3b47 100644 --- a/java/org/apache/jasper/compiler/Validator.java +++ b/java/org/apache/jasper/compiler/Validator.java @@ -685,6 +685,8 @@ class Validator { @Override public void visit(Node.NamedAttribute n) throws JasperException { JspUtil.checkAttributes("Attribute", n, attributeAttrs, err); + n.setOmit(getJspAttribute(null, "omit", null, null, n + .getAttributeValue("omit"), n, true)); visitBody(n); } diff --git a/test/org/apache/jasper/compiler/TestGenerator.java b/test/org/apache/jasper/compiler/TestGenerator.java index 1cd3f81c0..1d80c51ce 100644 --- a/test/org/apache/jasper/compiler/TestGenerator.java +++ b/test/org/apache/jasper/compiler/TestGenerator.java @@ -20,6 +20,9 @@ package org.apache.jasper.compiler; import java.io.File; import java.io.IOException; import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.TagData; @@ -211,7 +214,40 @@ public class TestGenerator extends TomcatBaseTest { return time; } } - + + public void testBug49799() throws Exception { + + String[] expected = { "

00-Red

", + "

01-Not Red

", + "

02-Red

", + "

03-Not Red

", + "

04-Red

", + "

05-Not Red

"}; + + Tomcat tomcat = getTomcatInstance(); + + File appDir = new File("test/webapp-3.0"); + tomcat.addWebapp(null, "/test", appDir.getAbsolutePath()); + tomcat.start(); + + ByteChunk res = new ByteChunk(); + Map> headers = new HashMap>(); + + getUrl("http://localhost:" + getPort() + "/test/bug49799.jsp", res, + headers); + + // Check request completed + String result = res.toString(); + String[] lines = result.split("\n|\r|\r\n"); + int i = 0; + for (String line : lines) { + if (line.length() > 0) { + assertEquals(expected[i], line); + i++; + } + } + } + /** Assertion for text printed by tags:echo */ private static void assertEcho(String result, String expected) { assertTrue(result.indexOf("

" + expected + "

") > 0); diff --git a/test/webapp-3.0/bug49799.jsp b/test/webapp-3.0/bug49799.jsp new file mode 100644 index 000000000..8fd39dbe9 --- /dev/null +++ b/test/webapp-3.0/bug49799.jsp @@ -0,0 +1,40 @@ +<%-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You 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. +--%> + + color:red + 00-Red + + + color:red + 01-Not Red + + + color:red + 02-Red + + + color:red + 03-Not Red + + + color:red + 04-Red + + + color:red + 05-Not Red + \ No newline at end of file diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index 1aa146b01..48a999a1d 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -119,6 +119,11 @@ group should not prevent a page from setting some other content type. (markt) + + 49799: The new omit attribute for + jsp:attribute elements now supports the use of expressions + and expression language. (markt) + -- 2.11.0