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/06/10 08:09:55 UTC

[xalan-java] branch xalan-j_xslt3.0 updated: improving implementation of the xpath 3.1 range expression. adding few new working related test cases as well.

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 9a42e9c0 improving implementation of the xpath 3.1 range expression. adding few new working related test cases as well.
9a42e9c0 is described below

commit 9a42e9c0f30f25dd63a4191832da8926df02af10
Author: Mukul Gandhi <ga...@gmail.com>
AuthorDate: Sat Jun 10 13:39:37 2023 +0530

    improving implementation of the xpath 3.1 range expression. adding few new working related test cases as well.
---
 src/org/apache/xpath/operations/Range.java        | 25 +++++++---
 tests/org/apache/xalan/xpath3/RangeExprTests.java | 53 ++++++++++++++++++++
 tests/range_expr/gold/test10.out                  | 12 +++++
 tests/range_expr/gold/test11.out                  |  0
 tests/range_expr/gold/test8.out                   | 10 ++++
 tests/range_expr/gold/test9.out                   |  1 +
 tests/range_expr/test10.xsl                       | 59 +++++++++++++++++++++++
 tests/range_expr/test11.xsl                       | 38 +++++++++++++++
 tests/range_expr/test12.xsl                       | 22 +++++++++
 tests/range_expr/test1_b.xml                      |  5 ++
 tests/range_expr/test8.xsl                        | 39 +++++++++++++++
 tests/range_expr/test9.xsl                        | 39 +++++++++++++++
 12 files changed, 297 insertions(+), 6 deletions(-)

diff --git a/src/org/apache/xpath/operations/Range.java b/src/org/apache/xpath/operations/Range.java
index c83777e7..b728f2df 100644
--- a/src/org/apache/xpath/operations/Range.java
+++ b/src/org/apache/xpath/operations/Range.java
@@ -30,7 +30,7 @@ import org.apache.xpath.xs.types.XSInteger;
 /**
  * The XPath 3.1 range "to" operation.
  * 
- * An XPath range expression can be used to construct a sequence of 
+ * An XPath range "to" expression can be used to construct a sequence of 
  * consecutive integers. Each of the operands of the XPath range "to" 
  * operator is converted as though it was an argument of a function 
  * with the expected parameter type xs:integer.
@@ -62,14 +62,27 @@ public class Range extends Operation
       
       XObject expr2 = m_right.execute(xctxt);
       
-      int fromIdx = (int)expr1.num();
-      int toIdx = (int)expr2.num();
+      double firstArg = expr1.num();
+      double secondArg = expr2.num();
       
-      for (int idx = fromIdx; idx <= toIdx; idx++) {
-         result.add(new XSInteger(BigInteger.valueOf((long)idx)));    
+      if (firstArg > (long)firstArg) {
+         throw new javax.xml.transform.TransformerException("XPTY0004 : The required item type of the first operand of "
+                                                     + "'to' is an xs:integer. The supplied value is of type xs:double.", xctxt.getSAXLocator());  
       }
       
-      return result;
+      if (secondArg > (long)secondArg) {
+         throw new javax.xml.transform.TransformerException("XPTY0004 : The required item type of the second operand of "
+                                                     + "'to' is an xs:integer. The supplied value is of type xs:double.", xctxt.getSAXLocator());  
+      }
+      
+      long fromIdx = (long)firstArg;
+      long toIdx = (long)secondArg;
+      
+      for (long idx = fromIdx; idx <= toIdx; idx++) {
+         result.add(new XSInteger(BigInteger.valueOf(idx)));    
+      }
+      
+      return result;      
     }
 
 }
diff --git a/tests/org/apache/xalan/xpath3/RangeExprTests.java b/tests/org/apache/xalan/xpath3/RangeExprTests.java
index 88435ccf..9302df3f 100644
--- a/tests/org/apache/xalan/xpath3/RangeExprTests.java
+++ b/tests/org/apache/xalan/xpath3/RangeExprTests.java
@@ -16,6 +16,7 @@
  */
 package org.apache.xalan.xpath3;
 
+import org.apache.xalan.util.XslTestsErrorHandler;
 import org.apache.xalan.util.XslTransformTestsUtil;
 import org.apache.xalan.xslt3.XSLConstants;
 import org.junit.AfterClass;
@@ -116,5 +117,57 @@ public class RangeExprTests extends XslTransformTestsUtil {
         
         runXslTransformAndAssertOutput(xmlFilePath, xslFilePath, goldFilePath, null);
     }
