01: /*
02: * Copyright 1999-2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: /*
17: * $Id: Pipe.java,v 1.3 2007/04/18 21:37:05 ewestfal Exp $
18: */
19: package edu.iu.uis.eden.transformation;
20:
21: import java.io.IOException;
22:
23: import javax.xml.transform.TransformerConfigurationException;
24: import javax.xml.transform.TransformerException;
25: import javax.xml.transform.TransformerFactory;
26: import javax.xml.transform.sax.SAXResult;
27: import javax.xml.transform.sax.SAXSource;
28: import javax.xml.transform.sax.SAXTransformerFactory;
29: import javax.xml.transform.sax.TransformerHandler;
30: import javax.xml.transform.stream.StreamSource;
31:
32: import org.apache.xml.serializer.OutputPropertiesFactory;
33: import org.apache.xml.serializer.Serializer;
34: import org.apache.xml.serializer.SerializerFactory;
35: import org.kuali.workflow.test.WorkflowTestCase;
36: import org.xml.sax.InputSource;
37: import org.xml.sax.SAXException;
38: import org.xml.sax.XMLReader;
39: import org.xml.sax.helpers.XMLReaderFactory;
40:
41: /**
42: * This example shows how to chain a series of transformations by
43: * piping SAX events from one Transformer to another. Each Transformer
44: * operates as a SAX2 XMLFilter/XMLReader.
45: */
46: public class Pipe extends WorkflowTestCase {
47: public void testPipe() throws TransformerException,
48: TransformerConfigurationException, SAXException,
49: IOException {
50: // Instantiate a TransformerFactory.
51: TransformerFactory tFactory = TransformerFactory.newInstance();
52: // Determine whether the TransformerFactory supports The use uf SAXSource
53: // and SAXResult
54: if (tFactory.getFeature(SAXSource.FEATURE)
55: && tFactory.getFeature(SAXResult.FEATURE)) {
56: // Cast the TransformerFactory to SAXTransformerFactory.
57: SAXTransformerFactory saxTFactory = ((SAXTransformerFactory) tFactory);
58: // Create a TransformerHandler for each stylesheet.
59: TransformerHandler tHandler1 = saxTFactory
60: .newTransformerHandler(new StreamSource(Pipe.class
61: .getResourceAsStream("foo1.xsl")));
62: TransformerHandler tHandler2 = saxTFactory
63: .newTransformerHandler(new StreamSource(Pipe.class
64: .getResourceAsStream("foo2.xsl")));
65: TransformerHandler tHandler3 = saxTFactory
66: .newTransformerHandler(new StreamSource(Pipe.class
67: .getResourceAsStream("foo3.xsl")));
68:
69: // Create an XMLReader.
70: XMLReader reader = XMLReaderFactory.createXMLReader();
71: reader.setContentHandler(tHandler1);
72: reader.setProperty(
73: "http://xml.org/sax/properties/lexical-handler",
74: tHandler1);
75:
76: tHandler1.setResult(new SAXResult(tHandler2));
77: tHandler2.setResult(new SAXResult(tHandler3));
78:
79: // transformer3 outputs SAX events to the serializer.
80: java.util.Properties xmlProps = OutputPropertiesFactory
81: .getDefaultMethodProperties("xml");
82: xmlProps.setProperty("indent", "yes");
83: xmlProps.setProperty("standalone", "no");
84: Serializer serializer = SerializerFactory
85: .getSerializer(xmlProps);
86: serializer.setOutputStream(System.out);
87: tHandler3.setResult(new SAXResult(serializer
88: .asContentHandler()));
89:
90: // Parse the XML input document. The input ContentHandler and output ContentHandler
91: // work in separate threads to optimize performance.
92: reader.parse(new InputSource(Pipe.class
93: .getResourceAsStream("foo.xml")));
94: }
95: }
96: }
|