Nevermind,
   My ultimate problem was I wasn't calling super.setup() in my
transformer's setup method, thus the namespaceURI variable was not
getting initialized (i.e. it was null), and caused a
nullPointerException during translation.

As I struggled a little with this, and I couldn't find a such an example
anywhere, I thought I'd post my simple custom transformer here.  Please
take it with a grain of salt, I'm very new to this, so I'm sure there
are better ways, but this at least seems to work.  Also, the log stuff
isn't required, but nice to have when debugging.  It outputs the log
file to your tomcat (not your cocoon) directory.

And to the experts out there, please let me know of improvements I could
make to this basic setup...

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.Serializable;
import java.util.Map;
import java.util.Properties;

import java.io.IOException;
import javax.xml.transform.*;
import org.apache.cocoon.caching.CacheableProcessingComponent;
import org.xml.sax.SAXException;

import org.apache.avalon.framework.parameters.Parameters;
import org.apache.excalibur.source.SourceValidity;
import org.apache.excalibur.source.impl.validity.NOPValidity;

import org.apache.cocoon.environment.SourceResolver;
import org.apache.cocoon.xml.*;
import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.transformation.AbstractSAXTransformer;
import org.apache.cocoon.caching.CacheableProcessingComponent;

public class Simpletrans extends AbstractSAXTransformer implements
                CacheableProcessingComponent {

        File logFile = new File("Simpletrans.log");

        PrintStream log;
        Properties format;

        public Simpletrans() {
                format = new Properties();
                format.put(OutputKeys.METHOD, "xml");
                format.put(OutputKeys.OMIT_XML_DECLARATION, "yes");
        }

        public void setup(SourceResolver resolver, Map objectModel,
String src,
                        Parameters params) throws ProcessingException,
SAXException,
                        IOException {
                try {
                  super.setup(resolver, objectModel, src, params);
                        FileOutputStream logOut = new
FileOutputStream(logFile);
                        log = new PrintStream(logOut);
                        log.println("setup called!!");

                } catch (Exception e) {
                        e.printStackTrace(log);
                }
        }

        public void startDocument() throws SAXException {
                log.println("Start Document called!!");
                this.startSerializedXMLRecording(format);
        }

        public void endDocument() throws SAXException {
                String content;

                log.println("endDocument called!!");
                try {
                        content = this.endSerializedXMLRecording();
                } catch (ProcessingException pe) {
                        throw new SAXException(pe.getMessage());
                }

            log.println(content);
                StringXMLizable xmlStr = new StringXMLizable(content);
                xmlStr.toSAX(contentHandler);

        }

        public String generate(String xmlStr) {

                return xmlStr;
        }

        public Serializable getKey() {
                return "1";
        }

        public SourceValidity getValidity() {
                return NOPValidity.SHARED_INSTANCE;
        }
}

 

-----Original Message-----
From: Schmitz, Jeffrey A
Sent: Wednesday, March 07, 2007 10:26 AM
To: users@cocoon.apache.org
Subject: XML Transforms and namespaceURI

This is hopefully a very simple question.  I'm trying to figure out the
whole namespace concept as it applies to a custom transformation.  For
my xml data, I don't use namespaces, and I don't want any filtering on
any namespace, i.e. I want to process all the data in the xml file.  For
this situation, what do I set the namespaceURI string to in my custom
transformer, and what is the best way to set the string? 

Also, what would the format be if I actually want to filter on some
namespace?  I have read the following form the source code, but an
example would be very nice.

* <h3>Namespace handling</h3>
 * By setting the instance variable namespaceURI to the namespace the
 * events are filtered and only events with this namespace are send to
 * the two hooks: <code>startTransformingElement</code> and
 * <code>endTransformingElement</code>. It is possible to override the
default
 * namespace for the transformer by specifying the parameter
"namespaceURI"
 * in the pipeline. This avoids possible namespace collisions.

/**
     * The namespace used by the transformer for the SAX events
filtering.
     * This either equals to the [EMAIL PROTECTED] #defaultNamespaceURI} or to 
the
value
     * set by the <code>namespaceURI</code> sitemap parameter for the
pipeline.
     * Must never be null.
     */

Thanks,
Jeff

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to