+    
+    @Test
+    public void xslRangeExprTest8() {
+        String xmlFilePath = XSL_TRANSFORM_INPUT_DIRPATH + "test8.xsl"; 
+        String xslFilePath = XSL_TRANSFORM_INPUT_DIRPATH + "test8.xsl";
+        
+        String goldFilePath = XSL_TRANSFORM_GOLD_DIRPATH + "test8.out";                
+        
+        runXslTransformAndAssertOutput(xmlFilePath, xslFilePath, goldFilePath, null);
+    }
+    
+    @Test
+    public void xslRangeExprTest9() {
+        String xmlFilePath = XSL_TRANSFORM_INPUT_DIRPATH + "test9.xsl"; 
+        String xslFilePath = XSL_TRANSFORM_INPUT_DIRPATH + "test9.xsl";
+        
+        String goldFilePath = XSL_TRANSFORM_GOLD_DIRPATH + "test9.out";                
+        
+        runXslTransformAndAssertOutput(xmlFilePath, xslFilePath, goldFilePath, null);
+    }
+    
+    @Test
+    public void xslNumberRangeTest10() {
+        String xmlFilePath = XSL_TRANSFORM_INPUT_DIRPATH + "test10.xsl"; 
+        String xslFilePath = XSL_TRANSFORM_INPUT_DIRPATH + "test10.xsl";
+        
+        String goldFilePath = XSL_TRANSFORM_GOLD_DIRPATH + "test10.out";                
+        
+        runXslTransformAndAssertOutput(xmlFilePath, xslFilePath, goldFilePath, null);
+    }
+    
+    @Test
+    public void xslRangeExprTest11() {
+        String xmlFilePath = XSL_TRANSFORM_INPUT_DIRPATH + "test11.xsl"; 
+        String xslFilePath = XSL_TRANSFORM_INPUT_DIRPATH + "test11.xsl";
+        
+        String goldFilePath = XSL_TRANSFORM_GOLD_DIRPATH + "test11.out";                
+        
+        runXslTransformAndAssertOutput(xmlFilePath, xslFilePath, goldFilePath, 
+                                                                       new XslTestsErrorHandler());
+    }
+    
+    @Test
+    public void xslRangeExprTest12() {
+        String xmlFilePath = XSL_TRANSFORM_INPUT_DIRPATH + "test1_b.xml"; 
+        String xslFilePath = XSL_TRANSFORM_INPUT_DIRPATH + "test12.xsl";
+        
+        String goldFilePath = XSL_TRANSFORM_GOLD_DIRPATH + "test11.out";                
+        
+        runXslTransformAndAssertOutput(xmlFilePath, xslFilePath, goldFilePath, 
+                                                                       new XslTestsErrorHandler());
+    }
 
 }
