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.io.StringWriter;
22  
23  import javax.xml.transform.Result;
24  import javax.xml.transform.Templates;
25  import javax.xml.transform.TransformerException;
26  import javax.xml.transform.TransformerFactory;
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.portletbridge.rewriter.DefaultUrlRewriter;
37  import org.xml.sax.InputSource;
38  
39  /***
40   * @author JMcCrindle
41   */
42  public class LinkRewriterXslTest extends TestCase {
43  
44      /*
45       * @see TestCase#setUp()
46       */
47      protected void setUp() throws Exception {
48          super.setUp();
49      }
50      
51      public void testTransform() throws Exception {
52          try {
53              TransformerFactory tfactory = TransformerFactory.newInstance();
54  
55              Parser parser = new Parser();
56              parser.setFeature(Parser.namespacesFeature, false);
57  
58              // Does this factory support SAX features?
59              if (tfactory.getFeature(SAXSource.FEATURE))
60              {
61                  // If so, we can safely cast.
62                  SAXTransformerFactory stfactory = 
63                      ((SAXTransformerFactory) tfactory);
64            
65                  // A TransformerHandler is a ContentHandler that will listen for
66                  // SAX events, and transform them to the result.
67                  // Set up a hanlder for each style sheet. 
68  
69                  StreamSource streamSource = new StreamSource(new File("./src/xsl/org/portletbridge/xsl/linkrewriter.xsl"));
70                  Templates templates = stfactory.newTemplates(streamSource);
71                  
72                  long startTime = System.currentTimeMillis();
73                  
74                  int max = 1;
75                  for(int i = 0; i < max; i++) {
76  
77                      TransformerHandler handler1 = 
78                          stfactory.newTransformerHandler(templates);
79  
80                      // Set the output of the stylesheets. The first stylesheet will
81                      // output to handle2, thus piping the result so the second
82                      // stylesheet can parse it. The second stylesheet outputs to the
83                      // terminal
84                      StringWriter stringWriter = new StringWriter();
85                      // Result result1 = new StreamResult(stringWriter);
86                      Result result1 = new StreamResult(new OutputStreamWriter(System.out));
87                      handler1.setResult(result1);
88  
89                      // Set up the parmeters. This paremeter will be passed to
90                      // stylesheet 1 (foo.xsl)
91                      handler1.getTransformer().setParameter("rewriter", new DefaultUrlRewriter("/test/test.do", "/mytest/"));
92  
93                      parser.setContentHandler(handler1);
94  
95                      // Parse the source XML, and send the parse events to the
96                      // TransformerHandler.
97                      parser.parse(new InputSource(
98                              new FileReader(new File(
99                              "./src/test/org/portletbridge/xsl/linkrewritertest.xml"))));
100 
101                 }
102                 
103                 long finishTime = System.currentTimeMillis();
104                 System.out.println("Total Time: " + (finishTime - startTime) + "ms");
105                 System.out.println("Average Time: " + ((finishTime - startTime) / max));
106             }
107             else
108             {
109                 System.out.println(
110                     "Can't do exampleContentHandlerToContentHandler because tfactory is not a SAXTransformerFactory");
111             }
112 
113         } catch (TransformerException e) {
114             e.printStackTrace();
115         }
116     }
117     
118     public static void main(String[] args) throws Exception {
119         LinkRewriterXslTest test = new LinkRewriterXslTest("LinkRewriterTest");
120         test.setUp();
121         test.testTransform();
122         test.tearDown();
123     }
124 
125     /*
126      * @see TestCase#tearDown()
127      */
128     protected void tearDown() throws Exception {
129         super.tearDown();
130     }
131 
132     /***
133      * Constructor for XslTest.
134      * @param arg0
135      */
136     public LinkRewriterXslTest(String arg0) {
137         super(arg0);
138     }
139 
140 }