View Javadoc

1   /**
2    * Copyright 2005-2011 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.FileOutputStream;
19  
20  import javax.xml.transform.TransformerFactory;
21  import javax.xml.transform.URIResolver;
22  import javax.xml.transform.sax.SAXResult;
23  import javax.xml.transform.sax.SAXSource;
24  import javax.xml.transform.sax.SAXTransformerFactory;
25  import javax.xml.transform.sax.TransformerHandler;
26  import javax.xml.transform.stream.StreamSource;
27  
28  import org.apache.xml.serializer.OutputPropertiesFactory;
29  import org.apache.xml.serializer.Serializer;
30  import org.apache.xml.serializer.SerializerFactory;
31  import org.junit.Test;
32  import org.kuali.rice.kew.test.KEWTestCase;
33  import org.xml.sax.InputSource;
34  import org.xml.sax.XMLReader;
35  import org.xml.sax.helpers.XMLReaderFactory;
36  
37  public class PipeTransformationTest extends KEWTestCase {
38  	
39  	@Test public void testPipeUsingTemplate(){
40  		try{
41  			// Instantiate a TransformerFactory.
42  		  	TransformerFactory tFactory = TransformerFactory.newInstance();
43  		    // Determine whether the TransformerFactory supports The use uf SAXSource 
44  		    // and SAXResult
45  		    if (tFactory.getFeature(SAXSource.FEATURE) && tFactory.getFeature(SAXResult.FEATURE)){
46  		    	// Cast the TransformerFactory to SAXTransformerFactory.
47  		        SAXTransformerFactory saxTFactory = ((SAXTransformerFactory) tFactory);	  
48  		        // Get Relevant StyleSheets
49  		        URIResolver resolver = new WidgetURITestResolver();
50  		        saxTFactory.setURIResolver(resolver);
51  		        
52  		        /*
53  		        Document edlStyle,templateStyle;
54  		        edlStyle=XMLUtil.parseInputStream(PipeTransformationTest.class.getResourceAsStream("StudentInitiatedDrop_Style_pipeTest.xml"));
55  		        // Get Template Name
56  		        XPathFactory xPathFactory=XPathFactory.newInstance();
57  	            XPath xPath=xPathFactory.newXPath();
58  	            Node node=(Node)xPath.evaluate("//use[0]",edlStyle,XPathConstants.NODE);
59  	            //Node node=nodes.item(0);
60  	            if(node!=null){
61  	            	if(node.hasAttributes()){
62  	            		Node testAttri=node.getAttributes().getNamedItem("name");
63  	            		if(testAttri!=null){
64  	            			templateName=testAttri.getNodeValue();
65  	            		}else{
66  	            			//set default template as widgets
67  	            			templateName="widgets";
68  	            		}
69  	            	}
70  	            }
71  	           
72  	            System.out.println(templateName);
73  	             */
74  	            //templateStyle=XMLUtil.parseInputStream(PipeTransformationTest.class.getResourceAsStream("widgets_pipeTest.xml"));
75  		        // Create a TransformerHandler for each stylesheet.
76  		        TransformerHandler tHandler1 = saxTFactory.newTransformerHandler(new StreamSource(PipeTransformationTest.class.getResourceAsStream("StudentInitiatedDrop_Style_pipeTest.xml")));
77  		       // TransformerHandler tHandler2 = saxTFactory.newTransformerHandler(new StreamSource(PipeTransformationTest.class.getResourceAsStream("trans.xml")));
78  		        	//TransformerHandler tHandler3 = saxTFactory.newTransformerHandler(new StreamSource("foo3.xsl"));
79  		      
80  		        // Create an XMLReader.
81  		  	    XMLReader reader = XMLReaderFactory.createXMLReader();
82  		        reader.setContentHandler(tHandler1);
83  		        reader.setProperty("http://xml.org/sax/properties/lexical-handler", tHandler1);
84  
85  		        //tHandler1.setResult(new SAXResult(tHandler2));
86  		        	//tHandler2.setResult(new SAXResult(tHandler3));
87  
88  		        // transformer3 outputs SAX events to the serializer.
89  		        java.util.Properties xmlProps = OutputPropertiesFactory.getDefaultMethodProperties("xml");
90  		        xmlProps.setProperty("indent", "yes");
91  		        xmlProps.setProperty("standalone", "no");
92  		        Serializer serializer = SerializerFactory.getSerializer(xmlProps);
93  		        FileOutputStream transformedXML=new FileOutputStream("c://file.xml");
94  		        serializer.setOutputStream(transformedXML);
95  		        tHandler1.setResult(new SAXResult(serializer.asContentHandler()));
96  
97  		  	    // Parse the XML input document. The input ContentHandler and output ContentHandler
98  		        // work in separate threads to optimize performance.   
99  		        reader.parse(new InputSource(PipeTransformationTest.class.getResourceAsStream("StudentInitiatedDrop.xml")));
100 		    }
101 			
102 		}catch(Exception ex){
103 			ex.printStackTrace();
104 		}
105 	}
106 	
107 
108 }