001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.transformation;
018:
019: import java.io.FileOutputStream;
020:
021: import javax.xml.transform.TransformerFactory;
022: import javax.xml.transform.URIResolver;
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.workflow.test.WorkflowTestCase;
034: import org.xml.sax.InputSource;
035: import org.xml.sax.XMLReader;
036: import org.xml.sax.helpers.XMLReaderFactory;
037:
038: public class PipeTransformationTest extends WorkflowTestCase {
039:
040: @Test
041: public void testPipeUsingTemplate() {
042: try {
043: // Instantiate a TransformerFactory.
044: TransformerFactory tFactory = TransformerFactory
045: .newInstance();
046: // Determine whether the TransformerFactory supports The use uf SAXSource
047: // and SAXResult
048: if (tFactory.getFeature(SAXSource.FEATURE)
049: && tFactory.getFeature(SAXResult.FEATURE)) {
050: // Cast the TransformerFactory to SAXTransformerFactory.
051: SAXTransformerFactory saxTFactory = ((SAXTransformerFactory) tFactory);
052: // Get Relevant StyleSheets
053: URIResolver resolver = new WidgetURITestResolver();
054: saxTFactory.setURIResolver(resolver);
055:
056: /*
057: Document edlStyle,templateStyle;
058: edlStyle=XMLUtil.parseInputStream(PipeTransformationTest.class.getResourceAsStream("StudentInitiatedDrop_Style_pipeTest.xml"));
059: // Get Template Name
060: XPathFactory xPathFactory=XPathFactory.newInstance();
061: XPath xPath=xPathFactory.newXPath();
062: Node node=(Node)xPath.evaluate("//use[0]",edlStyle,XPathConstants.NODE);
063: //Node node=nodes.item(0);
064: if(node!=null){
065: if(node.hasAttributes()){
066: Node testAttri=node.getAttributes().getNamedItem("name");
067: if(testAttri!=null){
068: templateName=testAttri.getNodeValue();
069: }else{
070: //set default template as widgets
071: templateName="widgets";
072: }
073: }
074: }
075:
076: System.out.println(templateName);
077: */
078: //templateStyle=XMLUtil.parseInputStream(PipeTransformationTest.class.getResourceAsStream("widgets_pipeTest.xml"));
079: // Create a TransformerHandler for each stylesheet.
080: TransformerHandler tHandler1 = saxTFactory
081: .newTransformerHandler(new StreamSource(
082: PipeTransformationTest.class
083: .getResourceAsStream("StudentInitiatedDrop_Style_pipeTest.xml")));
084: // TransformerHandler tHandler2 = saxTFactory.newTransformerHandler(new StreamSource(PipeTransformationTest.class.getResourceAsStream("trans.xml")));
085: //TransformerHandler tHandler3 = saxTFactory.newTransformerHandler(new StreamSource("foo3.xsl"));
086:
087: // Create an XMLReader.
088: XMLReader reader = XMLReaderFactory.createXMLReader();
089: reader.setContentHandler(tHandler1);
090: reader
091: .setProperty(
092: "http://xml.org/sax/properties/lexical-handler",
093: tHandler1);
094:
095: //tHandler1.setResult(new SAXResult(tHandler2));
096: //tHandler2.setResult(new SAXResult(tHandler3));
097:
098: // transformer3 outputs SAX events to the serializer.
099: java.util.Properties xmlProps = OutputPropertiesFactory
100: .getDefaultMethodProperties("xml");
101: xmlProps.setProperty("indent", "yes");
102: xmlProps.setProperty("standalone", "no");
103: Serializer serializer = SerializerFactory
104: .getSerializer(xmlProps);
105: FileOutputStream transformedXML = new FileOutputStream(
106: "c://file.xml");
107: serializer.setOutputStream(transformedXML);
108: tHandler1.setResult(new SAXResult(serializer
109: .asContentHandler()));
110:
111: // Parse the XML input document. The input ContentHandler and output ContentHandler
112: // work in separate threads to optimize performance.
113: reader
114: .parse(new InputSource(
115: PipeTransformationTest.class
116: .getResourceAsStream("StudentInitiatedDrop.xml")));
117: }
118:
119: } catch (Exception ex) {
120: ex.printStackTrace();
121: }
122: }
123:
124: }
|