View | Details | Raw Unified | Return to bug 49297
Collapse All | Expand All

(-)a/java/org/apache/jasper/util/UniqueAttributesImpl.java (-17 / +69 lines)
Lines 16-23 Link Here
16
 */
16
 */
17
package org.apache.jasper.util;
17
package org.apache.jasper.util;
18
18
19
import java.util.HashSet;
19
import java.util.HashMap;
20
import java.util.Set;
20
import java.util.Map;
21
21
22
import org.apache.jasper.compiler.Localizer;
22
import org.apache.jasper.compiler.Localizer;
23
import org.xml.sax.Attributes;
23
import org.xml.sax.Attributes;
Lines 29-82 import org.xml.sax.helpers.AttributesImpl; Link Here
29
 */
29
 */
30
public class UniqueAttributesImpl extends AttributesImpl {
30
public class UniqueAttributesImpl extends AttributesImpl {
31
31
32
    private Set<String> qNames = new HashSet<String>();
32
    private Map<String,String> attributes = new HashMap<String,String>();
33
33
34
    @Override
34
    @Override
35
    public void clear() {
35
    public void clear() {
36
        qNames.clear();
36
        attributes.clear();
37
        super.clear();
37
        super.clear();
38
    }
38
    }
39
39
40
    @Override
40
    @Override
41
    public void setAttributes(Attributes atts) {
41
    public void setAttributes(Attributes atts) {
42
        
42
        for (int i = 0; i < atts.getLength(); i++) {
43
        for (int i = 0; i < atts.getLength(); i++) {
43
            if (!qNames.add(atts.getQName(i))) {
44
            
44
                handleDuplicate(atts.getQName(i));
45
            String qName = atts.getQName(i);
46
            String value = atts.getValue(i);
47
48
            /*
49
             * A translation error will occur if the page directive defines duplicate attribute/values within a given
50
             * translation unit, unless the values for the duplicate attributes are identical for all occurrences. The
51
             * import and pageEncoding attributes are exempt from this rule and can appear multiple times.
52
             */
53
            if (attributes.keySet().contains(qName)) {
54
55
    /*            if (qName.equals("import")) {
56
57
                    StringBuffer sb = new StringBuffer(3);
58
                    sb.append(attributes.get(qName));
59
                    sb.append(",");
60
                    sb.append(value);
61
                    attributes.put(qName, sb.toString());
62
63
                } else*/
64
                    if (qName.equals("pageEncoding")) {
65
66
                    // It's not clear in the spec how would we handle multiple pageEncoding attributes.
67
                    
68
                } else if (!attributes.get(qName).equals(value)) {
69
70
                    handleDuplicate(qName);
71
                } 
72
73
            } else {
74
                attributes.put(qName, value);
45
            }
75
            }
76
            
46
        }
77
        }
78
        
47
        super.setAttributes(atts);
79
        super.setAttributes(atts);
48
    }
80
    }
49
81
50
    @Override
82
    @Override
51
    public void addAttribute(String uri, String localName, String qName,
83
    public void addAttribute(String uri, String localName, String qName,
52
            String type, String value) {
84
            String type, String value) {
53
        if (qNames.add(qName)) {
85
        
54
            super.addAttribute(uri, localName, qName, type, value);
86
        if (attributes.keySet().contains(qName)) {
87
88
            if (qName.equals("import")) {
89
90
                StringBuffer sb = new StringBuffer(3);
91
                sb.append(attributes.get(qName));
92
                sb.append(",");
93
                sb.append(value);
94
                attributes.put(qName, sb.toString());
95
                super.addAttribute(uri, localName, qName, type, sb.toString());
96
97
            } else if (qName.equals("pageEncoding")) {
98
99
                // It's not clear in the spec on how to handle multiple pageEncoding attributes.
100
                
101
            } else if (!attributes.get(qName).equals(value)) {
102
103
                handleDuplicate(qName);
104
            } 
105
55
        } else {
106
        } else {
56
            handleDuplicate(qName);
107
            attributes.put(qName, value);
108
            super.addAttribute(uri, localName, qName, type, value);
57
        }
109
        }
110
       
58
    }
111
    }
59
112
60
    @Override
113
    @Override
61
    public void setAttribute(int index, String uri, String localName,
114
    public void setAttribute(int index, String uri, String localName,
62
            String qName, String type, String value) {
115
            String qName, String type, String value) {
63
        qNames.remove(super.getQName(index));
116
        
64
        if (qNames.add(qName)) {
117
        attributes.remove(super.getQName(index));
65
            super.setAttribute(index, uri, localName, qName, type, value);
118
        attributes.put(qName, value);
66
        } else {
119
        super.setAttribute(index, uri, localName, qName, type, value);
67
            handleDuplicate(qName);
120
68
        }
69
    }
121
    }
70
122
71
    @Override
123
    @Override
72
    public void removeAttribute(int index) {
124
    public void removeAttribute(int index) {
73
        qNames.remove(super.getQName(index));
125
        attributes.remove(super.getQName(index));
74
        super.removeAttribute(index);
126
        super.removeAttribute(index);
75
    }
127
    }
76
128
77
    @Override
129
    @Override
78
    public void setQName(int index, String qName) {
130
    public void setQName(int index, String qName) {
79
        qNames.remove(super.getQName(index));
131
        attributes.remove(super.getQName(index));
80
        super.setQName(index, qName);
132
        super.setQName(index, qName);
81
    }
133
    }
82
134
(-)a/test/org/apache/jasper/compiler/TestParser.java (+34 lines)
Lines 144-149 public class TestParser extends TomcatBaseTest { Link Here
144
        assertEquals(500, sc);
144
        assertEquals(500, sc);
145
    }
145
    }
146
    
146
    
147
    public void testBug49297DuplicateAttr_samevalue() throws Exception {
148
        Tomcat tomcat = getTomcatInstance();
149
150
        File appDir = new File("test/webapp-3.0");
151
        // app dir is relative to server home
152
        tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
153
        
154
        tomcat.start();
155
156
        int sc = getUrl("http://localhost:" + getPort() +
157
                "/test/bug49297DuplicateAttr_samevalue.jsp", new ByteChunk(),
158
                new HashMap<String,List<String>>());
159
160
        assertEquals(200, sc);
161
    }
162
    
163
    public void testBug49297DuplicateAttr_import() throws Exception {
164
        Tomcat tomcat = getTomcatInstance();
165
166
        File appDir = new File("test/webapp-3.0");
167
        // app dir is relative to server home
168
        tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
169
        
170
        tomcat.start();
171
172
        int sc = getUrl("http://localhost:" + getPort() +
173
                "/test/bug49297DuplicateAttr_import.jsp", new ByteChunk(),
174
                new HashMap<String,List<String>>());
175
176
        assertEquals(200, sc);
177
    }
178
    
179
    
180
    
147
    /** Assertion for text printed by tags:echo */
181
    /** Assertion for text printed by tags:echo */
148
    private static void assertEcho(String result, String expected) {
182
    private static void assertEcho(String result, String expected) {
149
        assertTrue(result.indexOf("<p>" + expected + "</p>") > 0);
183
        assertTrue(result.indexOf("<p>" + expected + "</p>") > 0);
(-)a/test/webapp-3.0/bug49297DuplicateAttr_import.jsp (+24 lines)
Line 0 Link Here
1
<%--
2
 Licensed to the Apache Software Foundation (ASF) under one or more
3
  contributor license agreements.  See the NOTICE file distributed with
4
  this work for additional information regarding copyright ownership.
5
  The ASF licenses this file to You under the Apache License, Version 2.0
6
  (the "License"); you may not use this file except in compliance with
7
  the License.  You may obtain a copy of the License at
8
9
      http://www.apache.org/licenses/LICENSE-2.0
10
11
  Unless required by applicable law or agreed to in writing, software
12
  distributed under the License is distributed on an "AS IS" BASIS,
13
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
  See the License for the specific language governing permissions and
15
  limitations under the License.
16
--%>
17
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
18
<%@page import="java.util.List" import="java.util.ArrayList" %>  
19
<html>
20
  <head><title>Bug 49297 duplicate attribute test case - for import directives</title></head>
21
  <body>
22
23
  </body>
24
</html>
(-)a/test/webapp-3.0/bug49297DuplicateAttr_samevalue.jsp (-1 / +23 lines)
Line 0 Link Here
0
- 
1
<%--
2
 Licensed to the Apache Software Foundation (ASF) under one or more
3
  contributor license agreements.  See the NOTICE file distributed with
4
  this work for additional information regarding copyright ownership.
5
  The ASF licenses this file to You under the Apache License, Version 2.0
6
  (the "License"); you may not use this file except in compliance with
7
  the License.  You may obtain a copy of the License at
8
9
      http://www.apache.org/licenses/LICENSE-2.0
10
11
  Unless required by applicable law or agreed to in writing, software
12
  distributed under the License is distributed on an "AS IS" BASIS,
13
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
  See the License for the specific language governing permissions and
15
  limitations under the License.
16
--%>
17
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
18
<html>
19
  <head><title>Bug 49297 duplicate attribute test case</title></head>
20
  <body>
21
    <tags:echo echo="Hello World" echo="Hello World"/>
22
  </body>
23
</html>

Return to bug 49297