From de2fe0966bf551cd5cda1c2dcb3357a3c58ee266 Mon Sep 17 00:00:00 2001
From: srosse <stephane.rosse@frentix.com>
Date: Wed, 13 Mar 2019 18:51:41 +0100
Subject: [PATCH] OO-3967: try to use the XSLT transformer of the JDK (CXalan)

---
 .../qti/render/LocalizedXSLTransformer.java   | 23 +++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/olat/ims/qti/render/LocalizedXSLTransformer.java b/src/main/java/org/olat/ims/qti/render/LocalizedXSLTransformer.java
index 47ca6dcb0b7..fa111440240 100644
--- a/src/main/java/org/olat/ims/qti/render/LocalizedXSLTransformer.java
+++ b/src/main/java/org/olat/ims/qti/render/LocalizedXSLTransformer.java
@@ -37,6 +37,7 @@ import javax.xml.transform.Source;
 import javax.xml.transform.Templates;
 import javax.xml.transform.TransformerConfigurationException;
 import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.TransformerFactoryConfigurationError;
 import javax.xml.transform.sax.SAXSource;
 import javax.xml.transform.stream.StreamResult;
 
@@ -182,7 +183,7 @@ public class LocalizedXSLTransformer {
 			log.error("Could not convert xsl to string!", e);
 		}
 		String replacedOutput = evaluateValue(xslAsString, vcContext);
-		TransformerFactory tfactory = TransformerFactory.newInstance();
+		TransformerFactory tfactory = newTransformerFactory();
 		XMLReader reader;
 		try {
 			reader = XMLReaderFactory.createXMLReader();
@@ -195,6 +196,24 @@ public class LocalizedXSLTransformer {
 			throw new OLATRuntimeException("Could not initialize transformer (wrong config)!", e);
 		}
 	}
+	
+	/**
+	 * The method try to find the Xalan implementation of the JDK because the styelsheet
+	 * was with this one develop and not the Saxon XSLT 2.0 or 3.0 engine which lead to
+	 * some compatibility issue with special HTML entity (example: 129).
+	 * 
+	 * @return Try to get the embedded Xalan implementation which is a pure XSLT 1.0 engine.
+	 */
+	private TransformerFactory newTransformerFactory() {
+		TransformerFactory tfactory = null;
+		try {
+			tfactory = TransformerFactory.newInstance("com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl", null);
+		} catch (TransformerFactoryConfigurationError e) {
+			log.error("", e);
+			tfactory = TransformerFactory.newInstance();
+		}
+		return tfactory;
+	}
 
 	/**
 	 * Takes String with template and fills values from Translator in Context
@@ -231,7 +250,7 @@ public class LocalizedXSLTransformer {
 	 * @throws IOException
 	 */
 	private static String slurp(InputStream in) throws IOException {
-	   StringBuffer out = new StringBuffer();
+	   StringBuilder out = new StringBuilder();
 	   byte[] b = new byte[4096];
 	   for (int n; (n = in.read(b)) != -1;) {
 	       out.append(new String(b, 0, n));
-- 
GitLab