You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xalan.apache.org by mu...@apache.org on 2023/08/26 10:31:15 UTC

[xalan-java] branch xalan-j_xslt3.0 updated: solving an issue related to evaluation of xpath 3.1 expressions, having the string '.'. committing a new related working test case as well for this fix.

This is an automated email from the ASF dual-hosted git repository.

mukulg pushed a commit to branch xalan-j_xslt3.0
in repository https://gitbox.apache.org/repos/asf/xalan-java.git


The following commit(s) were added to refs/heads/xalan-j_xslt3.0 by this push:
     new de7ae655 solving an issue related to evaluation of xpath 3.1 expressions, having the string '.'. committing a new related working test case as well for this fix.
     new 3643c663 Merge pull request #63 from mukulga/xalan-j_xslt3.0_mukul
de7ae655 is described below

commit de7ae65567987e99b86a8b3e1c67aa0e808fb844
Author: Mukul Gandhi <ga...@gmail.com>
AuthorDate: Sat Aug 26 15:57:49 2023 +0530

    solving an issue related to evaluation of xpath 3.1 expressions, having the string '.'. committing a new related working test case as well for this fix.
---
 src/org/apache/xpath/XPath.java                    | 269 ++++++++++-----------
 .../xpath/functions/DynamicFunctionCall.java       |   4 +-
 .../apache/xpath/functions/FunctionDef1Arg.java    |  36 ++-
 tests/if_expr/test12.xsl                           |   8 +-
 tests/if_expr/{test12.xsl => test13.xsl}           |   5 +-
 tests/if_expr/test6.xsl                            |   2 +-
 tests/org/apache/xalan/xpath3/IfExprTests.java     |  10 +
 7 files changed, 170 insertions(+), 164 deletions(-)

diff --git a/src/org/apache/xpath/XPath.java b/src/org/apache/xpath/XPath.java
index 4c0eeb3c..5c121a3a 100644
--- a/src/org/apache/xpath/XPath.java
+++ b/src/org/apache/xpath/XPath.java
@@ -40,6 +40,7 @@ import org.apache.xpath.axes.LocPathIterator;
 import org.apache.xpath.compiler.Compiler;
 import org.apache.xpath.compiler.FunctionTable;
 import org.apache.xpath.compiler.XPathParser;
+import org.apache.xpath.functions.Function;
 import org.apache.xpath.objects.ResultSequence;
 import org.apache.xpath.objects.XNumber;
 import org.apache.xpath.objects.XObject;
@@ -73,8 +74,8 @@ public class XPath implements Serializable, ExpressionOwner
   /**
    * initial the function table
    */
