sboag 00/07/17 18:26:14
Modified: java/src/trax Examples.java
Log:
All examples except exampleUseAssociated now work in Xalan. (Except the
current examples use the Xerces serializers, instead of the TrAX API.)
Revision Changes Path
1.2 +73 -283 xml-xalan/java/src/trax/Examples.java
Index: Examples.java
===================================================================
RCS file: /home/cvs/xml-xalan/java/src/trax/Examples.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Examples.java 2000/06/20 16:30:16 1.1
+++ Examples.java 2000/07/18 01:26:13 1.2
@@ -14,6 +14,9 @@
import org.xml.sax.SAXException;
import org.xml.sax.Parser;
import org.xml.sax.helpers.ParserAdapter;
+import org.xml.sax.helpers.XMLReaderFactory;
+import org.xml.sax.XMLReader;
+import org.xml.sax.ContentHandler;
// Needed DOM classes
import org.w3c.dom.Node;
@@ -23,6 +26,10 @@
import org.apache.xml.serialize.Serializer;
import org.apache.xml.serialize.SerializerFactory;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
/**
* Some examples to show how the Simple API for Transformations
* could be used.
@@ -35,21 +42,28 @@
public static void main( String argv[] )
throws ProcessorException, ProcessorFactoryException,
- TransformException, SAXException, IOException
+ TransformException, SAXException, IOException,
+ ParserConfigurationException
{
+ System.out.println("==== exampleSimple ====");
exampleSimple("foo.xml", "foo.xsl");
+ System.out.println("\n==== exampleSAX2SAX ====");
+ exampleSAX2SAX("foo.xml", "foo.xsl");
+ System.out.println("\n==== exampleXMLFilter ====");
+ exampleXMLFilter("foo.xml", "foo.xsl");
+ System.out.println("\n==== exampleXMLFilterChain ====");
+ exampleXMLFilterChain("foo.xml", "foo.xsl", "t1.xsl", "t2.xsl");
+ System.out.println("\n==== exampleDOM2DOM ====");
+ exampleDOM2DOM("foo.xml", "foo.xsl");
+ System.out.println("\n==== exampleParam ====");
+ exampleParam("foo.xml", "param.xsl");
+ System.out.println("\n==== exampleOutputFormat ====");
+ exampleOutputFormat("foo.xml", "moo.xsl");
+ // System.out.println("==== exampleUseAssociated ====");
}
/**
* Show the simplest possible transformation from system id to output
stream.
- * <pre>
- Processor processor = Processor.newInstance("xslt");
-
- Templates templates = processor.process(new InputSource("t1.xsl"));
- Transformer transformer = templates.newTransformer();
-
- transformer.transform(new InputSource("foo.xml"), new
Result(System.out));
- * </pre>
*/
public static void exampleSimple(String sourceID, String xslID)
throws ProcessorException, ProcessorFactoryException,
@@ -65,263 +79,94 @@
/**
* Show the Transformer using SAX events in and SAX events out.
- * <pre>
- Processor processor = Processor.newInstance("xslt");
-
- // Use the JAXP interface to get a SAX1 parser interface.
- SAXParserFactory factory = SAXParserFactory.newInstance();
- SAXParser saxparser = factory.newSAXParser();
- Parser parser = saxparser.getParser();
-
- // Have a Templates builder handle the parse events from the SAXParser's
- // parse of an xslt file.
- TemplatesBuilder templatesBuilder = processor.getTemplatesBuilder();
- parser.setDocumentHandler(templatesBuilder);
- parser.parse("t1.xsl");
- Templates templates = templatesBuilder.getTemplates();
-
- // Get a transformer instance for the templates.
- Transformer transformer = templates.newTransformer();
-
- // Set the result handling to be a serialization to System.out.
- Serializer serializer = SerializerFactory.getSerializer( "xml" );
- serializer.setOutputStream(System.out);
- transformer.setContentHandler(serializer.asContentHandler());
-
-
- // Is it my imagination, or is there no way to set the DeclHandler and
- // the LexicalHandler, since there seems to be no way to get an
- // XMLReader. This is a rather major problem. Hopefully, that will
- // be fixed in the next round.
-
- // Cause the transformation to occur by asking the parser to send
- // the parse events from "foo.xsl" to the transformer.
- parser.setDocumentHandler(transformer.getInputContentHandler());
- parser.parse("foo.xml");
- * </pre>
*/
- /*
- public static void exampleSAX2SAX()
+ public static void exampleSAX2SAX(String sourceID, String xslID)
throws SAXException, IOException // , ParserConfigurationException
{
Processor processor = Processor.newInstance("xslt");
- // Use the JAXP interface to get a SAX1 parser interface.
- SAXParserFactory factory = SAXParserFactory.newInstance();
- SAXParser saxparser = factory.newSAXParser();
- Parser parser = saxparser.getParser();
+ XMLReader reader = XMLReaderFactory.createXMLReader();
// Have a Templates builder handle the parse events from the SAXParser's
// parse of an xslt file.
TemplatesBuilder templatesBuilder = processor.getTemplatesBuilder();
- parser.setDocumentHandler(templatesBuilder);
- parser.parse("t1.xsl");
+ reader.setContentHandler(templatesBuilder);
+ if(templatesBuilder instanceof org.xml.sax.ext.LexicalHandler)
+ reader.setProperty("http://xml.org/sax/properties/lexical-handler",
templatesBuilder);
+ reader.parse(xslID);
Templates templates = templatesBuilder.getTemplates();
// Get a transformer instance for the templates.
Transformer transformer = templates.newTransformer();
// Set the result handling to be a serialization to System.out.
- Serializer serializer = SerializerFactory.getSerializer( "xml" );
- serializer.setOutputStream(System.out);
+ SerializerFactory sf = SerializerFactory.getSerializerFactory("xml");
+ Serializer serializer = sf.makeSerializer(System.out, new
OutputFormat());
transformer.setContentHandler(serializer.asContentHandler());
-
-
- // Is it my imagination, or is there no way to set the DeclHandler and
- // the LexicalHandler, since there seems to be no way to get an
- // XMLReader. This is a rather major problem. Hopefully, that will
- // be fixed in the next round.
// Cause the transformation to occur by asking the parser to send
// the parse events from "foo.xsl" to the transformer.
- parser.setDocumentHandler(transformer.getInputContentHandler());
- parser.parse("foo.xml");
- }
- */
-
- /**
- * Show the Transformer using the JAXP interface for input.
- * <pre>
- Processor processor = Processor.newInstance("xslt");
-
- // The transformer will use a SAX parser as it's reader.
- SAXParserFactory factory = SAXParserFactory.newInstance();
- SAXParser saxparser = factory.newSAXParser();
-
- // Have a Templates builder handle the parse events from the SAXParser's
- // parse of an xslt file.
- TemplatesBuilder templatesBuilder = processor.getTemplatesBuilder();
- saxparser.parse("t1.xsl", templatesBuilder);
- Templates templates = templatesBuilder.getTemplates();
-
- // Get a transformer instance for the templates.
- Transformer transformer = templates.newTransformer();
-
- // Set the result handling to be a serialization to System.out.
- Serializer serializer = SerializerFactory.getSerializer( "xml" );
- serializer.setOutputStream(System.out);
- transformer.setContentHandler(serializer.asContentHandler());
-
-
- // Is it my imagination, or is there no way to set the DeclHandler and
- // the LexicalHandler, since there seems to be no way to get an
- // XMLReader. This is a rather major problem. Hopefully, that will
- // be fixed in the next round.
-
- // Cause the transformation to occur by asking the parser to send
- // the parse events from "foo.xsl" to the transformer.
- saxparser.parse("foo.xml", transformer.getInputContentHandler());
- * </pre>
- */
- /*
- public static void exampleJAXP2SAX2()
- throws SAXException, IOException // , ParserConfigurationException
- {
- Processor processor = Processor.newInstance("xslt");
-
- // The transformer will use a SAX parser as it's reader.
- SAXParserFactory factory = SAXParserFactory.newInstance();
- SAXParser saxparser = factory.newSAXParser();
-
- // Have a Templates builder handle the parse events from the SAXParser's
- // parse of an xslt file.
- TemplatesBuilder templatesBuilder = processor.getTemplatesBuilder();
- saxparser.parse("t1.xsl", templatesBuilder);
- Templates templates = templatesBuilder.getTemplates();
-
- // Get a transformer instance for the templates.
- Transformer transformer = templates.newTransformer();
-
- // Set the result handling to be a serialization to System.out.
- Serializer serializer = SerializerFactory.getSerializer( "xml" );
- serializer.setOutputStream(System.out);
- transformer.setContentHandler(serializer.asContentHandler());
-
-
- // Is it my imagination, or is there no way to set the DeclHandler and
- // the LexicalHandler, since there seems to be no way to get an
- // XMLReader. This is a rather major problem. Hopefully, that will
- // be fixed in the next round.
-
- // Cause the transformation to occur by asking the parser to send
- // the parse events from "foo.xsl" to the transformer.
- saxparser.parse("foo.xml", transformer.getInputContentHandler());
+ ContentHandler chandler = transformer.getInputContentHandler();
+ reader.setContentHandler(chandler);
+ if(chandler instanceof org.xml.sax.ext.LexicalHandler)
+ reader.setProperty("http://xml.org/sax/properties/lexical-handler",
chandler);
+ else
+ reader.setProperty("http://xml.org/sax/properties/lexical-handler",
null);
+ reader.parse(sourceID);
}
- */
-
/**
* Show the Transformer as a SAX2 XMLFilter/XMLReader. In this case
* the Transformer acts like a parser, and can in fact be polymorphicaly
* used in places where a SAX parser would be used.
- * <pre>
- Processor processor = Processor.newInstance("xslt");
-
- Templates templates = processor.process(new InputSource("t1.xsl"));
- Transformer transformer = templates.newTransformer();
-
- // Set the result handling to be a serialization to System.out.
- Serializer serializer = SerializerFactory.getSerializer( "xml" );
- serializer.setOutputStream(System.out);
- transformer.setContentHandler(serializer.asContentHandler());
-
- // The transformer will use a SAX parser as it's reader.
- SAXParserFactory factory = SAXParserFactory.newInstance();
- SAXParser parser = factory.newSAXParser();
- transformer.setParent(new ParserAdapter( parser.getParser() ));
-
- // Now, when you call transformer.parse, it will set itself as
- // the content handler for the parser object (it's "parent"), and
- // will then call the parse method on the parser.
- transformer.parse(new InputSource("foo.xml"));
- * </pre>
*/
- /*
- public static void exampleXMLFilter()
+ public static void exampleXMLFilter(String sourceID, String xslID)
throws SAXException, IOException// , ParserConfigurationException
{
Processor processor = Processor.newInstance("xslt");
- Templates templates = processor.process(new InputSource("t1.xsl"));
+ Templates templates = processor.process(new InputSource(xslID));
Transformer transformer = templates.newTransformer();
// Set the result handling to be a serialization to System.out.
- Serializer serializer = SerializerFactory.getSerializer( "xml" );
- serializer.setOutputStream(System.out);
+ SerializerFactory sf = SerializerFactory.getSerializerFactory("xml");
+ Serializer serializer = sf.makeSerializer(System.out, new
OutputFormat());
transformer.setContentHandler(serializer.asContentHandler());
// The transformer will use a SAX parser as it's reader.
- SAXParserFactory factory = SAXParserFactory.newInstance();
- SAXParser parser = factory.newSAXParser();
- transformer.setParent(new ParserAdapter( parser.getParser() ));
+ XMLReader reader = XMLReaderFactory.createXMLReader();
+ transformer.setParent(reader);
// Now, when you call transformer.parse, it will set itself as
// the content handler for the parser object (it's "parent"), and
// will then call the parse method on the parser.
- transformer.parse(new InputSource("foo.xml"));
+ transformer.parse(new InputSource(sourceID));
}
- */
/**
* This example shows how to chain events from one Transformer
* to another transformer, using the Transformer as a
* SAX2 XMLFilter/XMLReader.
- * <pre>
- Processor processor = Processor.newInstance("xslt");
-
- Templates stylesheet1 = processor.process(new InputSource("t1.xsl"));
- Transformer transformer1 = stylesheet1.newTransformer();
-
- Templates stylesheet2= processor.process(new InputSource("t2.xsl"));
- Transformer transformer2 = stylesheet2.newTransformer();
-
- Templates stylesheet3 = processor.process(new InputSource("t3.xsl"));
- Transformer transformer3= stylesheet3.newTransformer();
-
- // transformer1 will use a SAX parser as it's reader.
- SAXParserFactory factory = SAXParserFactory.newInstance();
- SAXParser parser = factory.newSAXParser();
- transformer1.setParent(new ParserAdapter( parser.getParser() ));
-
- // transformer2 will use transformer1 as it's reader.
- transformer2.setParent(transformer1);
-
- // transform3 will use transform2 as it's reader.
- transformer3.setParent(transformer2);
-
- // transform3 will output the events to the serializer.
- Serializer serializer = SerializerFactory.getSerializer( "xml" );
- serializer.setOutputStream(System.out);
- transformer3.setContentHandler(serializer.asContentHandler());
-
- // Now, when you call transformer3 to parse, it will set
- // itself as the ContentHandler for transform2, and
- // call transform2.parse, which will set itself as the
- // content handler for transform1, and call transform1.parse,
- // which will set itself as the content listener for the
- // SAX parser, and call parser.parse(new InputSource("foo.xml")).
- transformer3.parse(new InputSource("foo.xml"));
- * </pre>
*/
- /*
- public static void exampleXMLFilterChain()
+ public static void exampleXMLFilterChain(String sourceID, String xslID_1,
+ String xslID_2, String xslID_3)
throws SAXException, IOException// , ParserConfigurationException
{
Processor processor = Processor.newInstance("xslt");
- Templates stylesheet1 = processor.process(new InputSource("t1.xsl"));
+ Templates stylesheet1 = processor.process(new InputSource(xslID_1));
Transformer transformer1 = stylesheet1.newTransformer();
- Templates stylesheet2= processor.process(new InputSource("t2.xsl"));
+ Templates stylesheet2= processor.process(new InputSource(xslID_2));
Transformer transformer2 = stylesheet2.newTransformer();
- Templates stylesheet3 = processor.process(new InputSource("t3.xsl"));
+ Templates stylesheet3 = processor.process(new InputSource(xslID_3));
Transformer transformer3= stylesheet3.newTransformer();
+ XMLReader reader = XMLReaderFactory.createXMLReader();
+
// transformer1 will use a SAX parser as it's reader.
- SAXParserFactory factory = SAXParserFactory.newInstance();
- SAXParser parser = factory.newSAXParser();
- transformer1.setParent(new ParserAdapter( parser.getParser() ));
+ transformer1.setParent(reader);
// transformer2 will use transformer1 as it's reader.
transformer2.setParent(transformer1);
@@ -330,8 +175,8 @@
transformer3.setParent(transformer2);
// transform3 will output the events to the serializer.
- Serializer serializer = SerializerFactory.getSerializer( "xml" );
- serializer.setOutputStream(System.out);
+ SerializerFactory sf = SerializerFactory.getSerializerFactory("xml");
+ Serializer serializer = sf.makeSerializer(System.out, new
OutputFormat());
transformer3.setContentHandler(serializer.asContentHandler());
// Now, when you call transformer3 to parse, it will set
@@ -340,133 +185,78 @@
// content handler for transform1, and call transform1.parse,
// which will set itself as the content listener for the
// SAX parser, and call parser.parse(new InputSource("foo.xml")).
- transformer3.parse(new InputSource("foo.xml"));
+ transformer3.parse(new InputSource(sourceID));
}
- */
/**
* Show how to transform a DOM tree into another DOM tree.
* This uses the javax.xml.parsers to parse an XML file into a
* DOM, and create an output DOM.
- * <pre>
- Processor processor = Processor.newInstance("xslt");
-
- if(processor.getFeature("http://xml.org/trax/features/dom/input"))
- {
- Templates templates = processor.process(new InputSource("t1.xsl"));
- Transformer transformer = templates.newTransformer();
-
- DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
- DocumentBuilder docBuilder = dfactory.newDocumentBuilder();
- org.w3c.dom.Node outNode = docBuilder.newDocument();
- Node doc = docBuilder.parse(new InputSource("foo.xml"));
-
- transformer.transformNode(doc, new Result(outNode));
- }
- else
- {
- throw new org.xml.sax.SAXNotSupportedException("DOM node processing
not supported!");
- }
- * </pre>
*/
- /*
- public static void exampleDOM2DOM()
- throws SAXException, IOException// , ParserConfigurationException
+ public static void exampleDOM2DOM(String sourceID, String xslID)
+ throws SAXException, IOException, ParserConfigurationException
{
Processor processor = Processor.newInstance("xslt");
if(processor.getFeature("http://xml.org/trax/features/dom/input"))
{
- Templates templates = processor.process(new InputSource("t1.xsl"));
+ Templates templates = processor.process(new InputSource(xslID));
Transformer transformer = templates.newTransformer();
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dfactory.newDocumentBuilder();
- org.w3c.dom.Node outNode = docBuilder.newDocument();
- Node doc = docBuilder.parse(new InputSource("foo.xml"));
-
+ org.w3c.dom.Document outNode = docBuilder.newDocument();
+ Node doc = docBuilder.parse(new InputSource(sourceID));
+
transformer.transformNode(doc, new Result(outNode));
+
+ SerializerFactory sf = SerializerFactory.getSerializerFactory("xml");
+ Serializer serializer = sf.makeSerializer(System.out, new
OutputFormat());
+ serializer.asDOMSerializer().serialize(outNode);
}
else
{
throw new org.xml.sax.SAXNotSupportedException("DOM node processing
not supported!");
}
}
- */
/**
* This shows how to set a parameter for use by the templates.
- * <pre>
- Processor processor = Processor.newInstance("xslt");
-
- Templates templates = processor.process(new InputSource("t1.xsl"));
- Transformer transformer = templates.newTransformer();
- transformer.setParameter("my-param", "http://foo.com", "hello");
-
- transformer.transform(new InputSource("foo.xml"), new
Result(System.out));
- * </pre>
*/
- public static void exampleParam()
+ public static void exampleParam(String sourceID, String xslID)
throws ProcessorException, ProcessorFactoryException,
TransformException, SAXException, IOException
{
Processor processor = Processor.newInstance("xslt");
- Templates templates = processor.process(new InputSource("t1.xsl"));
+ Templates templates = processor.process(new InputSource(xslID));
Transformer transformer = templates.newTransformer();
- transformer.setParameter("my-param", "http://foo.com" /* namespace */,
"hello");
+ transformer.setParameter("my-param", null /* namespace */, "hello to
you!");
- transformer.transform(new InputSource("foo.xml"), new
Result(System.out));
+ transformer.transform(new InputSource(sourceID), new Result(System.out));
}
/**
* Show how to override output properties.
- * <pre>
- Processor processor = Processor.newInstance("xslt");
-
- Templates templates = processor.process(new InputSource("t1.xsl"));
- OutputFormat oprops = templates.getOutputFormat();
- oprops.setIndenting( true );
- Transformer transformer = templates.newTransformer();
- transformer.setOutputFormat(oprops);
-
- transformer.transform(new InputSource("foo.xml"), new
Result(System.out));
- * </pre>
*/
- public static void exampleOutputFormat()
+ public static void exampleOutputFormat(String sourceID, String xslID)
throws ProcessorException, ProcessorFactoryException,
TransformException, SAXException, IOException
{
Processor processor = Processor.newInstance("xslt");
- Templates templates = processor.process(new InputSource("t1.xsl"));
+ Templates templates = processor.process(new InputSource(xslID));
OutputFormat oprops = templates.getOutputFormat();
oprops.setIndenting( true );
Transformer transformer = templates.newTransformer();
transformer.setOutputFormat(oprops);
- transformer.transform(new InputSource("foo.xml"), new
Result(System.out));
+ transformer.transform(new InputSource(sourceID), new Result(System.out));
}
/**
* Show how to get stylesheets that are associated with a given
* xml document via the xml-stylesheet PI (see
http://www.w3.org/TR/xml-stylesheet/).
- * <pre>
- Processor processor = Processor.newInstance("xslt");
-
- InputSource docSouce = new InputSource("foo.xml");
- InputSource[] sources
- = processor.getAssociatedStylesheets(docSouce, "text/xslt", null,
null);
- // Processor may remember it read the given document...
-
- Templates templates = (null != sources) ?
- processor.processMultiple(sources) :
- processor.process(new
InputSource("default.xsl"));
-
- Transformer transformer = templates.newTransformer();
-
- transformer.transform(docSouce, new Result(System.out));
- * </pre>
*/
public static void exampleUseAssociated()
throws ProcessorException, ProcessorFactoryException,