Wednesday, September 16, 2009

XMLMarkupBuilder in groovy.

Since a while i wanted to find a reason to play with the xml markup builders in groovy, since I think they are an awesome idea.
So I gave it a shot


def document = new StreamingMarkupBuilder().bind({

config {
parameter {
param(name: "java.naming.provider.url", value: "127.0.0.1", public:true)
param(name: "java.naming.factory.initial", value: "org.jnp.interfaces.NamingContextFactory", public:true)
param(name: "java.naming.factory.url.pkgs", value: "org.jboss.naming:org.jnp.interfaces", public:true)
param(name: "edu.ucdavis.genomics.metabolomics.util.status.ReportFactory", value: "edu.ucdavis.genomics.metabolomics.binbase.cluster.status.EJBReportFactory")
param(name: "edu.ucdavis.genomics.metabolomics.util.thread.locking.LockableFactory", value: "edu.ucdavis.genomics.metabolomics.binbase.cluster.locking.EJBLockingFactory")

}
}
})

println document


and the result is


<config><parameter><param name='java.naming.provider.url' value='127.0.0.1' public='true'/><param name='java.naming.factory.initial' value='org.jnp.interfaces.NamingContextFactory'/><param name='java.naming.factory.url.pkgs' value='org.jboss.naming:org.jnp.interfaces'/><param name='edu.ucdavis.genomics.metabolomics.util.status.ReportFactory' value='edu.ucdavis.genomics.metabolomics.binbase.cluster.status.EJBReportFactory'/><param name='edu.ucdavis.genomics.metabolomics.util.thread.locking.LockableFactory' value='edu.ucdavis.genomics.metabolomics.binbase.cluster.locking.EJBLockingFactory'/></parameter></config>


Neat! This was very simple and the validation of the code happens at runtime. The only uggly part is that idea shows this part in red brackets


public:true


since public is a keyword of java/groovy. But it does compile...

Guess I have to change my api a bit to avoid this uggly keyword or at least make it optional.

No comments:

Post a Comment