001 /* 002 * Copyright 1999-2004 The Apache Software Foundation. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.kew.transformation; 017 018 import java.io.IOException; 019 020 import javax.xml.transform.TransformerConfigurationException; 021 import javax.xml.transform.TransformerException; 022 import javax.xml.transform.TransformerFactory; 023 import javax.xml.transform.sax.SAXResult; 024 import javax.xml.transform.sax.SAXSource; 025 import javax.xml.transform.sax.SAXTransformerFactory; 026 import javax.xml.transform.sax.TransformerHandler; 027 import javax.xml.transform.stream.StreamSource; 028 029 import org.apache.xml.serializer.OutputPropertiesFactory; 030 import org.apache.xml.serializer.Serializer; 031 import org.apache.xml.serializer.SerializerFactory; 032 import org.junit.Test; 033 import org.kuali.rice.kew.test.KEWTestCase; 034 import org.xml.sax.InputSource; 035 import org.xml.sax.SAXException; 036 import org.xml.sax.XMLReader; 037 import org.xml.sax.helpers.XMLReaderFactory; 038 039 /** 040 * This example shows how to chain a series of transformations by 041 * piping SAX events from one Transformer to another. Each Transformer 042 * operates as a SAX2 XMLFilter/XMLReader. 043 */ 044 public class Pipe extends KEWTestCase 045 { 046 @Test 047 public void testPipe() 048 throws TransformerException, TransformerConfigurationException, 049 SAXException, IOException 050 { 051 // Instantiate a TransformerFactory. 052 TransformerFactory tFactory = TransformerFactory.newInstance(); 053 // Determine whether the TransformerFactory supports The use uf SAXSource 054 // and SAXResult 055 if (tFactory.getFeature(SAXSource.FEATURE) && tFactory.getFeature(SAXResult.FEATURE)) 056 { 057 // Cast the TransformerFactory to SAXTransformerFactory. 058 SAXTransformerFactory saxTFactory = ((SAXTransformerFactory) tFactory); 059 // Create a TransformerHandler for each stylesheet. 060 TransformerHandler tHandler1 = saxTFactory.newTransformerHandler(new StreamSource(Pipe.class.getResourceAsStream("foo1.xsl"))); 061 TransformerHandler tHandler2 = saxTFactory.newTransformerHandler(new StreamSource(Pipe.class.getResourceAsStream("foo2.xsl"))); 062 TransformerHandler tHandler3 = saxTFactory.newTransformerHandler(new StreamSource(Pipe.class.getResourceAsStream("foo3.xsl"))); 063 064 // Create an XMLReader. 065 XMLReader reader = XMLReaderFactory.createXMLReader(); 066 reader.setContentHandler(tHandler1); 067 reader.setProperty("http://xml.org/sax/properties/lexical-handler", tHandler1); 068 069 tHandler1.setResult(new SAXResult(tHandler2)); 070 tHandler2.setResult(new SAXResult(tHandler3)); 071 072 // transformer3 outputs SAX events to the serializer. 073 java.util.Properties xmlProps = OutputPropertiesFactory.getDefaultMethodProperties("xml"); 074 xmlProps.setProperty("indent", "yes"); 075 xmlProps.setProperty("standalone", "no"); 076 Serializer serializer = SerializerFactory.getSerializer(xmlProps); 077 serializer.setOutputStream(System.out); 078 tHandler3.setResult(new SAXResult(serializer.asContentHandler())); 079 080 // Parse the XML input document. The input ContentHandler and output ContentHandler 081 // work in separate threads to optimize performance. 082 reader.parse(new InputSource(Pipe.class.getResourceAsStream("foo.xml"))); 083 } 084 } 085 }