1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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.portletbridge.xsl;
17  
18  import java.io.File;
19  import java.io.FileReader;
20  import java.io.OutputStreamWriter;
21  import java.util.Date;
22  
23  import javax.xml.transform.Result;
24  import javax.xml.transform.TransformerException;
25  import javax.xml.transform.TransformerFactory;
26  import javax.xml.transform.sax.SAXResult;
27  import javax.xml.transform.sax.SAXSource;
28  import javax.xml.transform.sax.SAXTransformerFactory;
29  import javax.xml.transform.sax.TransformerHandler;
30  import javax.xml.transform.stream.StreamResult;
31  import javax.xml.transform.stream.StreamSource;
32  
33  import junit.framework.TestCase;
34  
35  import org.ccil.cowan.tagsoup.Parser;
36  import org.xml.sax.InputSource;
37  
38  /***
39   * @author JMcCrindle
40   */
41  public class XslTest extends TestCase {
42  
43      /*
44       * @see TestCase#setUp()
45       */
46      protected void setUp() throws Exception {
47          super.setUp();
48      }
49      
50      public void testTransform() throws Exception {
51          try {
52              Parser parser = new Parser();
53              parser.setFeature(Parser.namespacesFeature, false);
54  
55              TransformerFactory tfactory = TransformerFactory.newInstance();
56  
57              // Does this factory support SAX features?
58              if (tfactory.getFeature(SAXSource.FEATURE))
59              {
60                  // If so, we can safely cast.
61                  SAXTransformerFactory stfactory = 
62                      ((SAXTransformerFactory) tfactory);
63            
64                  // A TransformerHandler is a ContentHandler that will listen for
65                  // SAX events, and transform them to the result.
66                  // Set up a hanlder for each style sheet. 
67  
68                  TransformerHandler handler1 = 
69                      stfactory.newTransformerHandler(new StreamSource(new File("./src/test/org/portletbridge/xsl/test.xsl")));
70                  TransformerHandler handler2 = 
71                      stfactory.newTransformerHandler(new StreamSource(new File("./src/test/org/portletbridge/xsl/testnext.xsl")));
72  
73                  // Set up the parmeters. This paremeter will be passed to
74                  // stylesheet 1 (foo.xsl)
75                  handler1.getTransformer().setParameter("test", new Date());
76                  //parameters for stylesheet 2
77                  handler2.getTransformer().setParameter("test", new Date());
78            
79                  // Set the output of the stylesheets. The first stylesheet will
80                  // output to handle2, thus piping the result so the second
81                  // stylesheet can parse it. The second stylesheet outputs to the
82                  // terminal
83                  Result result1 = new SAXResult(handler2);
84                  Result result2 = new StreamResult(new OutputStreamWriter(System.out));
85                  handler1.setResult(result1);
86                  handler2.setResult(result2);
87            
88                  parser.setContentHandler(handler1);
89  
90                  // Parse the source XML, and send the parse events to the
91                  // TransformerHandler.
92                  parser.parse(new InputSource(
93                          new FileReader(new File(
94                          "./src/test/org/portletbridge/xsl/test.xml"))));
95              }
96              else
97              {
98                  System.out.println(
99                      "Can't do exampleContentHandlerToContentHandler because tfactory is not a SAXTransformerFactory");
100             }
101 
102         } catch (TransformerException e) {
103             e.printStackTrace();
104         }
105     }
106 
107     /*
108      * @see TestCase#tearDown()
109      */
110     protected void tearDown() throws Exception {
111         super.tearDown();
112     }
113 
114     /***
115      * Constructor for XslTest.
116      * @param arg0
117      */
118     public XslTest(String arg0) {
119         super(arg0);
120     }
121 
122 }