cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tony Collen <>
Subject Custom Transformers
Date Wed, 19 May 2004 21:06:13 GMT
Hey everybody,

I'm about to dive in and finally write a custom transformer.

My use case is this:  I am going to look for a certain element in the stream with a specific

namespace, and when I see it, I need to get all the character content (i.e. everything that

characters() would return), manually process it and reformat it, and then place it back into
the SAX 

Looking at the ContentHandler API, it says that some implementations might chunk the data
coming to 
me in characters(), which complicates things, since I might not get the entire data, which
I need to 
be able to process in one piece.

Poking around, I found the TextRecorder in org.apache.cocoon.transformation.helpers, which
seems to 
be what I need.

So now I ask if this seems right, in presented in glorious pseudocode:

recording = false
TextRecorder recorder = new TextRecorder();

void startElement(ns, localname, qname, attrs) {

   if (ns.equals(myNs) && localname.equals(myElemName)) {
     this.recording = true;

   super.startElement(ns, localname, qname, attrs);

void endElement(ns, localname, qname, attrs) {

   if (ns.equals(myNs) && localname.equals(myElemName)) {
     // have we seen our element?

     this.recording = false
     String theText = process(this.recorder.getText());
     super.characters(theText.toCharArray(), 0, theText.length());
   super.endElement(ns, localname, qname, attrs)

void characters(char buf[], int start, int len) {

   if (this.recording) {
     this.recorder.characters(buf, start, len);
   } else {
     super.characters(buf, start, len);

String process(String myStr) {
   // process the string arbitrarily



View raw message