View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    * 
4    * 
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * 
9    * http://www.opensource.org/licenses/ecl2.php
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.kew.transformation;
18  
19  import java.io.FileOutputStream;
20  
21  import javax.xml.transform.TransformerFactory;
22  import javax.xml.transform.URIResolver;
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.XMLReader;
36  import org.xml.sax.helpers.XMLReaderFactory;
37  
38  public class PipeTransformationTest extends KEWTestCase {
39  	
40  	@Test public void testPipeUsingTemplate(){
41  		try{
42  			// Instantiate a TransformerFactory.
43  		  	TransformerFactory tFactory = TransformerFactory.newInstance();
44  		    // Determine whether the TransformerFactory supports The use uf SAXSource 
45  		    // and SAXResult
46  		    if (tFactory.getFeature(SAXSource.FEATURE) && tFactory.getFeature(SAXResult.FEATURE)){
47  		    	// Cast the TransformerFactory to SAXTransformerFactory.
48  		        SAXTransformerFactory saxTFactory = ((SAXTransformerFactory) tFactory);	  
49  		        // Get Relevant StyleSheets
50  		        URIResolver resolver = new WidgetURITestResolver();
51  		        saxTFactory.setURIResolver(resolver);
52  		        
53  		        /*
54  		        Document edlStyle,templateStyle;
55  		        edlStyle=XMLUtil.parseInputStream(PipeTransformationTest.class.getResourceAsStream("StudentInitiatedDrop_Style_pipeTest.xml"));
56  		        // Get Template Name
57  		        XPathFactory xPathFactory=XPathFactory.newInstance();
58  	            XPath xPath=xPathFactory.newXPath();
59  	            Node node=(Node)xPath.evaluate("//use[0]",edlStyle,XPathConstants.NODE);
60  	            //Node node=nodes.item(0);
61  	            if(node!=null){
62  	            	if(node.hasAttributes()){
63  	            		Node testAttri=node.getAttributes().getNamedItem("name");
64  	            		if(testAttri!=null){
65  	            			templateName=testAttri.getNodeValue();
66  	            		}else{
67  	            			//set default template as widgets
68  	            			templateName="widgets";
69  	            		}
70  	            	}
71  	            }
72  	           
73  	            System.out.println(templateName);
74  	             */
75  	            //templateStyle=XMLUtil.parseInputStream(PipeTransformationTest.class.getResourceAsStream("widgets_pipeTest.xml"));
76  		        // Create a TransformerHandler for each stylesheet.
77  		        TransformerHandler tHandler1 = saxTFactory.newTransformerHandler(new StreamSource(PipeTransformationTest.class.getResourceAsStream("StudentInitiatedDrop_Style_pipeTest.xml")));
78  		       // TransformerHandler tHandler2 = saxTFactory.newTransformerHandler(new StreamSource(PipeTransformationTest.class.getResourceAsStream("trans.xml")));
79  		        	//TransformerHandler tHandler3 = saxTFactory.newTransformerHandler(new StreamSource("foo3.xsl"));
80  		      
81  		        // Create an XMLReader.
82  		  	    XMLReader reader = XMLReaderFactory.createXMLReader();
83  		        reader.setContentHandler(tHandler1);
84  		        reader.setProperty("http://xml.org/sax/properties/lexical-handler", tHandler1);
85  
86  		        //tHandler1.setResult(new SAXResult(tHandler2));
87  		        	//tHandler2.setResult(new SAXResult(tHandler3));
88  
89  		        // transformer3 outputs SAX events to the serializer.
90  		        java.util.Properties xmlProps = OutputPropertiesFactory.getDefaultMethodProperties("xml");
91  		        xmlProps.setProperty("indent", "yes");
92  		        xmlProps.setProperty("standalone", "no");
93  		        Serializer serializer = SerializerFactory.getSerializer(xmlProps);
94  		        FileOutputStream transformedXML=new FileOutputStream("c://file.xml");
95  		        serializer.setOutputStream(transformedXML);
96  		        tHandler1.setResult(new SAXResult(serializer.asContentHandler()));
97  
98  		  	    // Parse the XML input document. The input ContentHandler and output ContentHandler
99  		        // work in separate threads to optimize performance.   
100 		        reader.parse(new InputSource(PipeTransformationTest.class.getResourceAsStream("StudentInitiatedDrop.xml")));
101 		    }
102 			
103 		}catch(Exception ex){
104 			ex.printStackTrace();
105 		}
106 	}
107 	
108 
109 }