You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by Tony Collen <co...@umn.edu> on 2004/05/19 23:06:13 UTC

Custom Transformers

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 
stream.

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
}

====
Regards,

Tony


Re: AW: Custom Transformers

Posted by Joerg Heinicke <jo...@gmx.de>.
On 21.05.2004 05:31, Tony Collen wrote:

> By using the LogTransformer before and after my transformer, it seems 
> that once startRecording() is called, no events are sent to my 
> tranformer.  I would assume that calling endRecording() would turn that 
> off, but that doesn't seem to be the case.  Can anyone tell me where I'm 
> going wrong?  I notices the SourceWritingTransformer keeps a stack... am 
> I going to have to keep track of the elements I see once I've started 
> recording?

Hello Tony,

from what I see it's not your fault. Something is broken with the 
TextRecorder/AbstractSAXTransformer. All the events after 
startTextRecording() go to the TextRecorder() and not to your 
transformer, so endElement() is never called on your transformer, but 
goes to the TextRecorder() that inherits the empty implementation of 
endElement() from the NOPRecorder. Unfortunately I don't see how to fix it.

Joerg


Re: AW: Custom Transformers

Posted by Tony Collen <co...@umn.edu>.
Round II:

Here's my transformer code, (I know there's extra imports I don't need just yet):
-----
package com.openweather.cocoon.transformation;

import org.apache.cocoon.transformation.AbstractSAXTransformer;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.cocoon.ProcessingException;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.logger.Logger;

import org.xml.sax.SAXException;
import org.xml.sax.Attributes;

import java.util.Map;
import java.io.IOException;

public class CAPDescriptionTransformer
     extends AbstractSAXTransformer {

     protected String NAMESPACE = "http://www.incident.com/cap/1.0";
     protected String DESCRIPTION = "description";
     private String text = null;

     public void startElement(String uri, String name, String raw, Attributes attr)
     throws SAXException {

         super.startElement(uri, name, raw, attr);

         if ( NAMESPACE.equals(uri) && DESCRIPTION.equals(name) ) {
             this.text = null;
             startTextRecording();
         }
     }

     public void endElement(String uri, String name, String raw, Attributes attr)
     throws SAXException {

         if ( NAMESPACE.equals(uri) && DESCRIPTION.equals(name) ) {
             // we found the element

             // get the text and process
             this.text = endTextRecording();


             // send the processed characters to the next pipeline component
             characters( process(this.text).toCharArray(), 0, text.length() );
         }

         // end the element
         super.endElement(uri, name, raw);
     }

     private String process(String text) {
         System.out.println("inside process()");

         // @TODO: finish me

         // arbitrarily process the text

         System.out.println("leaving process before return():");
         return text.toLowerCase();
     }
}

------


package com.openweather.cocoon.transformation;

import org.apache.cocoon.transformation.AbstractSAXTransformer;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.cocoon.ProcessingException;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.logger.Logger;

import org.xml.sax.SAXException;
import org.xml.sax.Attributes;

import java.util.Map;
import java.io.IOException;

public class CAPDescriptionTransformer
     extends AbstractSAXTransformer {

     protected String NAMESPACE = "http://www.incident.com/cap/1.0";
     protected String DESCRIPTION = "description";
     private String text = null;

     public void startElement(String uri, String name, String raw, Attributes attr)
     throws SAXException {

         super.startElement(uri, name, raw, attr);

         if ( NAMESPACE.equals(uri) && DESCRIPTION.equals(name) ) {
             this.text = null;
             startTextRecording();
         }
     }

     public void endElement(String uri, String name, String raw, Attributes attr)
     throws SAXException {

         if ( NAMESPACE.equals(uri) && DESCRIPTION.equals(name) ) {
             // we found the element

             // get the text and process
             this.text = endTextRecording();


             // send the processed characters to the next pipeline component
             characters( process(this.text).toCharArray(), 0, text.length() );
         }

         // end the element
         super.endElement(uri, name, raw);
     }

     private String process(String text) {
         System.out.println("inside process()");

         // @TODO: finish me

         // arbitrarily process the text

         System.out.println("leaving process before return():");
         return text.toLowerCase();
     }
}

By using the LogTransformer before and after my transformer, it seems that once 
startRecording() is called, no events are sent to my tranformer.  I would assume 
that calling endRecording() would turn that off, but that doesn't seem to be the 
case.  Can anyone tell me where I'm going wrong?  I notices the 
SourceWritingTransformer keeps a stack... am I going to have to keep track of 
the elements I see once I've started recording?


Regards,

Tony

Re: AW: Custom Transformers

Posted by Tony Collen <co...@umn.edu>.
Marco Rolappe wrote:

> hi tony,
> 
> AFAIR all that recording functionality you need exists in
> AbstractSAXTransformer. so, slightly modified, that pseudocode should do it.
> 

Marco,

Aha! Thanks for pointing me in the right direction.

Tony



AW: Custom Transformers

Posted by Marco Rolappe <m_...@web.de>.
hi tony,

AFAIR all that recording functionality you need exists in
AbstractSAXTransformer. so, slightly modified, that pseudocode should do it.

> -----Ursprungliche Nachricht-----
> Von: dev-return-61151-m_rolappe=web.de@cocoon.apache.org
> [mailto:dev-return-61151-m_rolappe=web.de@cocoon.apache.org]Im Auftrag
> von Tony Collen
> Gesendet: Mittwoch, 19. Mai 2004 23:06
> An: dev@cocoon.apache.org
> Betreff: Custom Transformers
>
>
> 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
> stream.
>
> 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
> }
>
> ====
> Regards,
>
> Tony
>