cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Robby Pelssers <>
Subject RE: potential SAX builder helper class
Date Mon, 23 Apr 2012 14:40:01 GMT
Looks definitely like an improvement in usability.  +1


-----Original Message-----
From: [] On Behalf Of Simone Tripodi
Sent: Monday, April 23, 2012 4:36 PM
Subject: potential SAX builder helper class

Hi all guys,

One thing I've always found a little "annoying" - "boring" is maybe
the more appropriate therm - of SAX APIs is that, when crating even
simple XML snippets via ContentHandler APIs, the following boilerplate
code has to be written:

ContentHandler handler = ... ;
contentHandler.startElement( "", "project", "project", new AttributesImpl() );
contentHandler.startElement( "", "modelVersion", "modelVersion", new
AttributesImpl() );

String modelVersion = "4.0.0";
contentHandler.characters( modelVersion.toCharArray(), 0,
modelVersion.length() );

contentHandler.endElement( "", "modelVersion", "modelVersion" );
contentHandler.endElement( "", "project", "project", new AttributesImpl() );

I think you would agree with me that to obtain the following snippet

<?xml version="1.0" encoding="UTF-8"?>

that code is maybe an overkill :P

So, at company I developed a small SAX wrapper that would help on
transforming the previous code in the following:

ContentHandler handler = ... ;
SAXEventsBuilder.newDocument( transformerHandler )
    .start( "project" )
        .start( "modelVersion" ).body( "4.0.0" ).end()

isn't more "sexy"? It still supports elements that require
namespaces/attributes but reduces the lines of code for hardcoded XML
documents - especially when closing elements.

It also allows users to add manually-generated elements in an existing
ContentHandler handler = ... ;
SAXEventsBuilder.wrap( transformerHandler )
    .start( "modelVersion" ).body( "4.0.0" ).end()
    .start( "groupId" ).body( "org.apache.cocoon.sax" ).end()
    .start( "artifactId" ).body( "cocoon-sax" ).end()
    .start( "version" ).body( "3.0.0-beta-1" ).end()

without starting/closing the document.

If you like it, I would be pleased to commit it in the Cocoon repo -
as a side question: which component that would fill?

Many thanks in advance, all the best!
View raw message