cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Scott Boag/CAM/Lotus" <Scott_B...@lotus.com>
Subject Re: TRAX: suggested revised examples
Date Sun, 13 Feb 2000 16:12:50 GMT

Mike, I'm off traveling for a couple of days, and will have relatively
little access to email and fairly little time to work on this.  I won't be
at all offended if you want to roll a version of the interfaces that use
your suggestions.  My primary goal is that this be a combined brainstorm,
ending up with something that everyone can live with, and will implement.
If you don't have time to do a roll of the interfaces, I'll give it another
whirl when I get back, probably around Thursday or Friday.  By that time,
Assaf should have a new version of the Serializer interfaces also.

It would be really good to hear from James Clark on this subject, though I
know he's been traveling and is overall very busy lately.

I would like to get this wrapped up in the next couple of weeks, to the
point that we can do preliminary implementations and let people give 'em a
try.  However, we shouldn't rush the cooking too much either, so we'll do
the best we can.

-scott




                                                                                         
                         
                    Kay Michael                                                          
                         
                    <Michael.Kay@        To:     Scott Boag/CAM/Lotus@Lotus, Kay Michael
<Michael.Kay@icl.com>     
                    icl.com>             cc:     James Clark <jjc@jclark.com>, Steve
Muench                        
                                         <smuench@us.oracle.com>, Adam Winer <awiner@us.oracle.com>,
Assaf Arkin   
                    02/11/2000           <arkin@exoffice.com>, Eduardo.Pelegrillopart@eng.sun.com,
                
                    06:02 AM             sax@megginson.com, cocoon-dev@xml.apache.org, xalan-dev@xml.apache.org
   
                    Please               Subject:     TRAX: suggested revised examples   
                         
                    respond to                                                           
                         
                    cocoon-dev                                                           
                         
                                                                                         
                         
                                                                                         
                         





I've prepared below a revised set of examples that show how I think I would
like this API to look. Haven't done the work of retrofitting this to a set
of interface definitions.

The classes used here are:

Processor - a piece of software that can do transformations
Stylesheet - an object that defines a specific kind of transformation
InputProvider - an object able to deliver a source document or stylesheet
as
a set of SAX events
Transformation - an activation of a Stylesheet
OutputProperties - a description of the desired characteristics and
destination of the output of the transformation; and after the
transformation, a description of the actual characteristics.
QName - a qualified Name

I haven't found any need to expose Result. I've exposed Serializer simply
as
one of the properties in OutputProperties.

I haven't worried about what the names should be, though I'm personally
happy with these.

I've gone for a "push" pipeline model, because I don't think SAX2's "pull"
model meets our requirements. This is from painful experience of
integrating
Saxon with FOP, etc.

There is of course some finessing that could be done to add extra methods
that default some of the arguments, e.g. a default OutputProperties.

Mike Kay


package org.xml.trax;


/**
 * Just some examples to show how the Transformation API for XML
 * could be used.
 */

public class Examples
{
  public static void exampleSimple()
    throws TRAXException
  {
    //=============================
    // Simple file transformation from file to output stream
    Processor processor = Processor.newInstance();

    Stylesheet stylesheet = processor.readStylesheet(new
InputProvider("t1.xsl"));
    Transformation transform = stylesheet.newTransformation();

    OutputProperties oprops = new OutputProperties();
    oprops.setOutputStream(System.out);

    transform.process(new InputProvider("foo.xml"), oprops);

    String mime = oprops.getMediaType();
    //=============================
  }

  public static void exampleSAXOutput()
    throws TRAXException
  {
    //=============================
    // Send the result of the Transformation to a user-supplied SAX2
ContentHandler;
    // also uses a different SAX Parser for the stylesheet

    Processor processor = Processor.newInstance();

    InputProvider style = new InputProvider();
    style.setInputSource(new InputSource("file:/c:/t1.xsl"));
    style.setXSLReader(new com.microstar.aelfred.XMLReader());

    Stylesheet stylesheet = processor.readStylesheet(style);
    Transformation transform = stylesheet.newTransformation();

    OutputProperties oprops = new OutputProperties();
    oprops.setContentHandler(new UserContentHandler());

    transform.process(new InputProvider("foo.xml"), oprops);
    //=============================
  }

  public static void exampleDOMInput()
    throws TRAXException
  {
    //=============================
    // Get source input from a DOM Node
    Processor processor = Processor.newInstance();

    Stylesheet stylesheet = processor.readStylesheet(new
InputProvider("t1.xsl"));
    Transformation transform = stylesheet.newTransformation();

    Document doc = // some document //

    OutputProperties oprops = new OutputProperties();
    oprops.setOutputStream(System.out);
    transform.process(new InputProvider(doc), oprops);
    //=============================
  }


