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 2024/07/07 14:01:22 UTC

(xalan-java) branch xalan-j_xslt3.0 updated: committing improvements to implementation of xpath3.1 fn:apply function

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 73d0830aa committing improvements to implementation of xpath3.1 fn:apply function
73d0830aa is described below

commit 73d0830aa77c8408b22042e3914eb94746f2f193
Author: Mukul Gandhi <ga...@gmail.com>
AuthorDate: Sun Jul 7 19:31:09 2024 +0530

    committing improvements to implementation of xpath3.1 fn:apply function
---
 src/org/apache/xpath/functions/FuncApply.java   | 96 +++++++++++++++++++++++--
 tests/fn_apply/gold/test2.out                   |  4 ++
 tests/fn_apply/test2.xsl                        | 45 ++++++++++++
 tests/org/apache/xalan/xpath3/FnApplyTests.java | 10 +++
 4 files changed, 148 insertions(+), 7 deletions(-)

diff --git a/src/org/apache/xpath/functions/FuncApply.java b/src/org/apache/xpath/functions/FuncApply.java
index 264af2d1c..b04c64a11 100644
--- a/src/org/apache/xpath/functions/FuncApply.java
+++ b/src/org/apache/xpath/functions/FuncApply.java
@@ -17,15 +17,26 @@
  */
 package org.apache.xpath.functions;
 
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+
+import javax.xml.transform.SourceLocator;
 import javax.xml.transform.TransformerException;
 
 import org.apache.xalan.res.XSLMessages;
+import org.apache.xalan.xslt.util.XslTransformEvaluationHelper;
+import org.apache.xml.utils.QName;
 import org.apache.xpath.Expression;
 import org.apache.xpath.XPathContext;
 import org.apache.xpath.compiler.FunctionTable;
 import org.apache.xpath.composite.XPathNamedFunctionReference;
+import org.apache.xpath.objects.InlineFunctionParameter;
+import org.apache.xpath.objects.XNumber;
 import org.apache.xpath.objects.XObject;
 import org.apache.xpath.objects.XPathArray;
+import org.apache.xpath.objects.XPathInlineFunction;
 import org.apache.xpath.operations.Variable;
 import org.apache.xpath.res.XPATHErrorResources;
 
@@ -56,25 +67,35 @@ public class FuncApply extends Function2Args {
         
         Expression arg0 = getArg0();
         Expression arg1 = getArg1();
+        
+        SourceLocator srcLocator = xctxt.getSAXLocator();
  
         if (arg0 instanceof XPathNamedFunctionReference) {
             XPathNamedFunctionReference namedFuncRef = (XPathNamedFunctionReference)arg0;
             result = getFnApplyResult(arg1, namedFuncRef, xctxt);
         }
+        else if (arg0 instanceof XPathInlineFunction) {
+        	XPathInlineFunction xpathInlineFunction = (XPathInlineFunction)arg0;
+        	result = evaluateInlineFunctionRef(arg1, xpathInlineFunction, xctxt);
+        }
         else if (arg0 instanceof Variable) {           
             XObject arg0VarValue = arg0.execute(xctxt);
             if (arg0VarValue instanceof XPathNamedFunctionReference) {
             	XPathNamedFunctionReference namedFuncRef = (XPathNamedFunctionReference)arg0VarValue;
             	result = getFnApplyResult(arg1, namedFuncRef, xctxt);
             }
+            else if (arg0VarValue instanceof XPathInlineFunction) {            	
+            	XPathInlineFunction xpathInlineFunction = (XPathInlineFunction)arg0VarValue;
+            	result = evaluateInlineFunctionRef(arg1, xpathInlineFunction, xctxt);
+            }
             else {
                 throw new javax.xml.transform.TransformerException("FORG0006 : The 1st argument provided to function call fn:apply, "
-                                                                                               + "is not a function reference.", xctxt.getSAXLocator());    
+                                                                                               + "is not a function reference.", srcLocator);    
             }
         }
         else {
             throw new javax.xml.transform.TransformerException("FORG0006 : The 1st argument provided to function call fn:apply, "
-                                                                                               + "is not a function reference.", xctxt.getSAXLocator());               
+                                                                                               + "is not a function reference.", srcLocator);               
         }
         
         return result;
