You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Bob Phillips <ra...@easystreet.com> on 2001/10/21 21:00:19 UTC

cocoon2 generator implementation query

I have been looking at making a cocoon2 generator and would be
interested in advice or criticism about my strategy.


I have standalone sax2  test programs that implement a 'fake parser'
like michal kay's gedcom parser reading a text file, emitting sax events

written to an xml file that is (possibly) transformed. I am breaking up
a text file into <line><word>.

another, similar parser reads comma delimited files from (for example)
excel.
--------------

Looking at the ../generation sources, I chose the HTML generator as a
model. It, however, uses DOM
and then converts it into SAX events.

The 'PoemtxtParser' emits sax events & I expect to hook it into the
{pipeline?} that is defined in the
sitemap by

 <map:generator name="poemtxt"
src="org.apache.cocoon.generation.PoemtxtGenerator" label="content"/>

in the generator section

and

 <map:match pattern="poemtxt">
     <map:generate type="poemtxt" src="poemtxt/**.txt"/>

     <map:transform type="xslt"
src="stylesheets/poemtxt/xpoem-html.xsl"/>
     <map:serialize type="html"/>
    </map:match>
---------------------------------------
---------------------------------------

The main program of my existing code writes to system.out and I need, I
believe to replace this output source with a connection to the cocoon2
pipeline. Here is the main excerpt:

 String poemtxtFileName = args[0];
      String xsltFileName = (args.length > 1) ? args[1] : null;

      TransformerFactory transFact = TransformerFactory.newInstance();
      if (transFact.getFeature(SAXTransformerFactory.FEATURE)) {
        SAXTransformerFactory saxTransFact = (SAXTransformerFactory)
transFact;
        TransformerHandler transHand = null;
        if (xsltFileName == null) {
          transHand = saxTransFact.newTransformerHandler();
        } else {
          transHand = saxTransFact.newTransformerHandler(new
StreamSource(new File(xsltFileName)));
        }

        // set the desitnation for the xslt transformation
        transHand.setResult(new StreamResult(System.out));

        //hook the poemtxtParser to the poemtxtFile
        PoemtxtParser poemtxtReader = new PoemtxtParser();
        InputSource poemtxtInputSrc = new InputSource(new
FileReader(poemtxtFileName));

        // attach the XSLT processor to the poemtxtParser
        poemtxtReader.setContentHandler(transHand);
        poemtxtReader.parse(poemtxtInputSrc);

--------------------------
--------------------------

The PoemtxtParser.parse  implements XMLReader and Locator

and emits calls to contentHandler.startDocument and .StartElement (and
so on).

-------------------------

Does this make sense or am I missing something already existing in
Cocoon2 that
would accomplish my goal/

regards

Bob Phillips








---------------------------------------------------------------------
Please check that your question has not already been answered in the
FAQ before posting. <http://xml.apache.org/cocoon/faqs.html>

To unsubscribe, e-mail: <co...@xml.apache.org>
For additional commands, e-mail: <co...@xml.apache.org>


Re: cocoon2 generator implementation query

Posted by "Laurens M. Fridael" <fr...@planet.nl>.
Hi,


Here are my thoughts.

For the sake of convenience you should derive your Generator from
"AbstractGenerator" (or any of its descendants). The protected field
"contentHandler" lets you send SAX events down the pipeline. The protected
field "source" represents the URI according to the mapping defined in the
Sitemap. You can use the protected field "resolver" to resolve the source
into a SAX InputSource.

Here's the code from my own DocumentGenerator class. This class generates a
simple document structure from a text file using a DocumentReader(an
XMLReader implementation that converts a text file into a stream of SAX
events). The DocumentGenerator is paired with a DocumentSerializer which can
produce a Palm PDB file from SAX events.

-----------------------------------------------
package distantcord.palm;

import distantcord.palm.DocumentReader;
import java.io.IOException;
import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.ResourceNotFoundException;
import org.apache.cocoon.generation.AbstractGenerator;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class DocumentGenerator extends AbstractGenerator {

    public void generate() throws IOException, SAXException,
            ProcessingException {
        // Obtain a SAX InputSource.
        InputSource inp = resolver.resolve(source).getInputSource();
        // Create a DocumentReader. This generates ContentHandler events.
        DocumentReader docReader = new DocumentReader();
        // Set the ContentHandler.
        docReader.setContentHandler(contentHandler);
        // Parse the InputSource. This starts off the stream of SAX events.
        docReader.parse(inp);
    }

}
-----------------------------------------------

The purpose of the class is beside the point. What I want to say is that you
should separate the parsing logic (the XMLReader implementation ) from the
Generator. This way you can reuse the parser in a standalone SAX
application. In fact, I think you could make a SAX-based Generator class
generic so that you could supply the class name of your XMLReader
implementation as an initialization parameter. You would then use
Class.newInstance() or another reflection mechanism to instantiate the class
at runtime.

There is no need to use a Transformer to convert your SAX events into an XML
stream first. You can send the SAX events down the pipeline immediately.

The mapping in your sitemap is wrong I think. The wildcard pattern must be
specified in <map:match> like this:

<map:match pattern="**.txt">
     <map:generate type="poemtxt" src="poemtxt/{1}.txt"/>
     <map:transform type="xslt" src="stylesheets/poemtxt/xpoem-html.xsl"/>
     <map:serialize type="html"/>
</map:match>


Regards
-Laurens

----- Original Message -----
From: "Bob Phillips" <ra...@easystreet.com>
To: <co...@xml.apache.org>
Sent: Sunday, October 21, 2001 9:00 PM
Subject: cocoon2 generator implementation query


> I have been looking at making a cocoon2 generator and would be
> interested in advice or criticism about my strategy.



---------------------------------------------------------------------
Please check that your question has not already been answered in the
FAQ before posting. <http://xml.apache.org/cocoon/faqs.html>

To unsubscribe, e-mail: <co...@xml.apache.org>
For additional commands, e-mail: <co...@xml.apache.org>