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 Fri, 11 Feb 2000 17:07:28 GMT

> Stylesheet - an object that defines a specific kind of transformation

As I said, I don't think we should have an object name that conficts with
DOM2.

>     OutputProperties oprops = new OutputProperties();
>    oprops.setOutputStream(System.out);
...
>    OutputProperties oprops = new OutputProperties();
>    oprops.setContentHandler(new UserContentHandler());

Seems funny to have OutputProperties take both specification and a
ContentHandler or Stream.  I can probably live with it, but need to think
on it...

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

The InputProvider seems to be nearly the same object as the original
XSLTInputSource.  I can live with this, though the double layering of
InputProvider/InputSource seems somewhat strained.

> InputProvider - an object able to deliver a source document or stylesheet
as
a set of SAX events

This isn't an accurate description, as this needs to handle DOM input... in
the case of Xalan, it will read the tree directly and not go through a SAX
layer.

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

Does that mean we don't match SAX2's "pull" model?  It seems like we should
either get SAX2 to change the model, or support it (I think there ought to
be both push and pull given the need to support pull).  It would seem
awfully funny to me if TRaX doesn't support the XMLFilter class.

More later... got to go to a parent-teacher's conference for my daughter.

-scott




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




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