001    /**
002     * Copyright 2005-2011 The Kuali Foundation
003     *
004     * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.FileOutputStream;
019    
020    import javax.xml.transform.TransformerFactory;
021    import javax.xml.transform.URIResolver;
022    import javax.xml.transform.sax.SAXResult;
023    import javax.xml.transform.sax.SAXSource;
024    import javax.xml.transform.sax.SAXTransformerFactory;
025    import javax.xml.transform.sax.TransformerHandler;
026    import javax.xml.transform.stream.StreamSource;
027    
028    import org.apache.xml.serializer.OutputPropertiesFactory;
029    import org.apache.xml.serializer.Serializer;
030    import org.apache.xml.serializer.SerializerFactory;
031    import org.junit.Test;
032    import org.kuali.rice.kew.test.KEWTestCase;
033    import org.xml.sax.InputSource;
034    import org.xml.sax.XMLReader;
035    import org.xml.sax.helpers.XMLReaderFactory;
036    
037    public class PipeTransformationTest extends KEWTestCase {
038            
039            @Test public void testPipeUsingTemplate(){
040                    try{
041                            // Instantiate a TransformerFactory.
042                            TransformerFactory tFactory = TransformerFactory.newInstance();
043                        // Determine whether the TransformerFactory supports The use uf SAXSource 
044                        // and SAXResult
045                        if (tFactory.getFeature(SAXSource.FEATURE) && tFactory.getFeature(SAXResult.FEATURE)){
046                            // Cast the TransformerFactory to SAXTransformerFactory.
047                            SAXTransformerFactory saxTFactory = ((SAXTransformerFactory) tFactory);   
048                            // Get Relevant StyleSheets
049                            URIResolver resolver = new WidgetURITestResolver();
050                            saxTFactory.setURIResolver(resolver);
051                            
052                            /*
053                            Document edlStyle,templateStyle;
054                            edlStyle=XMLUtil.parseInputStream(PipeTransformationTest.class.getResourceAsStream("StudentInitiatedDrop_Style_pipeTest.xml"));
055                            // Get Template Name
056                            XPathFactory xPathFactory=XPathFactory.newInstance();
057                        XPath xPath=xPathFactory.newXPath();
058                        Node node=(Node)xPath.evaluate("//use[0]",edlStyle,XPathConstants.NODE);
059                        //Node node=nodes.item(0);
060                        if(node!=null){
061                            if(node.hasAttributes()){
062                                    Node testAttri=node.getAttributes().getNamedItem("name");
063                                    if(testAttri!=null){
064                                            templateName=testAttri.getNodeValue();
065                                    }else{
066                                            //set default template as widgets
067                                            templateName="widgets";
068                                    }
069                            }
070                        }
071                       
072                        System.out.println(templateName);
073                         */
074                        //templateStyle=XMLUtil.parseInputStream(PipeTransformationTest.class.getResourceAsStream("widgets_pipeTest.xml"));
075                            // Create a TransformerHandler for each stylesheet.
076                            TransformerHandler tHandler1 = saxTFactory.newTransformerHandler(new StreamSource(PipeTransformationTest.class.getResourceAsStream("StudentInitiatedDrop_Style_pipeTest.xml")));
077                           // TransformerHandler tHandler2 = saxTFactory.newTransformerHandler(new StreamSource(PipeTransformationTest.class.getResourceAsStream("trans.xml")));
078                                    //TransformerHandler tHandler3 = saxTFactory.newTransformerHandler(new StreamSource("foo3.xsl"));
079                          
080                            // Create an XMLReader.
081                                XMLReader reader = XMLReaderFactory.createXMLReader();
082                            reader.setContentHandler(tHandler1);
083                            reader.setProperty("http://xml.org/sax/properties/lexical-handler", tHandler1);
084    
085                            //tHandler1.setResult(new SAXResult(tHandler2));
086                                    //tHandler2.setResult(new SAXResult(tHandler3));
087    
088                            // transformer3 outputs SAX events to the serializer.
089                            java.util.Properties xmlProps = OutputPropertiesFactory.getDefaultMethodProperties("xml");
090                            xmlProps.setProperty("indent", "yes");
091                            xmlProps.setProperty("standalone", "no");
092                            Serializer serializer = SerializerFactory.getSerializer(xmlProps);
093                            FileOutputStream transformedXML=new FileOutputStream("c://file.xml");
094                            serializer.setOutputStream(transformedXML);
095                            tHandler1.setResult(new SAXResult(serializer.asContentHandler()));
096    
097                                // Parse the XML input document. The input ContentHandler and output ContentHandler
098                            // work in separate threads to optimize performance.   
099                            reader.parse(new InputSource(PipeTransformationTest.class.getResourceAsStream("StudentInitiatedDrop.xml")));
100                        }
101                            
102                    }catch(Exception ex){
103                            ex.printStackTrace();
104                    }
105            }
106            
107    
108    }