View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
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  package org.kuali.rice.kew.transformation;
17  
18  import java.io.IOException;
19  
20  import javax.xml.transform.TransformerConfigurationException;
21  import javax.xml.transform.TransformerException;
22  import javax.xml.transform.TransformerFactory;
23  import javax.xml.transform.sax.SAXResult;
24  import javax.xml.transform.sax.SAXSource;
25  import javax.xml.transform.sax.SAXTransformerFactory;
26  import javax.xml.transform.sax.TransformerHandler;
27  import javax.xml.transform.stream.StreamSource;
28  
29  import org.apache.xml.serializer.OutputPropertiesFactory;
30  import org.apache.xml.serializer.Serializer;
31  import org.apache.xml.serializer.SerializerFactory;
32  import org.junit.Test;
33  import org.kuali.rice.kew.test.KEWTestCase;
34  import org.xml.sax.InputSource;
35  import org.xml.sax.SAXException;
36  import org.xml.sax.XMLReader;
37  import org.xml.sax.helpers.XMLReaderFactory;
38  
39    /**
40     * This example shows how to chain a series of transformations by
41     * piping SAX events from one Transformer to another. Each Transformer
42     * operates as a SAX2 XMLFilter/XMLReader.
43     */
44  public class PipeTest extends KEWTestCase
45  {
46  	@Test
47  	public void testPipe()
48  	throws TransformerException, TransformerConfigurationException, 
49           SAXException, IOException	   
50  	{
51      // Instantiate  a TransformerFactory.
52    	TransformerFactory tFactory = TransformerFactory.newInstance();
53      // Determine whether the TransformerFactory supports The use uf SAXSource 
54      // and SAXResult
55      if (tFactory.getFeature(SAXSource.FEATURE) && tFactory.getFeature(SAXResult.FEATURE))
56      { 
57        // Cast the TransformerFactory to SAXTransformerFactory.
58        SAXTransformerFactory saxTFactory = ((SAXTransformerFactory) tFactory);	  
59        // Create a TransformerHandler for each stylesheet.
60        TransformerHandler tHandler1 = saxTFactory.newTransformerHandler(new StreamSource(PipeTest.class.getResourceAsStream("foo1.xsl")));
61        TransformerHandler tHandler2 = saxTFactory.newTransformerHandler(new StreamSource(PipeTest.class.getResourceAsStream("foo2.xsl")));
62        TransformerHandler tHandler3 = saxTFactory.newTransformerHandler(new StreamSource(PipeTest.class.getResourceAsStream("foo3.xsl")));
63      
64        // Create an XMLReader.
65  	    XMLReader reader = XMLReaderFactory.createXMLReader();
66        reader.setContentHandler(tHandler1);
67        reader.setProperty("http://xml.org/sax/properties/lexical-handler", tHandler1);
68  
69        tHandler1.setResult(new SAXResult(tHandler2));
70        tHandler2.setResult(new SAXResult(tHandler3));
71  
72        // transformer3 outputs SAX events to the serializer.
73        java.util.Properties xmlProps = OutputPropertiesFactory.getDefaultMethodProperties("xml");
74        xmlProps.setProperty("indent", "yes");
75        xmlProps.setProperty("standalone", "no");
76        Serializer serializer = SerializerFactory.getSerializer(xmlProps);
77        serializer.setOutputStream(System.out);
78        tHandler3.setResult(new SAXResult(serializer.asContentHandler()));
79  
80  	    // Parse the XML input document. The input ContentHandler and output ContentHandler
81        // work in separate threads to optimize performance.   
82        reader.parse(new InputSource(PipeTest.class.getResourceAsStream("foo.xml")));
83      }
84    }
85  }