diff --git a/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/NodeSet.java b/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/NodeSet.java index aa8eeb170d3..d8dc27c0651 100644 --- a/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/NodeSet.java +++ b/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/NodeSet.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved. */ /* * Licensed to the Apache Software Foundation (ASF) under one or more @@ -55,7 +55,7 @@ import org.w3c.dom.traversal.NodeIterator; * to the same calls; the disadvantage is that some of them may return * less-than-enlightening results when you do so.
* @xsl.usage advanced - * @LastModified: Nov 2017 + * @LastModified: May 2022 */ public class NodeSet implements NodeList, NodeIterator, Cloneable, ContextNodeList @@ -379,11 +379,7 @@ public class NodeSet */ public void addNode(Node n) { - - if (!m_mutable) - throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESET_NOT_MUTABLE, null)); //"This NodeSet is not mutable!"); - - this.addElement(n); + addElement(n); } /** @@ -397,10 +393,6 @@ public class NodeSet */ public void insertNode(Node n, int pos) { - - if (!m_mutable) - throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESET_NOT_MUTABLE, null)); //"This NodeSet is not mutable!"); - insertElementAt(n, pos); } @@ -413,11 +405,7 @@ public class NodeSet */ public void removeNode(Node n) { - - if (!m_mutable) - throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESET_NOT_MUTABLE, null)); //"This NodeSet is not mutable!"); - - this.removeElement(n); + removeElement(n); } /** @@ -431,10 +419,6 @@ public class NodeSet */ public void addNodes(NodeList nodelist) { - - if (!m_mutable) - throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESET_NOT_MUTABLE, null)); //"This NodeSet is not mutable!"); - if (null != nodelist) // defensive to fix a bug that Sanjiva reported. { int nChildren = nodelist.getLength(); @@ -471,10 +455,6 @@ public class NodeSet */ public void addNodes(NodeSet ns) { - - if (!m_mutable) - throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESET_NOT_MUTABLE, null)); //"This NodeSet is not mutable!"); - addNodes((NodeIterator) ns); } @@ -488,10 +468,6 @@ public class NodeSet */ public void addNodes(NodeIterator iterator) { - - if (!m_mutable) - throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESET_NOT_MUTABLE, null)); //"This NodeSet is not mutable!"); - if (null != iterator) // defensive to fix a bug that Sanjiva reported. { Node obj; @@ -516,10 +492,6 @@ public class NodeSet */ public void addNodesInDocOrder(NodeList nodelist, XPathContext support) { - - if (!m_mutable) - throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESET_NOT_MUTABLE, null)); //"This NodeSet is not mutable!"); - int nChildren = nodelist.getLength(); for (int i = 0; i < nChildren; i++) @@ -544,10 +516,6 @@ public class NodeSet */ public void addNodesInDocOrder(NodeIterator iterator, XPathContext support) { - - if (!m_mutable) - throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESET_NOT_MUTABLE, null)); //"This NodeSet is not mutable!"); - Node node; while (null != (node = iterator.nextNode())) @@ -572,10 +540,6 @@ public class NodeSet private boolean addNodesInDocOrder(int start, int end, int testIndex, NodeList nodelist, XPathContext support) { - - if (!m_mutable) - throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESET_NOT_MUTABLE, null)); //"This NodeSet is not mutable!"); - boolean foundit = false; int i; Node node = nodelist.item(testIndex); @@ -632,10 +596,6 @@ public class NodeSet */ public int addNodeInDocOrder(Node node, boolean test, XPathContext support) { - - if (!m_mutable) - throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESET_NOT_MUTABLE, null)); //XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESET_NOT_MUTABLE, null)); //"This NodeSet is not mutable!"); - int insertIndex = -1; if (test) @@ -706,10 +666,6 @@ public class NodeSet */ public int addNodeInDocOrder(Node node, XPathContext support) { - - if (!m_mutable) - throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESET_NOT_MUTABLE, null)); //"This NodeSet is not mutable!"); - return addNodeInDocOrder(node, true, support); } // end addNodeInDocOrder(Vector v, Object obj) @@ -767,9 +723,6 @@ public class NodeSet return n; } - /** True if this list can be mutated. */ - transient protected boolean m_mutable = true; - /** True if this list is cached. * @serial */ transient protected boolean m_cacheNodes = true; @@ -804,7 +757,6 @@ public class NodeSet XSLMessages.createXPATHMessage(XPATHErrorResources.ER_CANNOT_CALL_SETSHOULDCACHENODE, null)); //"Can not call setShouldCacheNodes after nextNode has been called!"); m_cacheNodes = b; - m_mutable = true; } @@ -875,9 +827,6 @@ public class NodeSet */ public void addElement(Node value) { - if (!m_mutable) - throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESET_NOT_MUTABLE, null)); //"This NodeSet is not mutable!"); - if ((m_firstFree + 1) >= m_mapSize) { if (null == m_map) @@ -1102,9 +1051,6 @@ public class NodeSet */ public void insertElementAt(Node value, int at) { - if (!m_mutable) - throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESET_NOT_MUTABLE, null)); //"This NodeSet is not mutable!"); - if (null == m_map) { m_map = new Node[m_blocksize]; @@ -1195,9 +1141,6 @@ public class NodeSet */ public boolean removeElement(Node s) { - if (!m_mutable) - throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESET_NOT_MUTABLE, null)); //"This NodeSet is not mutable!"); - if (null == m_map) return false; @@ -1258,9 +1201,6 @@ public class NodeSet */ public void setElementAt(Node node, int index) { - if (!m_mutable) - throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESET_NOT_MUTABLE, null)); //"This NodeSet is not mutable!"); - if (null == m_map) { m_map = new Node[m_blocksize]; diff --git a/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/NodeSetDTM.java b/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/NodeSetDTM.java index 6b0c1bde1b5..ae0e196f0bb 100644 --- a/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/NodeSetDTM.java +++ b/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/NodeSetDTM.java @@ -1,6 +1,5 @@ /* - * reserved comment block - * DO NOT REMOVE OR ALTER! + * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. */ /* * Licensed to the Apache Software Foundation (ASF) under one or more @@ -57,6 +56,7 @@ import org.w3c.dom.traversal.NodeIterator; * to the same calls; the disadvantage is that some of them may return * less-than-enlightening results when you do so. * @xsl.usage advanced + * @LastModified: May 2022 */ public class NodeSetDTM extends NodeVector implements /* NodeList, NodeIterator, */ DTMIterator, @@ -536,11 +536,7 @@ public class NodeSetDTM extends NodeVector */ public void addNode(int n) { - - if (!m_mutable) - throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!"); - - this.addElement(n); + addElement(n); } /** @@ -554,10 +550,6 @@ public class NodeSetDTM extends NodeVector */ public void insertNode(int n, int pos) { - - if (!m_mutable) - throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!"); - insertElementAt(n, pos); } @@ -570,10 +562,6 @@ public class NodeSetDTM extends NodeVector */ public void removeNode(int n) { - - if (!m_mutable) - throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!"); - this.removeElement(n); } @@ -647,10 +635,6 @@ public class NodeSetDTM extends NodeVector */ public void addNodes(DTMIterator iterator) { - - if (!m_mutable) - throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!"); - if (null != iterator) // defensive to fix a bug that Sanjiva reported. { int obj; @@ -704,10 +688,6 @@ public class NodeSetDTM extends NodeVector */ public void addNodesInDocOrder(DTMIterator iterator, XPathContext support) { - - if (!m_mutable) - throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!"); - int node; while (DTM.NULL != (node = iterator.nextNode())) @@ -793,10 +773,6 @@ public class NodeSetDTM extends NodeVector */ public int addNodeInDocOrder(int node, boolean test, XPathContext support) { - - if (!m_mutable) - throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!"); - int insertIndex = -1; if (test) @@ -868,10 +844,6 @@ public class NodeSetDTM extends NodeVector */ public int addNodeInDocOrder(int node, XPathContext support) { - - if (!m_mutable) - throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!"); - return addNodeInDocOrder(node, true, support); } // end addNodeInDocOrder(Vector v, Object obj) @@ -894,10 +866,6 @@ public class NodeSetDTM extends NodeVector */ public void addElement(int value) { - - if (!m_mutable) - throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!"); - super.addElement(value); } @@ -914,10 +882,6 @@ public class NodeSetDTM extends NodeVector */ public void insertElementAt(int value, int at) { - - if (!m_mutable) - throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!"); - super.insertElementAt(value, at); } @@ -930,10 +894,6 @@ public class NodeSetDTM extends NodeVector */ public void appendNodes(NodeVector nodes) { - - if (!m_mutable) - throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!"); - super.appendNodes(nodes); } @@ -947,10 +907,6 @@ public class NodeSetDTM extends NodeVector */ public void removeAllElements() { - - if (!m_mutable) - throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!"); - super.removeAllElements(); } @@ -969,10 +925,6 @@ public class NodeSetDTM extends NodeVector */ public boolean removeElement(int s) { - - if (!m_mutable) - throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!"); - return super.removeElement(s); } @@ -988,10 +940,6 @@ public class NodeSetDTM extends NodeVector */ public void removeElementAt(int i) { - - if (!m_mutable) - throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!"); - super.removeElementAt(i); } @@ -1009,10 +957,6 @@ public class NodeSetDTM extends NodeVector */ public void setElementAt(int node, int index) { - - if (!m_mutable) - throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!"); - super.setElementAt(node, index); } @@ -1026,10 +970,6 @@ public class NodeSetDTM extends NodeVector */ public void setItem(int node, int index) { - - if (!m_mutable) - throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!"); - super.setElementAt(node, index); } @@ -1157,9 +1097,6 @@ public class NodeSetDTM extends NodeVector return n; } - /** True if this list can be mutated. */ - transient protected boolean m_mutable = true; - /** True if this list is cached. * @serial */ transient protected boolean m_cacheNodes = true; @@ -1197,7 +1134,6 @@ public class NodeSetDTM extends NodeVector XSLMessages.createXPATHMessage(XPATHErrorResources.ER_CANNOT_CALL_SETSHOULDCACHENODE, null)); //"Can not call setShouldCacheNodes after nextNode has been called!"); m_cacheNodes = b; - m_mutable = true; } /** @@ -1208,7 +1144,7 @@ public class NodeSetDTM extends NodeVector */ public boolean isMutable() { - return m_mutable; + return true; } transient private int m_last = 0; diff --git a/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/XPath.java b/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/XPath.java index 18632eab6ab..bd2d5a093a0 100644 --- a/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/XPath.java +++ b/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/XPath.java @@ -42,7 +42,7 @@ import jdk.xml.internal.XMLSecurityManager; * The XPath class wraps an expression object and provides general services * for execution of that expression. * @xsl.usage advanced - * @LastModified: Jan 2022 + * @LastModified: May 2022 */ public class XPath implements Serializable, ExpressionOwner { @@ -208,22 +208,16 @@ public class XPath implements Serializable, ExpressionOwner else if (MATCH == type) parser.initMatchPattern(compiler, exprString, prefixResolver); else - throw new RuntimeException(XSLMessages.createXPATHMessage( + throw new TransformerException(XSLMessages.createXPATHMessage( XPATHErrorResources.ER_CANNOT_DEAL_XPATH_TYPE, new Object[]{Integer.toString(type)})); - //"Can not deal with XPath type: " + type); - // System.out.println("----------------"); - Expression expr = compiler.compileExpression(0); - - // System.out.println("expr: "+expr); - this.setExpression(expr); + m_mainExp = compiler.compileExpression(0); if((null != locator) && locator instanceof ExpressionNode) { - expr.exprSetParent((ExpressionNode)locator); + m_mainExp.exprSetParent((ExpressionNode)locator); } - } /** @@ -274,7 +268,7 @@ public class XPath implements Serializable, ExpressionOwner */ public XPath(Expression expr) { - this.setExpression(expr); + m_mainExp = expr; initFunctionTable(); } diff --git a/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/axes/PredicatedNodeTest.java b/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/axes/PredicatedNodeTest.java index 6e52dc80393..b4ecbee15a0 100644 --- a/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/axes/PredicatedNodeTest.java +++ b/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/axes/PredicatedNodeTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved. */ /* * Licensed to the Apache Software Foundation (ASF) under one or more @@ -24,6 +24,7 @@ import com.sun.org.apache.xml.internal.dtm.DTM; import com.sun.org.apache.xml.internal.dtm.DTMIterator; import com.sun.org.apache.xml.internal.utils.PrefixResolver; import com.sun.org.apache.xml.internal.utils.QName; +import com.sun.org.apache.xml.internal.utils.WrappedRuntimeException; import com.sun.org.apache.xpath.internal.Expression; import com.sun.org.apache.xpath.internal.ExpressionOwner; import com.sun.org.apache.xpath.internal.XPathContext; @@ -34,7 +35,7 @@ import com.sun.org.apache.xpath.internal.patterns.NodeTest; import java.util.List; /** - * @LastModified: May 2020 + * @LastModified: May 2022 */ public abstract class PredicatedNodeTest extends NodeTest implements SubContextList { @@ -491,9 +492,8 @@ public abstract class PredicatedNodeTest extends NodeTest implements SubContextL } catch (javax.xml.transform.TransformerException se) { - - // TODO: Fix this. - throw new RuntimeException(se.getMessage()); + // the Xalan/XPath impl use WrappedRuntimeException to carry errors over + throw new WrappedRuntimeException(se); } finally { diff --git a/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/compiler/XPathParser.java b/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/compiler/XPathParser.java index 1efc5a32c37..e7f2f567c84 100644 --- a/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/compiler/XPathParser.java +++ b/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/compiler/XPathParser.java @@ -35,7 +35,7 @@ import jdk.xml.internal.XMLSecurityManager; * Tokenizes and parses XPath expressions. This should really be named * XPathParserImpl, and may be renamed in the future. * @xsl.usage general - * @LastModified: Apr 2022 + * @LastModified: May 2022 */ public class XPathParser { @@ -79,6 +79,16 @@ public class XPathParser // XML security manager XMLSecurityManager m_xmlSecMgr; + // Union operands must be node-sets, e.g. //a | //b + // A flag indicating whether the operand is a location path + boolean isLocationPath = false; + + // A flag indicating whether the next operand is required to be a location path + boolean lPathRequired = false; + + // Keep track of the status of reading the next token after lPathRequired is flagged + boolean nextTokenRead = false; + /** * The parser constructor. */ @@ -434,9 +444,19 @@ public class XPathParser * Retrieve the next token from the command and * store it in m_token string. */ - private final void nextToken() + private final void nextToken() throws TransformerException { - + // before reading another token, check the last one + if (lPathRequired) { + if (nextTokenRead) { + // check whether the operand behind the union was a Location path + checkNodeSet(); + lPathRequired = false; + nextTokenRead = false; + } else { + nextTokenRead = true; + } + } if (m_queueMark < m_ops.getTokenQueueSize()) { m_token = (String) m_ops.m_tokenQueue.elementAt(m_queueMark++); @@ -497,31 +517,28 @@ public class XPathParser } /** - * Consume an expected token, throwing an exception if it - * isn't there. - * - * @param expected The string to be expected. - * - * @throws TransformerException + * Checks whether the function token represents a function that returns a + * nodeset. + * @param funcTok the function token + * @return true if the function token represents a function that returns a + * nodeset, false otherwise */ - private final void consumeExpected(String expected) - throws TransformerException - { + private boolean isNodesetFunction(int funcTok) { + return (funcTok == FunctionTable.FUNC_CURRENT || funcTok == FunctionTable.FUNC_HERE + || funcTok == FunctionTable.FUNC_ID); + } - if (tokenIs(expected)) + /** + * Checks whether the operand is a location path, reports error if not. + * + * @throws TransformerException if an error is found + */ + private void checkNodeSet() throws TransformerException { + if (!isLocationPath) { - nextToken(); + error(XPATHErrorResources.ER_UNION_MUST_BE_NODESET, + new Object[]{}); } - else - { - error(XPATHErrorResources.ER_EXPECTED_BUT_FOUND, new Object[]{ expected, - m_token }); //"Expected "+expected+", but found: "+m_token); - - // Patch for Christina's gripe. She wants her errorHandler to return from - // this error and continue trying to parse, rather than throwing an exception. - // Without the patch, that put us into an endless loop. - throw new XPathProcessorException(CONTINUE_AFTER_FATAL_ERROR); - } } /** @@ -1209,13 +1226,17 @@ public class XPathParser if (tokenIs(Token.VBAR)) { + // check whether the operand before the union is a location path + checkNodeSet(); + if (false == foundUnion) { foundUnion = true; - insertOp(opPos, 2, OpCodes.OP_UNION); } + isLocationPath = false; + lPathRequired = true; nextToken(); } else @@ -1464,7 +1485,7 @@ public class XPathParser m_ops.setOp(opPos + OpMap.MAPINDEX_LENGTH + 1, m_queueMark - 1); nextToken(); - consumeExpected(':'); + consumeExpected(Token.COLON); m_ops.setOp(opPos + OpMap.MAPINDEX_LENGTH + 2, m_queueMark - 1); @@ -1494,6 +1515,10 @@ public class XPathParser m_ops.setOp(opPos + OpMap.MAPINDEX_LENGTH + 1, funcTok); } + // XML Signature here() function returns a node-set + if (isNodesetFunction(funcTok)) { + isLocationPath = true; + } nextToken(); } @@ -1544,7 +1569,7 @@ public class XPathParser */ protected void LocationPath() throws TransformerException { - + isLocationPath = true; int opPos = m_ops.getOp(OpMap.MAPINDEX_LENGTH); // int locationPathOpPos = opPos; @@ -1861,7 +1886,7 @@ public class XPathParser } nextToken(); - consumeExpected(':'); + consumeExpected(Token.COLON); } else { @@ -1908,7 +1933,7 @@ public class XPathParser nextToken(); PredicateExpr(); countPredicate--; - consumeExpected(']'); + consumeExpected(Token.RBRACK); } } @@ -1950,7 +1975,7 @@ public class XPathParser m_ops.setOp(OpMap.MAPINDEX_LENGTH, m_ops.getOp(OpMap.MAPINDEX_LENGTH) + 1); nextToken(); - consumeExpected(':'); + consumeExpected(Token.COLON); } else { @@ -1969,7 +1994,7 @@ public class XPathParser * NCName ::= (Letter | '_') (NCNameChar) * NCNameChar ::= Letter | Digit | '.' | '-' | '_' | CombiningChar | Extender */ - protected void NCName() + protected void NCName() throws TransformerException { m_ops.setOp(m_ops.getOp(OpMap.MAPINDEX_LENGTH), m_queueMark - 1); diff --git a/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/functions/FuncExtFunction.java b/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/functions/FuncExtFunction.java index ac1fd12745f..6fe57d28d2f 100644 --- a/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/functions/FuncExtFunction.java +++ b/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/functions/FuncExtFunction.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved. */ /* * Licensed to the Apache Software Foundation (ASF) under one or more @@ -34,13 +34,14 @@ import com.sun.org.apache.xpath.internal.res.XPATHErrorResources; import com.sun.org.apache.xpath.internal.res.XPATHMessages; import java.util.ArrayList; import java.util.List; +import javax.xml.transform.TransformerException; /** * An object of this class represents an extension call expression. When * the expression executes, it calls ExtensionsTable#extFunction, and then * converts the result to the appropriate XObject. * @xsl.usage advanced - * @LastModified: Oct 2017 + * @LastModified: May 2022 */ public class FuncExtFunction extends Function { @@ -183,8 +184,7 @@ public class FuncExtFunction extends Function * * @throws javax.xml.transform.TransformerException */ - public XObject execute(XPathContext xctxt) - throws javax.xml.transform.TransformerException + public XObject execute(XPathContext xctxt) throws TransformerException { if (xctxt.isSecureProcessing()) throw new javax.xml.transform.TransformerException( @@ -209,6 +209,12 @@ public class FuncExtFunction extends Function } //dml ExtensionsProvider extProvider = (ExtensionsProvider)xctxt.getOwnerObject(); + if (extProvider == null) { + String fmsg = XSLMessages.createXPATHMessage( + XPATHErrorResources.ER_NO_XPATH_FUNCTION_PROVIDER, + new Object[] {argVec} ); + throw new TransformerException ( fmsg ); + } Object val = extProvider.extFunction(this, argVec); if (null != val) diff --git a/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/jaxp/JAXPVariableStack.java b/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/jaxp/JAXPVariableStack.java index 46e7996fd9a..4a75ae4f1ff 100644 --- a/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/jaxp/JAXPVariableStack.java +++ b/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/jaxp/JAXPVariableStack.java @@ -1,6 +1,5 @@ /* - * reserved comment block - * DO NOT REMOVE OR ALTER! + * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. */ /* * Licensed to the Apache Software Foundation (ASF) under one or more @@ -38,6 +37,7 @@ import com.sun.org.apache.xalan.internal.res.XSLMessages; * {@link javax.xml.xpath.XPathVariableResolver}. * * @author Ramesh Mandava + * @LastModified: May 2022 */ public class JAXPVariableStack extends VariableStack { @@ -61,6 +61,13 @@ public class JAXPVariableStack extends VariableStack { new javax.xml.namespace.QName( qname.getNamespace(), qname.getLocalPart()); + + if (resolver == null) { + String fmsg = XSLMessages.createXPATHMessage( + XPATHErrorResources.ER_NO_XPATH_VARIABLE_RESOLVER, + new Object[] { name.toString()} ); + throw new TransformerException( fmsg ); + } Object varValue = resolver.resolveVariable( name ); if ( varValue == null ) { String fmsg = XSLMessages.createXPATHMessage( diff --git a/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/jaxp/XPathExpressionImpl.java b/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/jaxp/XPathExpressionImpl.java index 79f6dc382bb..5ded2b99eaa 100644 --- a/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/jaxp/XPathExpressionImpl.java +++ b/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/jaxp/XPathExpressionImpl.java @@ -20,6 +20,7 @@ package com.sun.org.apache.xpath.internal.jaxp; +import com.sun.org.apache.xml.internal.utils.WrappedRuntimeException; import com.sun.org.apache.xpath.internal.objects.XObject; import javax.xml.namespace.QName; import javax.xml.transform.TransformerException; @@ -27,6 +28,7 @@ import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathEvaluationResult; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFunctionException; import javax.xml.xpath.XPathFunctionResolver; import javax.xml.xpath.XPathVariableResolver; import jdk.xml.internal.JdkXmlFeatures; @@ -37,7 +39,7 @@ import org.xml.sax.InputSource; * The XPathExpression interface encapsulates a (compiled) XPath expression. * * @author Ramesh Mandava - * @LastModified: Apr 2022 + * @LastModified: May 2022 */ public class XPathExpressionImpl extends XPathImplUtil implements XPathExpression { @@ -77,7 +79,7 @@ public class XPathExpressionImpl extends XPathImplUtil implements XPathExpressio } public Object eval(Object item, QName returnType) - throws javax.xml.transform.TransformerException { + throws TransformerException { XObject resultObject = eval(item, xpath); return getResultAsType(resultObject, returnType); } @@ -88,20 +90,20 @@ public class XPathExpressionImpl extends XPathImplUtil implements XPathExpressio isSupported(returnType); try { return eval(item, returnType); - } catch (java.lang.NullPointerException npe) { - // If VariableResolver returns null Or if we get - // NullPointerException at this stage for some other reason - // then we have to reurn XPathException - throw new XPathExpressionException (npe); - } catch (javax.xml.transform.TransformerException te) { + } catch (TransformerException te) { Throwable nestedException = te.getException(); - if (nestedException instanceof javax.xml.xpath.XPathFunctionException) { - throw (javax.xml.xpath.XPathFunctionException)nestedException; + if (nestedException instanceof XPathFunctionException) { + throw (XPathFunctionException)nestedException; } else { // For any other exceptions we need to throw // XPathExpressionException (as per spec) throw new XPathExpressionException(te); } + } catch (RuntimeException re) { + if (re instanceof WrappedRuntimeException) { + throw new XPathExpressionException(((WrappedRuntimeException)re).getException()); + } + throw new XPathExpressionException(re); } } @@ -115,12 +117,18 @@ public class XPathExpressionImpl extends XPathImplUtil implements XPathExpressio @Override public Object evaluate(InputSource source, QName returnType) throws XPathExpressionException { + requireNonNull(source, "Source"); isSupported (returnType); try { Document document = getDocument(source); return eval(document, returnType); } catch (TransformerException e) { throw new XPathExpressionException(e); + } catch (RuntimeException re) { + if (re instanceof WrappedRuntimeException) { + throw new XPathExpressionException(((WrappedRuntimeException)re).getException()); + } + throw new XPathExpressionException(re); } } @@ -143,8 +151,13 @@ public class XPathExpressionImpl extends XPathImplUtil implements XPathExpressio return XPathResultImpl.getValue(resultObject, type); } - } catch (javax.xml.transform.TransformerException te) { + } catch (TransformerException te) { throw new XPathExpressionException(te); + } catch (RuntimeException re) { + if (re instanceof WrappedRuntimeException) { + throw new XPathExpressionException(((WrappedRuntimeException)re).getException()); + } + throw new XPathExpressionException(re); } } diff --git a/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/jaxp/XPathImpl.java b/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/jaxp/XPathImpl.java index c070b2806f2..79a06eec785 100644 --- a/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/jaxp/XPathImpl.java +++ b/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/jaxp/XPathImpl.java @@ -20,6 +20,7 @@ package com.sun.org.apache.xpath.internal.jaxp; +import com.sun.org.apache.xml.internal.utils.WrappedRuntimeException; import com.sun.org.apache.xpath.internal.*; import com.sun.org.apache.xpath.internal.objects.XObject; import javax.xml.namespace.NamespaceContext; @@ -47,7 +48,7 @@ import org.xml.sax.InputSource; * New methods: evaluateExpression * Refactored to share code with XPathExpressionImpl. * - * @LastModified: Jan 2022 + * @LastModified: May 2022 */ public class XPathImpl extends XPathImplUtil implements javax.xml.xpath.XPath { @@ -134,11 +135,6 @@ public class XPathImpl extends XPathImplUtil implements javax.xml.xpath.XPath { XObject resultObject = eval(expression, item); return getResultAsType(resultObject, returnType); - } catch (java.lang.NullPointerException npe) { - // If VariableResolver returns null Or if we get - // NullPointerException at this stage for some other reason - // then we have to reurn XPathException - throw new XPathExpressionException (npe); } catch (TransformerException te) { Throwable nestedException = te.getException(); if (nestedException instanceof javax.xml.xpath.XPathFunctionException) { @@ -146,10 +142,14 @@ public class XPathImpl extends XPathImplUtil implements javax.xml.xpath.XPath { } else { // For any other exceptions we need to throw // XPathExpressionException (as per spec) - throw new XPathExpressionException (te); + throw new XPathExpressionException(te); } + } catch (RuntimeException re) { + if (re instanceof WrappedRuntimeException) { + throw new XPathExpressionException(((WrappedRuntimeException)re).getException()); + } + throw new XPathExpressionException(re); } - } //-Override- @@ -172,26 +172,18 @@ public class XPathImpl extends XPathImplUtil implements javax.xml.xpath.XPath { return ximpl; } catch (TransformerException te) { throw new XPathExpressionException (te) ; + } catch (RuntimeException re) { + if (re instanceof WrappedRuntimeException) { + throw new XPathExpressionException(((WrappedRuntimeException)re).getException()); + } + throw new XPathExpressionException(re); } } //-Override- public Object evaluate(String expression, InputSource source, QName returnType) throws XPathExpressionException { - isSupported(returnType); - - try { - Document document = getDocument(source); - XObject resultObject = eval(expression, document); - return getResultAsType(resultObject, returnType); - } catch (TransformerException te) { - Throwable nestedException = te.getException(); - if (nestedException instanceof javax.xml.xpath.XPathFunctionException) { - throw (javax.xml.xpath.XPathFunctionException)nestedException; - } else { - throw new XPathExpressionException (te); - } - } + return evaluate(expression, getDocument(source), returnType); } //-Override- @@ -210,7 +202,8 @@ public class XPathImpl extends XPathImplUtil implements javax.xml.xpath.XPath { //-Override- public