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 2025/02/26 02:01:07 UTC
(xalan-java) branch xalan-j_xslt3.0_mvn updated: committing improvements to xpath 3.1 function implementation for fn:unparsed-text, along with few new related working test cases
This is an automated email from the ASF dual-hosted git repository.
mukulg pushed a commit to branch xalan-j_xslt3.0_mvn
in repository https://gitbox.apache.org/repos/asf/xalan-java.git
The following commit(s) were added to refs/heads/xalan-j_xslt3.0_mvn by this push:
new 46df7d2a1 committing improvements to xpath 3.1 function implementation for fn:unparsed-text, along with few new related working test cases
46df7d2a1 is described below
commit 46df7d2a18e05cfe42c6c5583be11d674ce33ad3
Author: Mukul Gandhi <ga...@gmail.com>
AuthorDate: Wed Feb 26 07:30:53 2025 +0530
committing improvements to xpath 3.1 function implementation for fn:unparsed-text, along with few new related working test cases
---
README | 2 +-
.../java/org/apache/xpath/functions/FuncDoc.java | 2 +-
.../apache/xpath/functions/FuncUnparsedText.java | 41 +++++++++++-----
.../org/apache/xalan/tests/util/XSLConstants.java | 18 +++++--
.../xalan/tests/util/XSLTransformTestsUtil.java | 55 ++++++++++++++++++++++
.../xalan/tests/xpath3/FnUnparsedTextTests.java | 34 +++++++++++++
src/test/resources/fn_unparsed_text/gold/test6.out | 19 ++++++++
src/test/resources/fn_unparsed_text/gold/test7.out | 19 ++++++++
src/test/resources/fn_unparsed_text/person.json | 19 ++++++++
src/test/resources/fn_unparsed_text/test6.xsl | 38 +++++++++++++++
src/test/resources/fn_unparsed_text/test7.xsl | 40 ++++++++++++++++
11 files changed, 269 insertions(+), 18 deletions(-)
diff --git a/README b/README
index dcdbf15dd..3eb7cca2b 100644
--- a/README
+++ b/README
@@ -56,7 +56,7 @@ This produces Maven build output with trailing information like following:
[INFO]
[INFO] Results:
[INFO]
-[INFO] Tests run: 900, Failures: 0, Errors: 0, Skipped: 0
+[INFO] Tests run: 903, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
diff --git a/src/main/java/org/apache/xpath/functions/FuncDoc.java b/src/main/java/org/apache/xpath/functions/FuncDoc.java
index ca3a252b7..db30c3a8d 100644
--- a/src/main/java/org/apache/xpath/functions/FuncDoc.java
+++ b/src/main/java/org/apache/xpath/functions/FuncDoc.java
@@ -76,7 +76,7 @@ public class FuncDoc extends FunctionOneArg {
String hrefStrVal = XslTransformEvaluationHelper.getStrVal(xObject0);
// If the first argument is a relative uri reference, then
- // resolve that relative uri with base uri of the stylesheet
+ // resolve that relative uri with base uri of the stylesheet.
URL resolvedArg0Url = null;
diff --git a/src/main/java/org/apache/xpath/functions/FuncUnparsedText.java b/src/main/java/org/apache/xpath/functions/FuncUnparsedText.java
index b4c39c19d..f5212ccc8 100644
--- a/src/main/java/org/apache/xpath/functions/FuncUnparsedText.java
+++ b/src/main/java/org/apache/xpath/functions/FuncUnparsedText.java
@@ -30,6 +30,7 @@ import javax.xml.transform.SourceLocator;
import org.apache.xalan.res.XSLMessages;
import org.apache.xalan.xslt.util.XslTransformEvaluationHelper;
+import org.apache.xpath.ExpressionNode;
import org.apache.xpath.XPathContext;
import org.apache.xpath.objects.XObject;
import org.apache.xpath.objects.XString;
@@ -77,32 +78,46 @@ public class FuncUnparsedText extends Function2Args {
}
// If the first argument is a relative uri reference, then
- // resolve that relative uri with base uri of the stylesheet
+ // resolve that relative uri with base uri of the stylesheet.
+
URL resolvedArg0Url = null;
try {
URI arg0Uri = new URI(hrefStrVal);
- String stylesheetSystemId = srcLocator.getSystemId(); // base uri of stylesheet, if available
-
- if (!arg0Uri.isAbsolute() && (stylesheetSystemId != null)) {
- URI resolvedUriArg = (new URI(stylesheetSystemId)).resolve(hrefStrVal);
- resolvedArg0Url = resolvedUriArg.toURL();
+
+ if (arg0Uri.isAbsolute()) {
+ resolvedArg0Url = new URL(hrefStrVal);
}
-
- if (resolvedArg0Url == null) {
- resolvedArg0Url = new URL(hrefStrVal);
+ else {
+ String stylesheetSystemId = null;
+
+ if (srcLocator != null) {
+ stylesheetSystemId = srcLocator.getSystemId();
+ }
+ else {
+ ExpressionNode expressionNode = getExpressionOwner();
+ stylesheetSystemId = expressionNode.getSystemId();
+ }
+
+ if (stylesheetSystemId != null) {
+ URI resolvedUriArg = (new URI(stylesheetSystemId)).resolve(hrefStrVal);
+ resolvedArg0Url = resolvedUriArg.toURL();
+ }
+ else {
+ resolvedArg0Url = new URL(hrefStrVal);
+ }
}
String urlStrContents = XslTransformEvaluationHelper.getStringContentFromUrl(resolvedArg0Url);
-
+
String resultStr = null;
if (encodingStr != null) {
- resultStr = new String(urlStrContents.getBytes(), encodingStr.toUpperCase());
+ resultStr = new String(urlStrContents.getBytes(), encodingStr.toUpperCase());
}
else {
- resultStr = urlStrContents;
+ resultStr = urlStrContents;
}
-
+
result = new XString(resultStr);
}
catch (URISyntaxException ex) {
diff --git a/src/test/java/org/apache/xalan/tests/util/XSLConstants.java b/src/test/java/org/apache/xalan/tests/util/XSLConstants.java
index c0b4902fa..a2cfcf81d 100644
--- a/src/test/java/org/apache/xalan/tests/util/XSLConstants.java
+++ b/src/test/java/org/apache/xalan/tests/util/XSLConstants.java
@@ -33,9 +33,21 @@ public class XSLConstants {
public static final String XSL_TRANSFORM_GOLD_DIRPATH_PREFIX = "./src/test/resources/";
- // Currently, one test each within files FnBaseUriTests.java and FnDocumentUriTests.java, require
- // user workstation running these tests to be connected to web. Specify boolean value of following
- // class variable to false to disable running these tests.
+
+ /**
+ * Few XSL tests within this test suite, require the workstation where these tests
+ * are running to be connected to web. To enable proper working of these tests, the
+ * value of this class field needs to be set to boolean 'true'.
+ */
public static final boolean IS_TESTS_USING_ONLINE_URIS_ENABLED = true;
+
+ /**
+ * Few XSL tests within this test suite, require specifying absolute URI of local files
+ * with file: scheme. The following two class field variables are used for these XSL tests
+ * within this test suite.
+ */
+ public static final String LOCAL_BASE_URI_PREFIX_OF_TESTS1 = "file:/D:/eclipseWorkspaces/xalanj/xalan-j_xslt3.0_mvn/src/test/resources/";
+
+ public static final String LOCAL_BASE_URI_PREFIX_OF_TESTS2 = "file:/D:///eclipseWorkspaces/xalanj/xalan-j_xslt3.0_mvn/src/test/resources/";
}
diff --git a/src/test/java/org/apache/xalan/tests/util/XSLTransformTestsUtil.java b/src/test/java/org/apache/xalan/tests/util/XSLTransformTestsUtil.java
index ff4b0d62b..2e2e97b92 100644
--- a/src/test/java/org/apache/xalan/tests/util/XSLTransformTestsUtil.java
+++ b/src/test/java/org/apache/xalan/tests/util/XSLTransformTestsUtil.java
@@ -184,6 +184,61 @@ public class XSLTransformTestsUtil {
}
}
+ /**
+ * This function is similar to the function 'runXslTransformAndAssertOutput' except
+ * that, this function accepts a base uri prefix as argument for the purpose of running
+ * few XSL tests within this test suite.
+ *
+ * This function does an XSLT transformation via JAXP XSL transformation API, and
+ * compares the XSLT transformation's output with the corresponding expected output.
+ */
+ protected void runXslTransformAndAssertOutputWithXslParamBaseUri(String xmlFilePath, String xslFilePath,
+ String xslGoldFilePath, XslTestsErrorHandler
+ xslTransformErrHandler, String localBaseUriPrefix) {
+ try {
+ String xmlDocumentUriStr = ((new File(xmlFilePath)).toURI()).toString();
+ String xslDocumentUriStr = ((new File(xslFilePath)).toURI()).toString();
+
+ Node xmlDomSource = xmlDocumentBuilder.parse(new InputSource(xmlDocumentUriStr));
+
+ Transformer transformer = xslTransformerFactory.newTransformer(new StreamSource(xslDocumentUriStr));
+
+ setXslTransformProperties(transformer);
+
+ if (xslTransformErrHandler != null) {
+ transformer.setErrorListener(xslTransformErrHandler);
+ }
+
+ StringWriter resultStrWriter = new StringWriter();
+
+ DOMSource xmlDomSrc = new DOMSource(xmlDomSource, xmlDocumentUriStr);
+
+ transformer.setParameter("localBaseUriPrefix", localBaseUriPrefix);
+ transformer.transform(xmlDomSrc, new StreamResult(resultStrWriter));
+
+ if (xslTransformErrHandler != null) {
+ List<String> trfErrorList = xslTransformErrHandler.getTrfErrorList();
+ List<String> trfFatalErrorList = xslTransformErrHandler.getTrfFatalErrorList();
+ if (trfErrorList.size() > 0 || trfFatalErrorList.size() > 0) {
+ // The test has passed
+ return;
+ }
+ else {
+ // The test has failed
+ Assert.fail();
+ }
+ }
+ else {
+ byte[] goldFileBytes = Files.readAllBytes(Paths.get(xslGoldFilePath));
+
+ Assert.assertEquals(new String(goldFileBytes), resultStrWriter.toString());
+ }
+ }
+ catch (Exception ex) {
+ Assert.fail();
+ }
+ }
+
/**
* This function is similar to the function 'runXslTransformAndAssertOutput' except
* that, this function accepts a document URI representing an XML document to
diff --git a/src/test/java/org/apache/xalan/tests/xpath3/FnUnparsedTextTests.java b/src/test/java/org/apache/xalan/tests/xpath3/FnUnparsedTextTests.java
index 9d6158274..a85c2f241 100644
--- a/src/test/java/org/apache/xalan/tests/xpath3/FnUnparsedTextTests.java
+++ b/src/test/java/org/apache/xalan/tests/xpath3/FnUnparsedTextTests.java
@@ -96,5 +96,39 @@ public class FnUnparsedTextTests extends XSLTransformTestsUtil {
runXslTransformAndAssertOutput(xmlFilePath, xslFilePath, goldFilePath, null);
}
+
+ @Test
+ public void xslFnUnparsedTextTest6() {
+ String xmlFilePath = XSL_TRANSFORM_INPUT_DIRPATH + "test6.xsl";
+ String xslFilePath = XSL_TRANSFORM_INPUT_DIRPATH + "test6.xsl";
+
+ String goldFilePath = XSL_TRANSFORM_GOLD_DIRPATH + "test6.out";
+
+ runXslTransformAndAssertOutput(xmlFilePath, xslFilePath, goldFilePath, null);
+ }
+
+ @Test
+ public void xslFnUnparsedTextTest7() {
+ String xmlFilePath = XSL_TRANSFORM_INPUT_DIRPATH + "test7.xsl";
+ String xslFilePath = XSL_TRANSFORM_INPUT_DIRPATH + "test7.xsl";
+
+ String goldFilePath = XSL_TRANSFORM_GOLD_DIRPATH + "test7.out";
+
+ String localBaseUriPrefix = XSLConstants.LOCAL_BASE_URI_PREFIX_OF_TESTS1;
+
+ runXslTransformAndAssertOutputWithXslParamBaseUri(xmlFilePath, xslFilePath, goldFilePath, null, localBaseUriPrefix);
+ }
+
+ @Test
+ public void xslFnUnparsedTextTest8() {
+ String xmlFilePath = XSL_TRANSFORM_INPUT_DIRPATH + "test7.xsl";
+ String xslFilePath = XSL_TRANSFORM_INPUT_DIRPATH + "test7.xsl";
+
+ String goldFilePath = XSL_TRANSFORM_GOLD_DIRPATH + "test7.out";
+
+ String localBaseUriPrefix = XSLConstants.LOCAL_BASE_URI_PREFIX_OF_TESTS2;
+
+ runXslTransformAndAssertOutputWithXslParamBaseUri(xmlFilePath, xslFilePath, goldFilePath, null, localBaseUriPrefix);
+ }
}
diff --git a/src/test/resources/fn_unparsed_text/gold/test6.out b/src/test/resources/fn_unparsed_text/gold/test6.out
new file mode 100644
index 000000000..136f42f13
--- /dev/null
+++ b/src/test/resources/fn_unparsed_text/gold/test6.out
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?><map xmlns="http://www.w3.org/2005/xpath-functions">
+ <array key="data">
+ <map>
+ <string key="lName">Gregory</string>
+ <string key="fName">Gary</string>
+ <number key="id">1</number>
+ </map>
+ <map>
+ <string key="lName">Gandhi</string>
+ <string key="fName">Mukul</string>
+ <number key="id">2</number>
+ </map>
+ <map>
+ <string key="lName">Kesselman</string>
+ <string key="fName">Joseph</string>
+ <number key="id">3</number>
+ </map>
+ </array>
+</map>
diff --git a/src/test/resources/fn_unparsed_text/gold/test7.out b/src/test/resources/fn_unparsed_text/gold/test7.out
new file mode 100644
index 000000000..136f42f13
--- /dev/null
+++ b/src/test/resources/fn_unparsed_text/gold/test7.out
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?><map xmlns="http://www.w3.org/2005/xpath-functions">
+ <array key="data">
+ <map>
+ <string key="lName">Gregory</string>
+ <string key="fName">Gary</string>
+ <number key="id">1</number>
+ </map>
+ <map>
+ <string key="lName">Gandhi</string>
+ <string key="fName">Mukul</string>
+ <number key="id">2</number>
+ </map>
+ <map>
+ <string key="lName">Kesselman</string>
+ <string key="fName">Joseph</string>
+ <number key="id">3</number>
+ </map>
+ </array>
+</map>
diff --git a/src/test/resources/fn_unparsed_text/person.json b/src/test/resources/fn_unparsed_text/person.json
new file mode 100644
index 000000000..5347e85c5
--- /dev/null
+++ b/src/test/resources/fn_unparsed_text/person.json
@@ -0,0 +1,19 @@
+{
+ "data": [
+ {
+ "id" : 1,
+ "fName" : "Gary",
+ "lName" : "Gregory"
+ },
+ {
+ "id" : 2,
+ "fName" : "Mukul",
+ "lName" : "Gandhi"
+ },
+ {
+ "id" : 3,
+ "fName" : "Joseph",
+ "lName" : "Kesselman"
+ }
+ ]
+}
diff --git a/src/test/resources/fn_unparsed_text/test6.xsl b/src/test/resources/fn_unparsed_text/test6.xsl
new file mode 100644
index 000000000..455c94fcf
--- /dev/null
+++ b/src/test/resources/fn_unparsed_text/test6.xsl
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+ xmlns:xs="http://www.w3.org/2001/XMLSchema"
+ exclude-result-prefixes="xs"
+ version="3.0">
+
+ <!-- Author: mukulg@apache.org -->
+
+ <!-- An XSL stylesheet test case to test, XPath 3.1 function
+ 'unparsed-text'. -->
+
+ <xsl:output method="xml" indent="yes"/>
+
+ <xsl:variable name="fileStrContents" select="unparsed-text('person.json', 'UTF-8')" as="xs:string"/>
+
+ <xsl:template match="/">
+ <xsl:copy-of select="json-to-xml($fileStrContents)"/>
+ </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/src/test/resources/fn_unparsed_text/test7.xsl b/src/test/resources/fn_unparsed_text/test7.xsl
new file mode 100644
index 000000000..760ac46b1
--- /dev/null
+++ b/src/test/resources/fn_unparsed_text/test7.xsl
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+ xmlns:xs="http://www.w3.org/2001/XMLSchema"
+ exclude-result-prefixes="xs"
+ version="3.0">
+
+ <!-- Author: mukulg@apache.org -->
+
+ <!-- An XSL stylesheet test case to test, XPath 3.1 function
+ 'unparsed-text'. -->
+
+ <xsl:output method="xml" indent="yes"/>
+
+ <xsl:param name="localBaseUriPrefix" as="xs:string"/>
+
+ <xsl:variable name="fileStrContents" select="unparsed-text($localBaseUriPrefix || 'fn_unparsed_text/person.json', 'UTF-8')" as="xs:string"/>
+
+ <xsl:template match="/">
+ <xsl:copy-of select="json-to-xml($fileStrContents)"/>
+ </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