  public static void exampleParam()
    throws TRAXException
  {
    //=============================
    // Simple file transformation from file to output stream with
parameters
    // and setting of output properties to override those in stylesheet
    Processor processor = Processor.newInstance();

    Stylesheet stylesheet = processor.readStylesheet(new
InputProvider("t1.xsl"));
    Transformation transform = stylesheet.newTransformation();

    OutputProperties oprops = new OutputProperties();
    oprops.setOutputStream(System.out);
    oprops.setIndenting(true);
    oprops.setOverrideStylesheet(true);

    QName param = new QName("prefix:my-favorite-number",
"http://namespace.org/uri");
    transform.setParameter(param, new Double(93.7));
    transform.process(new InputProvider("foo.xml"), oprops);
    //=============================
  }

  public static void exampleSAXInput()
    throws TRAXException
  {
    //=============================
    // Transformation in which the application supplies input in the form
of
a stream of
    // SAX events

    Processor processor = Processor.newInstance();

    Stylesheet stylesheet = processor.readStylesheet(new
InputProvider("t1.xsl"));
    Transformation transform = stylesheet.newTransformation();

    OutputProperties oprops = new OutputProperties();
    oprops.setOutputStream(System.out);
    oprops.setIndenting(true);
    oprops.setOverrideStylesheet(true);
    transform.setOutputProperties(oprops);

    ContentHandler ch = transform.asContentHandler();
    ch.startDocument();
    ch.startElement("a", emptyAttributeList);
    ch.endElement("a");
    ch.endDocument();

    //=============================
  }


  public static void exampleChaining()
    throws TRAXException
  {
    //=============================
    // Chaining events from one transformation to another.

    Processor processor = Processor.newInstance();

    Stylesheet stylesheet1 = processor.readStylesheet(new
InputProvider("t1.xsl"));
    Transformation transform1 = stylesheet1.newTransformation();

    Stylesheet stylesheet2= processor.readStylesheet(new
InputProvider("t2.xsl"));
    Transformation transform2 = stylesheet2.newTransformation();

    Stylesheet  stylesheet3 = processor.readStylesheet(new
InputProvider("t3.xsl"));
    Transformation transform3= stylesheet3.newTransformation();

    OutputProperties oprops1 = new OutputProperties();
    transform1.setOutputProperties(oprops1);
    oprops1.setContentHandler(transform2.asContentHandler());

    OutputProperties oprops2 = new OutputProperties();
    transform2.setOutputProperties(oprops2);
    oprops2.setContentHandler(transform3.asContentHandler());

    OutputProperties oprops3 = new OutputProperties();
    transform3.setOutputProperties(oprops3);
    oprops3.setOutputStream(System.out);
  }

  public static void exampleAssociated()
    throws TRAXException
  {
    //=============================
    // Transform a document using the stylesheet identified in its source
    Processor processor = Processor.newInstance();

    InputProvider source = new InputProvider("source.xml");
    InputProvider style = processor.getAssociatedStylesheet(source);

    Stylesheet stylesheet = processor.readStylesheet(style);
    Transformation transform = stylesheet.newTransformation();

    OutputProperties oprops = new OutputProperties();
    oprops.setOutputStream(System.out);

    transform.process(source, oprops);

    String mime = oprops.getMediaType();
    //=============================
  }

  public static void exampleEmbedded()
    throws TRAXException
  {
    //=============================
    // Transform a document using the stylesheet identified and embedded
    // in its source
    Processor processor = Processor.newInstance();

    InputProvider source = new InputProvider("source.xml");
    InputProvider style = processor.getAssociatedStylesheet(source);
     // no difference: the fact that it's embedded doesn't need to be
visible!

    Stylesheet stylesheet = processor.readStylesheet(style);
    Transformation transform = stylesheet.newTransformation();

    OutputProperties oprops = new OutputProperties();
    oprops.setOutputStream(System.out);

    transform.process(source, oprops);

    String mime = oprops.getMediaType();
    //=============================
  }

  public static void exampleSerializer()
    throws TRAXException
  {
    //=============================
    // Use a different serializer from the processor's default one
    Processor processor = Processor.newInstance();

    Stylesheet stylesheet = processor.readStylesheet(new
InputProvider("t1.xsl"));
    Transformation transform = stylesheet.newTransformation();

    OutputProperties oprops = new OutputProperties();
    oprops.setSerializer(new MyFavoriteSerializer());
    oprops.setOutputStream(System.out);

    transform.process(new InputProvider("foo.xml"), oprops);

    String mime = oprops.getMediaType();
    //=============================
  }

}




Mime
View raw message