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