cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Schmitz, Jeffrey A" <>
Subject RE: Custom Transformer
Date Thu, 01 Mar 2007 16:24:49 GMT

So something like this (did I get the SaxBuffer stuff right?):

public class Mytrans extends AbstractTransformer implements
		CacheableProcessingComponent {

    XMLConsumer tempConsumer;
    SaxBuffer mySaxConsumer = new SaxBuffer();
    String content;

    public void setup(SourceResolver resolver, Map objectModel, String src,
			Parameters params) throws ProcessingException, SAXException,
			IOException {

	tempConsumer = xmlConsumer;
	xmlConsumer = mySaxConsumer;


	public void endDocument() throws SAXException {
		content = mySaxConsumer.toString();
		xmlConsumer = tempConsumer;
		content = myTransform(content);
		StringXMLizable xmlStr = new StringXMLizable(content);



-----Original Message-----
From: Grzegorz Kossakowski [] 
Sent: Thursday, March 01, 2007 9:22 AM
Subject: Re: Custom Transformer

Schmitz, Jeffrey A napisaƂ(a):
> OK (you asked for it), basically I have some xml that needs to be massaged first by a
simple xslt before being fed into my transformer, and then once my transformer is done, I
need to result to be massaged again with another xslt.
> My transformer relies on some predefined and specific transformer libraries that I have
no control over, so the xml has to be massaged to fit into the format required by these libraries.
 If I can just build a string with the incoming xml (from the first xslt), I can then feed
this string of xml into the custom transform libraries (as long as the xml contained in the
string is structured correctly). So basically, here is what I need my custom tranformer to
> 1. Accept SAX events and build a string of the incoming xml 2. Perform 
> transform on the received xml string 3. Output the resulting xml in 
> the form of more Sax events for the next tranformer/serializer in line.  i.e. something
like this:
>       String message = doMyCustomTransform(incomingXMLString);
>       //Create a reader for turning a string into an InputSource
>       XMLReader xmlreader = XMLReaderFactory.createXMLReader();
>       //Set the content handler into the XMLReader class.
>       xmlreader.setContentHandler(contentHandler);
>       //Now feed the source into the xml reader, which will turn around and
>       //invoke the proper handlers in the contentHandler based on the
>       //string. 
>       InputSource source = new InputSource(new StringReader(message));
>       xmlreader.parse(source);
I think it's overkill to use reader here. You should use StringXMLizable[1] instead. Basically
you have to do this:
extend AbstractTransformer and in setup() remember it's current value of xmlConsumer and replace
by instance of SaxBuffer[2].
SaxBuffer will record all events and you can serialize them into String. 
Do your transformations and create StringXMLizable instance and call toSAX method with original
xmlConsumer as parameter.
You should do this in overloaded endDocument() methods but do not forget to call super method
otherwise your SaxBuffer would be incomplete.

Hope that helps.
> I've pretty much got steps 2 and 3 worked out.  I just need the step 1 part.
> Need anymore details?
No. I really like answering this kind of questions: precise, showing preparation and effort
involved before asking for anything.


Grzegorz Kossakowski

To unsubscribe, e-mail:
For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message