-  private void initFunctionTable(){
-  	      m_funcTable = new FunctionTable();
+  private void initFunctionTable() {
+  	 m_funcTable = new FunctionTable();
   }
 
   /**
@@ -130,19 +131,6 @@ public class XPath implements Serializable, ExpressionOwner
     return m_mainExp;
   }
 
-//  /**
-//   * Set the SourceLocator on the expression object.
-//   *
-//   *
-//   * @param l the SourceLocator on the expression object, which may be null.
-//   */
-//  public void setLocator(SourceLocator l)
-//  {
-//    // Note potential hazards -- l may not be serializable, or may be changed
-//      // after being assigned here.
-//    m_mainExp.setSourceLocator(l);
-//  }
-
   /** The pattern string, mainly kept around for diagnostic purposes.
    *  @serial  */
   String m_patternString;
@@ -199,10 +187,8 @@ public class XPath implements Serializable, ExpressionOwner
     else
       throw new RuntimeException(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.compile(0);
 
-    // System.out.println("expr: "+expr);
     this.setExpression(expr);
     
     if((null != locator) && locator instanceof ExpressionNode)
@@ -249,12 +235,9 @@ public class XPath implements Serializable, ExpressionOwner
       throw new RuntimeException(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.compile(0);
 
-    // System.out.println("expr: "+expr);
     this.setExpression(expr);
     
     if((null != locator) && locator instanceof ExpressionNode)
@@ -366,135 +349,136 @@ public class XPath implements Serializable, ExpressionOwner
         }
     }
 
-    try
-    {
+    try {
        if (isToBeProcessedAsNodeSet) {
-          xobj = m_mainExp.execute(xctxt);
+          if (m_mainExp instanceof Function) {
+             xobj = ((Function)m_mainExp).execute(xctxt); 
+          }
+          else {
+             xobj = m_mainExp.execute(xctxt);
+          }
        }
        else {
-           String xpathPatternStr = getPatternString();
-           
-           if (xpathPatternStr.startsWith("$") && xpathPatternStr.contains("[") && 
-                                                                        xpathPatternStr.endsWith("]")) {              
-              // Within this 'if' clause, we handle the case, where the XPath expression is
-              // syntactically of type $varName[expr], for example $varName[1], $varName[$idx],
-              // $varName[funcCall(arg)] etc, and $varName resolves to a 'ResultSequence' object.
+          String xpathPatternStr = getPatternString();
                
-              String varRefXPathExprStr = "$" + xpathPatternStr.substring(1, xpathPatternStr.indexOf('['));
-              String xpathIndexExprStr = xpathPatternStr.substring(xpathPatternStr.indexOf('[') + 1, 
-                                                                                      xpathPatternStr.indexOf(']'));
-              ElemTemplateElement elemTemplateElement = (ElemTemplateElement)xctxt.getNamespaceContext();
-              List<XMLNSDecl> prefixTable = null;
-              if (elemTemplateElement != null) {
-                 prefixTable = (List<XMLNSDecl>)elemTemplateElement.getPrefixTable();
-              }
-              
-              // Evaluate the, variable reference XPath expression
-              if (prefixTable != null) {
-                 varRefXPathExprStr = XslTransformEvaluationHelper.replaceNsUrisWithPrefixesOnXPathStr(
-                                                                                                varRefXPathExprStr, 
-                                                                                                prefixTable);
-              }
-              
-              XPath xpathObj = new XPath(varRefXPathExprStr, srcLocator, 
-                                                                    xctxt.getNamespaceContext(), XPath.SELECT, null);            
-              if (fVars != null) {
-                 xpathObj.fixupVariables(fVars, fGlobalsSize);  
-              }
-              
-              XObject varEvalResult = xpathObj.execute(xctxt, xctxt.getCurrentNode(), xctxt.getNamespaceContext());
-              
-              // Evaluate the, xdm sequence index XPath expression
-              if (prefixTable != null) {
-                 xpathIndexExprStr = XslTransformEvaluationHelper.replaceNsUrisWithPrefixesOnXPathStr(
-                                                                                                 xpathIndexExprStr, 
+          if (xpathPatternStr.startsWith("$") && xpathPatternStr.contains("[") && 
+                                                                            xpathPatternStr.endsWith("]")) {              
+             // Within this 'if' clause, we handle the case, where the XPath expression is
+             // syntactically of type $varName[expr], for example $varName[1], $varName[$idx],
+             // $varName[funcCall(arg)] etc, and $varName resolves to a 'ResultSequence' object.
+                   
+             String varRefXPathExprStr = "$" + xpathPatternStr.substring(1, xpathPatternStr.indexOf('['));
+             String xpathIndexExprStr = xpathPatternStr.substring(xpathPatternStr.indexOf('[') + 1, 
+                                                                                          xpathPatternStr.indexOf(']'));
+             ElemTemplateElement elemTemplateElement = (ElemTemplateElement)xctxt.getNamespaceContext();
+             List<XMLNSDecl> prefixTable = null;
+             if (elemTemplateElement != null) {
+                prefixTable = (List<XMLNSDecl>)elemTemplateElement.getPrefixTable();
+             }
+                  
+             // Evaluate the, variable reference XPath expression
+             if (prefixTable != null) {
+                varRefXPathExprStr = XslTransformEvaluationHelper.replaceNsUrisWithPrefixesOnXPathStr(
+                                                                                                 varRefXPathExprStr, 
                                                                                                  prefixTable);
-              }
-              
-              xpathObj = new XPath(xpathIndexExprStr, srcLocator, xctxt.getNamespaceContext(), XPath.SELECT, null);
-              
-              if (fVars != null) {
-                 xpathObj.fixupVariables(fVars, fGlobalsSize);  
-              }
-              
-              XObject seqIndexEvalResult = xpathObj.execute(xctxt, xctxt.getCurrentNode(), 
-                                                                                     xctxt.getNamespaceContext());
-              if (varEvalResult instanceof ResultSequence) {
-                 ResultSequence resultSeq = (ResultSequence)varEvalResult;
-                 
-                 if (seqIndexEvalResult instanceof XNumber) {
-                    double dValIndex = ((XNumber)seqIndexEvalResult).num();
-                    if (dValIndex == (int)dValIndex) {
-                       xobj = resultSeq.item((int)dValIndex - 1);
-                    }
-                    else {
-                       throw new javax.xml.transform.TransformerException("XPTY0004 : an index value used with an xdm "
-                                                                                            + "sequence reference, is not an integer.", 
-                                                                                                 srcLocator); 
-                    }  
-                 }
-                 else if (seqIndexEvalResult instanceof XSNumericType) {
-                    String indexStrVal = ((XSNumericType)seqIndexEvalResult).stringValue();
-                    double dValIndex = (Double.valueOf(indexStrVal)).doubleValue();
-                    if (dValIndex == (int)dValIndex) {
-                       xobj = resultSeq.item((int)dValIndex - 1);                                  
-                    }
-                    else {
-                       throw new javax.xml.transform.TransformerException("XPTY0004 : an index value used with an xdm "
-                                                                                            + "sequence reference, is not an integer.", 
-                                                                                                 srcLocator); 
-                    } 
-                 }
-                 else {
-                    throw new javax.xml.transform.TransformerException("XPTY0004 : an index value used with an xdm sequence "
-                                                                                           + "reference, is not numeric.", srcLocator);  
-                 }
+             }
+                  
+             XPath xpathObj = new XPath(varRefXPathExprStr, srcLocator, 
+                                                                   xctxt.getNamespaceContext(), XPath.SELECT, null);            
+             if (fVars != null) {
+                xpathObj.fixupVariables(fVars, fGlobalsSize);  
+             }
+                  
+             XObject varEvalResult = xpathObj.execute(xctxt, xctxt.getCurrentNode(), xctxt.getNamespaceContext());
+                  
+             // Evaluate the, xdm sequence index XPath expression
+             if (prefixTable != null) {
+                xpathIndexExprStr = XslTransformEvaluationHelper.replaceNsUrisWithPrefixesOnXPathStr(
+                                                                                                xpathIndexExprStr, 
+                                                                                                prefixTable);
+             }
+                  
+             xpathObj = new XPath(xpathIndexExprStr, srcLocator, xctxt.getNamespaceContext(), XPath.SELECT, null);
+                  
+             if (fVars != null) {
+                xpathObj.fixupVariables(fVars, fGlobalsSize);  
+             }
+                  
+             XObject seqIndexEvalResult = xpathObj.execute(xctxt, xctxt.getCurrentNode(), 
+                                                                                    xctxt.getNamespaceContext());
+             if (varEvalResult instanceof ResultSequence) {
+                ResultSequence resultSeq = (ResultSequence)varEvalResult;
+                     
+                if (seqIndexEvalResult instanceof XNumber) {
+                   double dValIndex = ((XNumber)seqIndexEvalResult).num();
+                   if (dValIndex == (int)dValIndex) {
+                      xobj = resultSeq.item((int)dValIndex - 1);
+                   }
+                   else {
+                      throw new javax.xml.transform.TransformerException("XPTY0004 : an index value used with an xdm "
+                                                                                                      + "sequence reference, is not an integer.", 
+                                                                                                            srcLocator); 
+                   }  
+                }
+                else if (seqIndexEvalResult instanceof XSNumericType) {
+                        String indexStrVal = ((XSNumericType)seqIndexEvalResult).stringValue();
+                        double dValIndex = (Double.valueOf(indexStrVal)).doubleValue();
+                        if (dValIndex == (int)dValIndex) {
+                           xobj = resultSeq.item((int)dValIndex - 1);                                  
+                        }
+                        else {
+                           throw new javax.xml.transform.TransformerException("XPTY0004 : an index value used with an xdm "
+                                                                                                + "sequence reference, is not an integer.", 
+                                                                                                     srcLocator); 
+                        } 
+                }
+                else {
+                        throw new javax.xml.transform.TransformerException("XPTY0004 : an index value used with an xdm sequence "
+                                                                                               + "reference, is not numeric.", srcLocator);  
+                }
               }          
-           }  
-       }
-    }
-    catch (TransformerException te)
-    {
-      te.setLocator(this.getLocator());
-      ErrorListener el = xctxt.getErrorListener();
-      if(null != el) // defensive, should never happen.
-      {
-        el.error(te);
-      }
-      else
-        throw te;
-    }
+            }  
+          }
+        }
+        catch (TransformerException te)
+        {
+          te.setLocator(this.getLocator());
+          ErrorListener el = xctxt.getErrorListener();
+          if(null != el)
+          {
+            el.error(te);
+          }
+          else
+            throw te;
+        }
     catch (Exception e)
     {
-      while (e instanceof org.apache.xml.utils.WrappedRuntimeException)
-      {
-        e = ((org.apache.xml.utils.WrappedRuntimeException) e).getException();
-      }
-      // e.printStackTrace();
-
-      String msg = e.getMessage();
-      
-      if (msg == null || msg.length() == 0) {
-           msg = XSLMessages.createXPATHMessage(
-               XPATHErrorResources.ER_XPATH_ERROR, null);
-     
-      }  
-      TransformerException te = new TransformerException(msg,
-              getLocator(), e);
-      ErrorListener el = xctxt.getErrorListener();
-      // te.printStackTrace();
-      if(null != el) // defensive, should never happen.
-      {
-        el.fatalError(te);
-      }
-      else
-        throw te;
+       while (e instanceof org.apache.xml.utils.WrappedRuntimeException)
+       {
+          e = ((org.apache.xml.utils.WrappedRuntimeException) e).getException();
+       }
+    
+       String msg = e.getMessage();
+          
+       if (msg == null || msg.length() == 0) {
+          msg = XSLMessages.createXPATHMessage(
+                                     XPATHErrorResources.ER_XPATH_ERROR, null);
+         
+       }  
+       TransformerException te = new TransformerException(msg, getLocator(), e);
+       ErrorListener el = xctxt.getErrorListener();
+       if(null != el)
+       {
+          el.fatalError(te);
+       }
+       else
+          throw te;
     }
     finally
     {
-      xctxt.popNamespaceContext();
-
-      xctxt.popCurrentNodeAndExpression();
+       xctxt.popNamespaceContext();
+    
+       xctxt.popCurrentNodeAndExpression();
     }
 
     return xobj;
@@ -532,7 +516,7 @@ public class XPath implements Serializable, ExpressionOwner
     {
       te.setLocator(this.getLocator());
       ErrorListener el = xctxt.getErrorListener();
-      if(null != el) // defensive, should never happen.
+      if(null != el)
       {
         el.error(te);
       }
@@ -545,7 +529,6 @@ public class XPath implements Serializable, ExpressionOwner
       {
         e = ((org.apache.xml.utils.WrappedRuntimeException) e).getException();
       }
-      // e.printStackTrace();
 
       String msg = e.getMessage();
       
@@ -558,8 +541,7 @@ public class XPath implements Serializable, ExpressionOwner
       TransformerException te = new TransformerException(msg,
               getLocator(), e);
       ErrorListener el = xctxt.getErrorListener();
-      // te.printStackTrace();
-      if(null != el) // defensive, should never happen.
+      if(null != el)
       {
         el.fatalError(te);
       }
@@ -647,8 +629,6 @@ public class XPath implements Serializable, ExpressionOwner
 
     if (null != ehandler)
     {
-
-      // TO DO: Need to get stylesheet Locator from here.
       ehandler.warning(new TransformerException(fmsg, (SAXSourceLocator)xctxt.getSAXLocator()));
     }
   }
@@ -757,4 +737,5 @@ public class XPath implements Serializable, ExpressionOwner
    * @xsl.usage advanced
    */
   public static final double MATCH_SCORE_OTHER = 0.5;
+
 }
diff --git a/src/org/apache/xpath/functions/DynamicFunctionCall.java b/src/org/apache/xpath/functions/DynamicFunctionCall.java
index de869f33..3c2fbc2f 100644
--- a/src/org/apache/xpath/functions/DynamicFunctionCall.java
+++ b/src/org/apache/xpath/functions/DynamicFunctionCall.java
@@ -105,8 +105,8 @@ public class DynamicFunctionCall extends Expression {
               functionRef = exprContext.getVariableOrParam(new QName(funcRefVarName));
            }
            catch (TransformerException ex) {
-              // Try to get an XPath inline function reference, within stylesheet's 
-              // global scope. 
+              // Try to get an XPath inline function reference, from within 
+              // stylesheet's global scope. 
               ExpressionNode expressionNode = getExpressionOwner();
               ExpressionNode stylesheetRootNode = null;
               while (expressionNode != null) {
diff --git a/src/org/apache/xpath/functions/FunctionDef1Arg.java b/src/org/apache/xpath/functions/FunctionDef1Arg.java
index 848a4a80..58b721ee 100644
--- a/src/org/apache/xpath/functions/FunctionDef1Arg.java
+++ b/src/org/apache/xpath/functions/FunctionDef1Arg.java
@@ -25,6 +25,7 @@ import org.apache.xalan.xslt.util.XslTransformEvaluationHelper;
 import org.apache.xml.dtm.DTM;
 import org.apache.xml.utils.XMLString;
 import org.apache.xpath.XPathContext;
+import org.apache.xpath.axes.SelfIteratorNoPredicate;
 import org.apache.xpath.objects.XObject;
 import org.apache.xpath.objects.XString;
 import org.apache.xpath.res.XPATHErrorResources;
@@ -83,23 +84,38 @@ public class FunctionDef1Arg extends FunctionOneArg
   protected XMLString getArg0AsString(XPathContext xctxt)
           throws javax.xml.transform.TransformerException
   {
-    if(null == m_arg0)
+    XMLString resultVal = null;
+      
+    if (m_arg0 == null)
     {
       int currentNode = xctxt.getCurrentNode();
-      if(DTM.NULL == currentNode)
-        return XString.EMPTYSTRING;
+      if(DTM.NULL == currentNode) {
+          resultVal = XString.EMPTYSTRING;
+      }
       else
       {
-        DTM dtm = xctxt.getDTM(currentNode);
-        return dtm.getStringValue(currentNode);
-      }
-      
+          DTM dtm = xctxt.getDTM(currentNode);
+          resultVal = dtm.getStringValue(currentNode);
+      }      
+    }
+    else if (m_arg0 instanceof SelfIteratorNoPredicate) {
+       XObject xpath3ContextItem = xctxt.getXPath3ContextItem();
+       if (xpath3ContextItem != null) {
+          resultVal = new XString(XslTransformEvaluationHelper.getStrVal(xpath3ContextItem));
+       }
+       else {
+          XObject arg0XObject = m_arg0.execute(xctxt);
+           
+          resultVal = new XString(XslTransformEvaluationHelper.getStrVal(arg0XObject));
+       }
     }
     else {
-      XObject arg0XObject = m_arg0.execute(xctxt);
-      
-      return new XString(XslTransformEvaluationHelper.getStrVal(arg0XObject));
+       XObject arg0XObject = m_arg0.execute(xctxt);
+        
+       resultVal = new XString(XslTransformEvaluationHelper.getStrVal(arg0XObject));  
     }
+    
+    return resultVal;
   }
 
   /**
diff --git a/tests/if_expr/test12.xsl b/tests/if_expr/test12.xsl
index eb633868..1672e9fc 100644
--- a/tests/if_expr/test12.xsl
+++ b/tests/if_expr/test12.xsl
@@ -20,15 +20,15 @@
     <xsl:template match="/">
        <result>
           <xsl:for-each select="$nameValSeq">
-             <xsl:variable name="currentItem" select="."/>
+             <xsl:variable name="currentSeqItem" select="."/>
              <data>
                 <one>
                    <xsl:value-of select="if (current-date() gt xs:date('2000-12-31')) 
-                                                       then upper-case($currentItem) else lower-case($currentItem)"/>
+                                                                    then upper-case($currentSeqItem) else lower-case($currentSeqItem)"/>
                 </one>
                 <two>
-	           <xsl:value-of select="if (current-date() gt xs:date('2501-12-31')) 
-	                                               then upper-case($currentItem) else lower-case($currentItem)"/>
+	               <xsl:value-of select="if (current-date() gt xs:date('2501-12-31')) 
+	                                                                then upper-case($currentSeqItem) else lower-case($currentSeqItem)"/>
                 </two>
              </data>
           </xsl:for-each>
diff --git a/tests/if_expr/test12.xsl b/tests/if_expr/test13.xsl
similarity index 87%
copy from tests/if_expr/test12.xsl
copy to tests/if_expr/test13.xsl
index eb633868..fa801ec4 100644
--- a/tests/if_expr/test12.xsl
+++ b/tests/if_expr/test13.xsl
@@ -20,15 +20,14 @@
     <xsl:template match="/">
        <result>
           <xsl:for-each select="$nameValSeq">
-             <xsl:variable name="currentItem" select="."/>
              <data>
                 <one>
                    <xsl:value-of select="if (current-date() gt xs:date('2000-12-31')) 
-                                                       then upper-case($currentItem) else lower-case($currentItem)"/>
+                                                                    then upper-case(.) else lower-case(.)"/>
                 </one>
                 <two>
 	           <xsl:value-of select="if (current-date() gt xs:date('2501-12-31')) 
-	                                               then upper-case($currentItem) else lower-case($currentItem)"/>
+	                                                                then upper-case(.) else lower-case(.)"/>
                 </two>
              </data>
           </xsl:for-each>
diff --git a/tests/if_expr/test6.xsl b/tests/if_expr/test6.xsl
index 6d55b708..f01d4156 100644
--- a/tests/if_expr/test6.xsl
+++ b/tests/if_expr/test6.xsl
@@ -17,7 +17,7 @@
                                                                            then true() 
                                                                            else false() }"/>
 
-   <xsl:template match="/list">
+   <xsl:template match="/list">      
       <result>
          <one> 
             <xsl:copy-of select="word[$filterCheck(., 'l')]"/>
diff --git a/tests/org/apache/xalan/xpath3/IfExprTests.java b/tests/org/apache/xalan/xpath3/IfExprTests.java
index 75c527db..43c8cfe9 100644
--- a/tests/org/apache/xalan/xpath3/IfExprTests.java
+++ b/tests/org/apache/xalan/xpath3/IfExprTests.java
@@ -176,5 +176,15 @@ public class IfExprTests extends XslTransformTestsUtil {
         
         runXslTransformAndAssertOutput(xmlFilePath, xslFilePath, goldFilePath, null);
     }
+    
+    @Test
+    public void xslIfExprTest14() {
+        String xmlFilePath = XSL_TRANSFORM_INPUT_DIRPATH + "test13.xsl"; 
+        String xslFilePath = XSL_TRANSFORM_INPUT_DIRPATH + "test13.xsl";
+        
+        String goldFilePath = XSL_TRANSFORM_GOLD_DIRPATH + "test13.out";                
+        
+        runXslTransformAndAssertOutput(xmlFilePath, xslFilePath, goldFilePath, null);
+    }
 
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xalan.apache.org
For additional commands, e-mail: commits-help@xalan.apache.org