cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kay Michael <Michael....@icl.com>
Subject TRAX: suggested revised examples
Date Fri, 11 Feb 2000 11:02:21 GMT
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