@@ -106,16 +127,20 @@ public class FuncApply extends Function2Args {
   }
   
   /**
-   * Get the result of function call, fn:apply.
+   * Get the result of function call, fn:apply where the 1st argument to fn:apply
+   * function call is an XPath named function reference.
    */
   private XObject getFnApplyResult(Expression arg1XpathExpr, XPathNamedFunctionReference 
 		                                                namedFuncRef, XPathContext xctxt) throws TransformerException {
 	  XObject result = null;
 	  
 	  XObject arg1XObj = arg1XpathExpr.execute(xctxt);
+	  
+	  SourceLocator srcLocator = xctxt.getSAXLocator();
+	  
 	  if (!(arg1XObj instanceof XPathArray)) {
 		 throw new TransformerException("XPTY0004 : The 2nd argument provided to function call fn:apply, "
-		 		                                                                         + "is not an array reference.");   
+		 		                                                            + "is not an array reference.", srcLocator);   
 	  }
 	  
 	  String funcNamespace = namedFuncRef.getFuncNamespace();
@@ -138,7 +163,7 @@ public class FuncApply extends Function2Args {
 		  funcIdObj = funcTable.getFunctionIdForXPathBuiltinArrayFuncs(funcLocalName);
 	  }
 
-	  String expandedFuncName = "{" + funcNamespace + ":" + funcLocalName + "}#" + funcArity;
+	  String expandedFuncName = "{" + funcNamespace + ":" + funcLocalName + "}#" + funcArity;	  	  
 	  
 	  if (funcIdObj != null) {
 		  String funcIdStr = funcIdObj.toString();
@@ -153,12 +178,69 @@ public class FuncApply extends Function2Args {
 		  } 
 		  catch (WrongNumberArgsException ex) {			    
 			 throw new javax.xml.transform.TransformerException("XPTY0004 : Wrong number of arguments provided, "
-					                                                   + "during function call " + expandedFuncName + "."); 
+					                                                   + "during function call " + expandedFuncName + ".", srcLocator); 
 		  }               
 	  }
 	  else {
 		  throw new javax.xml.transform.TransformerException("XPTY0004 : There is no function definition "
-		  		                                                    + "found, for the function " + expandedFuncName);
+		  		                                                    + "found, for the function " + expandedFuncName + ".", srcLocator);
+	  }
+
+	  return result;
+  }
+  
+  /**
+   * Evaluate a function call to inline function expression, that gets its arguments
+   * from an xdm array. This method effectively produces the result of fn:apply 
+   * function call.
+   */
+  private XObject evaluateInlineFunctionRef(Expression arrXPathExpr, XPathInlineFunction 
+		                                             xpathInlineFunction, XPathContext xctxt) throws TransformerException {
+	  
+	  XObject result = null;
+	  
+	  SourceLocator srcLocator = xctxt.getSAXLocator();
+	  
+	  // We manually construct an XPath dynamic function call expression, 
+	  // to evaluate this function call.
+	  
+	  XPathDynamicFunctionCall xpathDynamicFunctionCall = new XPathDynamicFunctionCall();
+	  String funcRefVarName = "dfc_" + (UUID.randomUUID()).toString();
+	  xpathDynamicFunctionCall.setFuncRefVarName(funcRefVarName);
+
+	  Map<QName, XObject> inlineFunctionVarMap = xctxt.getXPathVarMap();
+	  inlineFunctionVarMap.put(new QName(funcRefVarName), xpathInlineFunction);
+
+	  XObject arg1XObj = arrXPathExpr.execute(xctxt);
+	  if (!(arg1XObj instanceof XPathArray)) {
+		  throw new TransformerException("XPTY0004 : The 2nd argument provided to function call fn:apply, "
+				                                                    + "is not an array reference.", srcLocator);   
+	  }
+	  else {
+		  XPathArray arg1XpathArr = (XPathArray)arg1XObj;
+		  List<InlineFunctionParameter> inlineFuncParamList = xpathInlineFunction.getFuncParamList();
+		  if (arg1XpathArr.size() != inlineFuncParamList.size()) {
+			  throw new TransformerException("XPTY0004 : The number of arguments provided with function call fn:apply() "
+					                                                        + "within its arguments array is " + arg1XpathArr.size() + ". Required " + 
+					                                                         inlineFuncParamList.size() + "."); 
+		  }
+		  else {
+			  List<String> dfcArgList = new ArrayList<String>();
+			  for (int idx = 0; idx < arg1XpathArr.size(); idx++) {
+				  XObject argObj = arg1XpathArr.get(idx);
+				  String argStr = null;
+				  if (argObj instanceof XNumber) {
+					  argStr = ((XNumber)argObj).num() + "";	 
+				  }
+				  else {
+					  argStr = XslTransformEvaluationHelper.getStrVal(argObj);  
+				  }
+				  dfcArgList.add(argStr);
+			  }
+
+			  xpathDynamicFunctionCall.setArgList(dfcArgList);
+			  result = xpathDynamicFunctionCall.execute(xctxt);
+		  }
 	  }
 
 	  return result;
diff --git a/tests/fn_apply/gold/test2.out b/tests/fn_apply/gold/test2.out
new file mode 100644
index 000000000..f6b6b5723
--- /dev/null
+++ b/tests/fn_apply/gold/test2.out
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?><result>
+  <one>7</one>
+  <two>8</two>
+</result>
diff --git a/tests/fn_apply/test2.xsl b/tests/fn_apply/test2.xsl
new file mode 100644
index 000000000..f526f3ac1
--- /dev/null
+++ b/tests/fn_apply/test2.xsl
@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"              
+                version="3.0">
+                
+   <!-- Author: mukulg@apache.org -->
+   
+   <!-- An XSLT stylesheet test case, for the XPath 3.1 
+        function fn:apply. -->                 
+
+   <xsl:output method="xml" indent="yes"/>
+
+   <xsl:template match="/">
+     <result>
+	    <xsl:variable name="func1" select="function ($a) { $a + 2 }"/>
+		<xsl:variable name="func2" select="function ($a,$b) { $a + $b }"/>				
+		<one>
+		   <xsl:value-of select="apply($func1,[2]) + apply($func2,[1,2])"/>
+		</one>
+		<two>
+		   <!-- Within the below xsl:value-of instruction, the 2nd operand of an operator 
+		        +, is a literal inline function expression. -->
+		   <xsl:value-of select="apply($func1,[2]) + apply(function ($a,$b) { $a*$b + 2 },[1,2])"/>
+		</two>
+	 </result>
+   </xsl:template>
+   
+   <!--
+      * 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.
+   -->
+
+</xsl:stylesheet>
diff --git a/tests/org/apache/xalan/xpath3/FnApplyTests.java b/tests/org/apache/xalan/xpath3/FnApplyTests.java
index 267145fb3..892ec7097 100644
--- a/tests/org/apache/xalan/xpath3/FnApplyTests.java
+++ b/tests/org/apache/xalan/xpath3/FnApplyTests.java
@@ -56,5 +56,15 @@ public class FnApplyTests extends XSLTransformTestsUtil {
         
         runXslTransformAndAssertOutput(xmlFilePath, xslFilePath, goldFilePath, null);
     }
+    
+    @Test
+    public void xslFnApplyTest2() {
+        String xmlFilePath = XSL_TRANSFORM_INPUT_DIRPATH + "test2.xsl"; 
+        String xslFilePath = XSL_TRANSFORM_INPUT_DIRPATH + "test2.xsl";
+        
+        String goldFilePath = XSL_TRANSFORM_GOLD_DIRPATH + "test2.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