forked from xmlcalabash/xmlcalabash3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support XSLT 1.0 by way of Saxon 6.5.5 and reflection
- Loading branch information
Showing
8 changed files
with
211 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.xmlcalabash.steps | ||
|
||
import com.xmlcalabash.exceptions.XProcException | ||
import com.xmlcalabash.util.Xslt10Source | ||
|
||
import java.io.OutputStream | ||
|
||
class Xslt10(loader: ClassLoader) { | ||
def transform(document: Xslt10Source, stylesheet: Xslt10Source, stream: OutputStream, params: Map[String, String]): Unit = { | ||
val factoryClass = loader.loadClass("com.icl.saxon.TransformerFactoryImpl") | ||
val streamResultClass = loader.loadClass("javax.xml.transform.stream.StreamResult") | ||
val sourceClass = loader.loadClass("javax.xml.transform.Source") | ||
val resultClass = loader.loadClass("javax.xml.transform.Result") | ||
|
||
val factory = factoryClass.getDeclaredConstructor().newInstance() | ||
|
||
try { | ||
val newTransformerMethod = factory.getClass.getMethod("newTransformer", sourceClass) | ||
val transformer = newTransformerMethod.invoke(factory, stylesheet.source) | ||
|
||
val setParameterMethod = transformer.getClass.getMethod("setParameter", classOf[String], classOf[Any]) | ||
for ((key, value) <- params) { | ||
setParameterMethod.invoke(transformer, key, value) | ||
} | ||
|
||
val streamResult = streamResultClass.getDeclaredConstructor(classOf[OutputStream]).newInstance(stream) | ||
|
||
val transformMethod = transformer.getClass.getMethod("transform", sourceClass, resultClass) | ||
|
||
transformMethod.invoke(transformer, document.source, streamResult) | ||
} catch { | ||
case ex: Exception => | ||
val cause = ex.getCause | ||
if (Option(cause).isDefined) { | ||
val message = Option(cause.getMessage).getOrElse("") | ||
if (message.contains("Processing terminated by xsl:message")) { | ||
throw XProcException.xcXsltUserTermination(cause.getMessage, None) | ||
} | ||
if (message.contains("Failed to compile")) { | ||
throw XProcException.xcXsltCompileError(message, cause.asInstanceOf[Exception], None) | ||
} | ||
} | ||
|
||
throw ex | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/scala/com/xmlcalabash/util/Xslt10ClassLoader.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.xmlcalabash.util | ||
|
||
import java.net.{URL, URLClassLoader} | ||
|
||
// Special class loader to load Saxon 6 | ||
// | ||
// Adapted from https://dzone.com/articles/java-classloader-handling | ||
// | ||
class Xslt10ClassLoader(classpath: Array[URL]) extends ClassLoader(Thread.currentThread().getContextClassLoader) { | ||
private val childClassLoader = new ChildClassLoader(classpath, new DetectClass(this.getParent)) | ||
|
||
override protected def loadClass(name: String, resolve: Boolean): Class[_] = { | ||
try { | ||
childClassLoader.findClass(name) | ||
} catch { | ||
case _: ClassNotFoundException => | ||
super.loadClass(name, resolve) | ||
} | ||
} | ||
|
||
private class ChildClassLoader(urls: Array[URL], parent: DetectClass) extends URLClassLoader(urls, null) { | ||
private val realParent = parent | ||
|
||
override def findClass(name: String): Class[_] = { | ||
try { | ||
val loaded = super.findLoadedClass(name) | ||
if (loaded == null) { | ||
super.findClass(name) | ||
} else { | ||
loaded | ||
} | ||
} catch { | ||
case _: ClassNotFoundException => | ||
realParent.loadClass(name) | ||
} | ||
} | ||
} | ||
|
||
private class DetectClass(parent: ClassLoader) extends ClassLoader(parent) { | ||
override protected def findClass(name: String): Class[_] = { | ||
super.findClass(name) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.xmlcalabash.util | ||
|
||
import java.io.StringReader | ||
import java.net.URI | ||
import javax.xml.transform.stream.StreamSource | ||
|
||
class Xslt10Source() { | ||
private var _source: StreamSource = _ | ||
|
||
def source: StreamSource = _source | ||
|
||
def this(document: String, baseURI: URI) = { | ||
this() | ||
_source = new StreamSource(new StringReader(document), baseURI.toString) | ||
} | ||
|
||
def this(document: URI) = { | ||
this() | ||
_source = new StreamSource(document.toURL.openStream(), document.toString) | ||
} | ||
} |
Binary file not shown.