xquery4j in action

In my previous article, I introduced a wrapper library for Saxon, xquery4j http://github.com/rafalrusin/xquery4j.
Here, I will explain how to use it to create an article generator in Java and XQuery for XHTML, called Article. You can download it here: http://github.com/rafalrusin/Article. It’s a simple DSL for article generation.

I think it is something worth noticing, because the whole project took me just a while to implement and has interesting features. Those are:

  • embedded code syntax highlighting for a lot of programming languages (using external program highlight),
  • creating href entries for links, so you don’t need to type URL twice
  • it integrates natively with XHTML constructs

This is an example of an input it takes:

<a:article xmlns='http://www.w3.org/1999/xhtml' xmlns:a="urn:article">
Some text
<a:code lang="xml"><![CDATA[

]]>

It generates XHTML output for it, using command

./run <input.xml >output.xhtml

The interesting thing is that XQuery expression for this transformation is very simple to do in Saxon. This is the complete code of it:

declare namespace a="urn:article";
declare default element namespace "http://www.w3.org/1999/xhtml";

declare function a:processLine($l) {
for $i in $l/node()
return
typeswitch ($i)
case element(a:link, xs:untyped) return <a href=“{$i/text()}”>{$i/text()}
default return $i
};

declare function a:articleItem($i) {
typeswitch ($i)
case element(a:l, xs:untyped) return (a:processLine($i),
)

case element(a:code, xs:untyped) return
( a:highlight($i/text(), $i/@lang)/body/* ,
)

default return “error;”
};

<html xmlns=“http://www.w3.org/1999/xhtml”>

<br /> a.xml


<link rel=“stylesheet” type=“text/css” href=“highlight.css”/>


{
for $i in a:article/*
return
a:articleItem($i)
}

Inside this expression, there is bound a:highlight Java function, which takes two strings on input (a code and a language) and returns DOM Node containing XHTML output from highlight command.
Since there is not much trouble with manipulating DOM using xquery4j, we can get as simple solution as this for a:highlight function:

public static class Mod {
public static Node highlight(final String code, String lang) throws Exception {
Validate.notNull(lang);
final Process p = new ProcessBuilder("highlight", "-X", "--syntax", lang).start();
Thread t = new Thread(new Runnable() {

public void run() {
try {
OutputStream out = p.getOutputStream();
IOUtils.write(code, out);
out.flush();
out.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
t.start();
String result = IOUtils.toString(p.getInputStream());
t.join();
return DOMUtils.parse(result).getDocumentElement();
}
}

Please note that creating a separate thread for feeding input into highlight command is required, since Thread’s output queue is limited and potentially might lead to dead lock. So we need to concurrently collect output from spawned Process.
However at the end, when we need to convert a String to DOM and we use xquery4j’s DOMUtils.parse(result), so it’s a very simple construct.

You May Also Like

Grails render as JSON catch

One of a reasons your controller doesn't render a proper response in JSON format might be wrong package name that you use. It is easy to overlook. Import are on top of a file, you look at your code and everything seems to be fine. Except response is still not in JSON format.

Consider this simple controller:

class RestJsonCatchController {
def grailsJson() {
render([first: 'foo', second: 5] as grails.converters.JSON)
}

def netSfJson() {
render([first: 'foo', second: 5] as net.sf.json.JSON)
}
}

And now, with finger crossed... We have a winner!

$ curl localhost:8080/example/restJsonCatch/grailsJson
{"first":"foo","second":5}
$ curl localhost:8080/example/restJsonCatch/netSfJson
{first=foo, second=5}

As you can see only grails.converters.JSON converts your response to JSON format. There is no such converter for net.sf.json.JSON, so Grails has no converter to apply and it renders Map normally.

Conclusion: always carefully look at your imports if you're working with JSON in Grails!

Edit: Burt suggested that this is a bug. I've submitted JIRA issue here: GRAILS-9622 render as class that is not a codec should throw exception