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.Templates;
23  import javax.xml.transform.Transformer;
24  import javax.xml.transform.TransformerException;
25  import javax.xml.transform.TransformerFactory;
26  import javax.xml.transform.sax.SAXSource;
27  import javax.xml.transform.sax.SAXTransformerFactory;
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.portletbridge.rewriter.DefaultUrlRewriter;
35  import org.xml.sax.InputSource;
36  
37  /***
38   * @author JMcCrindle
39   */
40  public class TransformerTest extends TestCase {
41  
42      /*
43       * @see TestCase#setUp()
44       */
45      protected void setUp() throws Exception {
46          super.setUp();
47      }
48      
49      public void testTransform() throws Exception {
50          try {
51              TransformerFactory tfactory = TransformerFactory.newInstance();
52  
53              Parser parser = new Parser();
54              parser.setFeature(Parser.namespacesFeature, false);
55  
56              // Does this factory support SAX features?
57              if (tfactory.getFeature(SAXSource.FEATURE))
58              {
59                  // If so, we can safely cast.
60                  SAXTransformerFactory stfactory = 
61                      ((SAXTransformerFactory) tfactory);
62            
63                  // A TransformerHandler is a ContentHandler that will listen for
64                  // SAX events, and transform them to the result.
65                  // Set up a hanlder for each style sheet. 
66  
67                  StreamSource streamSource = new StreamSource(new File("./src/xsl/org/portletbridge/xsl/linkrewriter.xsl"));
68                  Templates templates = stfactory.newTemplates(streamSource);
69  
70                  Transformer transformer = templates.newTransformer();
71                  
72                  transformer.setParameter("rewriter", new DefaultUrlRewriter("/test/test.do", "/mytest/"));
73  
74                  transformer.transform(new SAXSource(parser,
75                          new InputSource(new FileReader(new File(
76                          "./src/test/org/portletbridge/xsl/linkrewritertest.xml")))), new StreamResult(new OutputStreamWriter(System.out)));
77                  
78              }
79              else
80              {
81                  System.out.println(
82                      "Can't do exampleContentHandlerToContentHandler because tfactory is not a SAXTransformerFactory");
83              }
84  
85          } catch (TransformerException e) {
86              e.printStackTrace();
87          }
88      }
89      
90      public static void main(String[] args) throws Exception {
91          TransformerTest test = new TransformerTest("LinkRewriterTest");
92          test.setUp();
93          test.testTransform();
94          test.tearDown();
95      }
96  
97      /*
98       * @see TestCase#tearDown()
99       */
100     protected void tearDown() throws Exception {
101         super.tearDown();
102     }
103 
104     /***
105      * Constructor for XslTest.
106      * @param arg0
107      */
108     public TransformerTest(String arg0) {
109         super(arg0);
110     }
111 
112 }