Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit 9b074a7

Browse files
author
Yuri Nesterenko
committed
8285727: [11u, 17u] Unify fix for JDK-8284920 with version from head
Reviewed-by: bae
1 parent 47b35cc commit 9b074a7

File tree

4 files changed

+132
-10
lines changed

4 files changed

+132
-10
lines changed

src/java.xml/share/classes/com/sun/org/apache/xpath/internal/compiler/Lexer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ else if ((Token.LPAREN != c) && (Token.RPAREN != c) && (Token.RBRACK != c)) {
360360

361361
addToTokenQueue(pat.substring(i, i + 1));
362362
break;
363-
case Token.COLON_CHAR:
363+
case Token.COLON :
364364
if (i>0)
365365
{
366366
if (posOfNSSep == (i - 1))
@@ -615,7 +615,7 @@ private void recordTokenString(List<String> targetStrings)
615615
resetTokenMark(tokPos + 1);
616616
}
617617

618-
if (m_processor.lookahead(Token.COLON_CHAR, 1))
618+
if (m_processor.lookahead(Token.COLON, 1))
619619
{
620620
tokPos += 2;
621621
}

src/java.xml/share/classes/com/sun/org/apache/xpath/internal/compiler/Token.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ public final class Token {
4545
static final char LPAREN = '(';
4646
static final char RPAREN = ')';
4747
static final char COMMA = ',';
48+
static final char DOT = '.';
4849
static final char AT = '@';
4950
static final char US = '_';
50-
static final char COLON_CHAR = ':';
51+
static final char COLON = ':';
5152
static final char SQ = '\'';
5253
static final char DQ = '"';
5354
static final char DOLLAR = '$';
@@ -57,7 +58,7 @@ public final class Token {
5758
static final String DIV = "div";
5859
static final String MOD = "mod";
5960
static final String QUO = "quo";
60-
static final String DOT = ".";
61+
static final String DOT_STR = ".";
6162
static final String DDOT = "..";
6263
static final String DCOLON = "::";
6364
static final String ATTR = "attribute";

src/java.xml/share/classes/com/sun/org/apache/xpath/internal/compiler/XPathParser.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
* Tokenizes and parses XPath expressions. This should really be named
3636
* XPathParserImpl, and may be renamed in the future.
3737
* @xsl.usage general
38-
* @LastModified: Jan 2022
38+
* @LastModified: Apr 2022
3939
*/
4040
public class XPathParser
4141
{
@@ -1413,7 +1413,7 @@ else if ((null != m_token) && ((('.' == m_tokenChar) && (m_token.length() > 1) &
14131413

14141414
matchFound = true;
14151415
}
1416-
else if (lookahead(Token.LPAREN, 1) || (lookahead(Token.COLON_CHAR, 1) && lookahead(Token.LPAREN, 3)))
1416+
else if (lookahead(Token.LPAREN, 1) || (lookahead(Token.COLON, 1) && lookahead(Token.LPAREN, 3)))
14171417
{
14181418
matchFound = FunctionCall();
14191419
}
@@ -1457,7 +1457,7 @@ protected boolean FunctionCall() throws TransformerException
14571457

14581458
int opPos = m_ops.getOp(OpMap.MAPINDEX_LENGTH);
14591459

1460-
if (lookahead(Token.COLON_CHAR, 1))
1460+
if (lookahead(Token.COLON, 1))
14611461
{
14621462
appendOp(4, OpCodes.OP_EXTFUNCTION);
14631463

@@ -1661,7 +1661,7 @@ protected boolean Step() throws TransformerException
16611661
opPos = m_ops.getOp(OpMap.MAPINDEX_LENGTH);
16621662
}
16631663

1664-
if (tokenIs(Token.DOT))
1664+
if (tokenIs(Token.DOT_STR))
16651665
{
16661666
nextToken();
16671667

@@ -1841,7 +1841,7 @@ protected void NodeTest(int axesType) throws TransformerException
18411841
m_ops.setOp(m_ops.getOp(OpMap.MAPINDEX_LENGTH), OpCodes.NODENAME);
18421842
m_ops.setOp(OpMap.MAPINDEX_LENGTH, m_ops.getOp(OpMap.MAPINDEX_LENGTH) + 1);
18431843

1844-
if (lookahead(Token.COLON_CHAR, 1))
1844+
if (lookahead(Token.COLON, 1))
18451845
{
18461846
if (tokenIs(Token.STAR))
18471847
{
@@ -1944,7 +1944,7 @@ protected void PredicateExpr() throws TransformerException
19441944
protected void QName() throws TransformerException
19451945
{
19461946
// Namespace
1947-
if(lookahead(Token.COLON_CHAR, 1))
1947+
if(lookahead(Token.COLON, 1))
19481948
{
19491949
m_ops.setOp(m_ops.getOp(OpMap.MAPINDEX_LENGTH), m_queueMark - 1);
19501950
m_ops.setOp(OpMap.MAPINDEX_LENGTH, m_ops.getOp(OpMap.MAPINDEX_LENGTH) + 1);
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
package xpath;
24+
25+
import java.io.ByteArrayInputStream;
26+
import java.io.InputStream;
27+
import javax.xml.parsers.DocumentBuilder;
28+
import javax.xml.parsers.DocumentBuilderFactory;
29+
import javax.xml.xpath.XPath;
30+
import javax.xml.xpath.XPathConstants;
31+
import javax.xml.xpath.XPathFactory;
32+
import org.testng.Assert;
33+
import org.testng.annotations.DataProvider;
34+
import org.testng.annotations.Test;
35+
import org.w3c.dom.Document;
36+
import org.w3c.dom.Node;
37+
import org.w3c.dom.NodeList;
38+
39+
/*
40+
* @test
41+
* @bug 8284920
42+
* @run testng/othervm xpath.XPathExpTest
43+
* @summary Tests for various XPath Expressions.
44+
*/
45+
public class XPathExpTest {
46+
47+
private static final String XML =
48+
"<root>"
49+
+ " <child id='1'>"
50+
+ " <grandchild id='1'/>"
51+
+ " <grandchild id='2'/>"
52+
+ " </child>"
53+
+ " <child id='2'>"
54+
+ " <grandchild id='3'/>"
55+
+ " <grandchild id='4'/>"
56+
+ " </child>"
57+
+ " <child id='3'>"
58+
+ " <grandchild id='5'/>"
59+
+ " <grandchild id='6'/>"
60+
+ " </child>"
61+
+ " </root>";
62+
private static final String PARENT_CHILD = "child(2)";
63+
64+
/*
65+
* DataProvider for XPath expression test.
66+
* Data columns:
67+
* see parameters of the test "test"
68+
*/
69+
@DataProvider(name = "xpathExp")
70+
public Object[][] getXPathExpression() throws Exception {
71+
72+
return new Object[][]{
73+
// verifies various forms of the parent axis
74+
{"/root/child[@id='2']", PARENT_CHILD},
75+
{"//grandchild[@id='3']/parent::child", PARENT_CHILD},
76+
{"//grandchild[@id='3']/parent::node()", PARENT_CHILD},
77+
{"//grandchild[@id='3']/parent::*", PARENT_CHILD},
78+
{"//grandchild[@id='3']/parent::node()/grandchild[@id='4']/parent::node()", PARENT_CHILD},
79+
{"//grandchild[@id='3']/..", PARENT_CHILD},
80+
{"//grandchild[@id='3']/../grandchild[@id='4']/..", PARENT_CHILD},
81+
{"//grandchild[@id='3']/parent::node()/grandchild[@id='4']/..", PARENT_CHILD},
82+
83+
// verifies various forms of the self axis
84+
{"/root/child[@id='2']/self::child", PARENT_CHILD},
85+
{"/root/child[@id='2']/self::node()", PARENT_CHILD},
86+
{"/root/child[@id='2']/self::*", PARENT_CHILD},
87+
{"self::node()/root/child[@id='2']", PARENT_CHILD},
88+
{"/root/child[@id='2']/.", PARENT_CHILD},
89+
{"./root/child[@id='2']", PARENT_CHILD},
90+
{".//child[@id='2']", PARENT_CHILD},
91+
{"//grandchild[@id='3']/./../grandchild[@id='4']/..", PARENT_CHILD},
92+
{"//grandchild[@id='3']/./parent::node()/grandchild[@id='4']/..", PARENT_CHILD},
93+
};
94+
}
95+
96+
/**
97+
* Verifies XPath expressions.
98+
*
99+
* @param exp XPath expression
100+
* @param expected expected result
101+
* @throws Exception
102+
*/
103+
@Test(dataProvider = "xpathExp")
104+
void test(String exp, String expected) throws Exception {
105+
Document doc = getDoc(XML);
106+
XPath xPath = XPathFactory.newInstance().newXPath();
107+
NodeList nl = (NodeList) xPath.evaluate(exp, doc, XPathConstants.NODESET);
108+
Node child = nl.item(0);
109+
Assert.assertEquals(
110+
child.getNodeName() + "(" + child.getAttributes().item(0).getNodeValue() + ")",
111+
expected);
112+
}
113+
114+
Document getDoc(String xml) throws Exception {
115+
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
116+
dfactory.setNamespaceAware(true);
117+
DocumentBuilder docBuilder = dfactory.newDocumentBuilder();
118+
InputStream is = new ByteArrayInputStream(xml.getBytes());
119+
return docBuilder.parse(is);
120+
}
121+
}

0 commit comments

Comments
 (0)