View source on Github

Let's make an ePub

What is Java?

Requiring Java-the-language has to do with the idea that "we know Java, we'll understand Java code."

Requiring the JVM is usually about ease of deployment, increased security, etc.

Meet the Partially Powered Languages

XSLT, used for transforming XML.

XProc, an XML pipeline language. More on this later.

Ant, used to orchestrate all this.

Write some Java code that lists files in folder and outputs them as an XML file. Call that code before calling XSLT. Pass in a referenced to that generated file when calling the XSLT. Let XSLT do its normal stuff at that point.

So what's the problem?

We use Ant to orchestrate everything. It's what the user calls (perhaps ironically via a Bash script), it accepts parameters, and calls out to everything else. There's one piece of Java code to generate an XML file containing the list of files. XSLT takes the input XML files and produces the XHTML and XML files required by the ePub. Ant does some file copying as necessary, then ZIPs everything up into an ePub.

Nonetheless, for some reason that baffles me, using XProc, XSLT and Ant is still considered "Java" by our clients. I truly can't explain this mindset, I'm sorry I have no insight to share.

Why did you let this happen?

addAttr :: Name -> Text -> Node -> Node addAttr name value (NodeElement (Element tag attrs children)) = NodeElement (Element tag ((name, value) : attrs) (map (addAttr name value) children)) addAttr _ _ node = node

public static void addAttr(String name, String value, Node node) { if (node.getNodeType() != Node.ELEMENT_NODE) return; Element element = (Element) node; element.setAttribute(name, value); NodeList nl = element.getChildNodes(); for (int i = 0; i < nl.getLength(); ++i) { addAttr(name, value, nl.item(i)); } }

So... we should only use full-fledged languages?

Hamlet is nothing more than simple, syntactic sugar. You can easily trace everything it does into the corresponding Haskell code. XSLT, on the other hand, is a very powerful language, for which it is very difficult to reproduce features in Java.

Hamlet only has meaning within the context of a Haskell program. It reads variables from the surrounding code and returns a value, which must be used by Haskell. XSLT can exist entirely outside of a Java program.

Hamlet has absolutely no side effects. XSLT can be used to generate as many output files as it wants.

[ NodeElement $ Element "foo" [("bar", "baz")] [ NodeContent "some content" , NodeElement $ Element "bin" [] [] ] ]

[xml| <foo bar=baz> some content <bin> |]

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); Element foo = doc.createElement("foo"); doc.appendChild(foo); foo.setAttribute("bar", "baz"); foo.appendChild(doc.createTextNode("some content")); foo.appendChild(doc.createElement("bin"));

writeFile

Conclusion