eerohele / sigel

XSLT and XPath in your Clojure
Eclipse Public License 1.0
28 stars 3 forks source link

Loading XSLT Stylesheets from Classpath #2

Closed martinklepsch closed 5 years ago

martinklepsch commented 5 years ago

Hello again,

I'm wondering if there's any particular support to compile XSLT stylesheets from the classpath or a raw string?

(str (xslt/transform (xslt/compile-xslt (.getPath to-html-xslt)) content))

This works well when the file is a regular file in the filesystem but fails if the file is within a Jar.

It seems that it would be possible to turn a resource into an input stream and then into a StreamSource.

Am I missing something or is this just something you didn't yet need? Would be happy to provide a PR.

martinklepsch commented 5 years ago

But maybe this is also perfectly acceptable:

(xslt/transform (xslt/compile-source (StreamSource. (io/input-stream to-html-xslt))) (slurp f))
eerohele commented 5 years ago

I don't think you're missing anything. Your latter example is probably the most straightforward solution at the moment.

Unfortunately, it seems I've made a poor API choice here: similarly to compile-edn, compile-xslt should probably be a function that accepts anything that can be turned into a java.io.InputStream. As in:

(xslt/transform (xslt/compile-xslt (io/file "resources/foo.xsl")))

;; or

(xslt/transform (xslt/compile-xslt (io/resource "foo.xslt")))

However, I don't want to break backwards compatibility.

One option would be to modify compile-xslt such that it checks whether the input argument is already something that can be turned into an InputStream. If yes, simply pass it to io/input-stream. Otherwise, assume it is a file path and compile it as before.

Maybe checking whether the input arguments ~satisfies~ is an instance of IOFactory would do the trick? Didn't have the time to investigate more than that yet, though.

PR more than welcome if you're up to it!

eerohele commented 5 years ago

Actually, it appears that io/input-stream already considers a string argument it can't parse into a URL a file path, so the fix is fortunately quite simple.

Pushed v0.2.2. With it, this should work:

(xslt/compile-xslt (io/resource "path/to/classpath/stylesheet.xsl"))
martinklepsch commented 5 years ago

That's a sweet solution! 👍