diff --git a/tests/range_expr/gold/test10.out b/tests/range_expr/gold/test10.out
new file mode 100644
index 00000000..5722d96b
--- /dev/null
+++ b/tests/range_expr/gold/test10.out
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?><result>
+  <val>1</val>
+  <val>2</val>
+  <val>3</val>
+  <val>4</val>
+  <val>5</val>
+  <val>6</val>
+  <val>7</val>
+  <val>8</val>
+  <val>9</val>
+  <val>10</val>
+</result>
diff --git a/tests/range_expr/gold/test11.out b/tests/range_expr/gold/test11.out
new file mode 100644
index 00000000..e69de29b
diff --git a/tests/range_expr/gold/test8.out b/tests/range_expr/gold/test8.out
new file mode 100644
index 00000000..6f16ee13
--- /dev/null
+++ b/tests/range_expr/gold/test8.out
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?><result>
+  <val>-2</val>
+  <val>-1</val>
+  <val>0</val>
+  <val>1</val>
+  <val>2</val>
+  <val>3</val>
+  <val>4</val>
+  <val>5</val>
+</result>
diff --git a/tests/range_expr/gold/test9.out b/tests/range_expr/gold/test9.out
new file mode 100644
index 00000000..a19077d4
--- /dev/null
+++ b/tests/range_expr/gold/test9.out
@@ -0,0 +1 @@
+<?xml version="1.0" encoding="UTF-8"?><result/>
diff --git a/tests/range_expr/test10.xsl b/tests/range_expr/test10.xsl
new file mode 100644
index 00000000..c0ec5446
--- /dev/null
+++ b/tests/range_expr/test10.xsl
@@ -0,0 +1,59 @@
+<?xml version="1.0"?>
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+                version="3.0">
+                
+   <!-- Author: mukulg@apache.org -->
+   
+   <!-- An XSLT stylesheet, demonstrating producing an integer 
+        number range, using a recursive named template. An XSLT 
+        algorithm described within this stylesheet, shall work 
+        with an XSLT 1.0 processor as well. -->                
+
+   <xsl:output method="xml" indent="yes"/>
+
+   <xsl:template match="/">
+      <result>
+         <xsl:call-template name="produceNumberRange">
+            <xsl:with-param name="from" select="1"/>
+            <xsl:with-param name="to" select="10"/>
+         </xsl:call-template>
+      </result>
+   </xsl:template>
+   
+   <xsl:template name="produceNumberRange">
+      <xsl:param name="from"/>
+      <xsl:param name="to"/>
+
+      <xsl:choose>
+         <xsl:when test="$from &lt; $to">
+            <val><xsl:value-of select="$from"/></val>
+            <xsl:call-template name="produceNumberRange">
+	           <xsl:with-param name="from" select="$from + 1"/>
+	           <xsl:with-param name="to" select="$to"/>
+            </xsl:call-template>
+         </xsl:when>
+         <xsl:otherwise>
+            <val><xsl:value-of select="$from"/></val>
+         </xsl:otherwise>
+      </xsl:choose>
+   </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>
\ No newline at end of file
diff --git a/tests/range_expr/test11.xsl b/tests/range_expr/test11.xsl
new file mode 100644
index 00000000..3f1f7bdb
--- /dev/null
+++ b/tests/range_expr/test11.xsl
@@ -0,0 +1,38 @@
+<?xml version="1.0"?>
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+                version="3.0">
+                
+   <!-- Author: mukulg@apache.org -->
+   
+   <!-- An XSLT stylesheet demonstrating that, arguments of XPath 
+        range "to" operator should have data type xs:integer. -->               
+
+   <xsl:output method="xml" indent="yes"/>
+
+   <xsl:template match="/">
+      <result>
+        <xsl:for-each select="1 to 10.7">
+           <val><xsl:value-of select="."/></val>
+        </xsl:for-each>
+      </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>
\ No newline at end of file
diff --git a/tests/range_expr/test12.xsl b/tests/range_expr/test12.xsl
new file mode 100644
index 00000000..8e015de4
--- /dev/null
+++ b/tests/range_expr/test12.xsl
@@ -0,0 +1,22 @@
+<?xml version="1.0"?>
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+                version="3.0">
+
+   <!-- Author: mukulg@apache.org -->
+   
+   <!-- use with test1_b.xml -->
+   
+   <!-- An XSLT stylesheet demonstrating that, arguments of XPath 
+        range "to" operator should have data type xs:integer. -->
+        
+   <xsl:output method="xml" indent="yes"/>
+
+   <xsl:template match="/elem">
+      <result>
+        <xsl:for-each select="x to y">
+           <val><xsl:value-of select="."/></val>
+        </xsl:for-each>
+      </result>
+   </xsl:template>
+
+</xsl:stylesheet>
\ No newline at end of file
diff --git a/tests/range_expr/test1_b.xml b/tests/range_expr/test1_b.xml
new file mode 100644
index 00000000..51bbd678
--- /dev/null
+++ b/tests/range_expr/test1_b.xml
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<elem>
+  <x>1</x>
+  <y>10.7</y>
+</elem>
\ No newline at end of file
diff --git a/tests/range_expr/test8.xsl b/tests/range_expr/test8.xsl
new file mode 100644
index 00000000..846db360
--- /dev/null
+++ b/tests/range_expr/test8.xsl
@@ -0,0 +1,39 @@
+<?xml version="1.0"?>
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+                version="3.0">
+                
+   <!-- Author: mukulg@apache.org -->
+   
+   <!-- An XSLT stylesheet using xsl:for-each loop, to iterate through a 
+        sequence of numbers produced by XPath range "to" operator, from 
+        a -ve minimum to a +ve maximum. -->                
+
+   <xsl:output method="xml" indent="yes"/>
+
+   <xsl:template match="/">
+      <result>
+         <xsl:for-each select="-2 to 5">
+           <val><xsl:value-of select="."/></val>
+         </xsl:for-each>
+      </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>
\ No newline at end of file
diff --git a/tests/range_expr/test9.xsl b/tests/range_expr/test9.xsl
new file mode 100644
index 00000000..bf701272
--- /dev/null
+++ b/tests/range_expr/test9.xsl
@@ -0,0 +1,39 @@
+<?xml version="1.0"?>
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+                version="3.0">
+                
+   <!-- Author: mukulg@apache.org -->
+   
+   <!-- An XSLT stylesheet, demonstrating that an integer number 
+        range produced by XPath range "to" operator by a larger 
+        minimum and a lesser maximum, is an empty sequence. -->                
+
+   <xsl:output method="xml" indent="yes"/>
+
+   <xsl:template match="/">
+      <result>
+         <xsl:for-each select="5 to 2">
+           <val><xsl:value-of select="."/></val>
+         </xsl:for-each>
+      </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>
\ No newline at end of file


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