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 junit.framework.TestCase;
23  
24  import org.ccil.cowan.tagsoup.XMLWriter;
25  import org.portletbridge.rewriter.FullUrlRewriter;
26  import org.xml.sax.InputSource;
27  import org.xml.sax.XMLReader;
28  import org.xml.sax.helpers.XMLReaderFactory;
29  
30  /***
31   * @author JMcCrindle
32   */
33  public class LinkRewriterXmlFilterTest extends TestCase {
34  
35      /*
36       * @see TestCase#setUp()
37       */
38      protected void setUp() throws Exception {
39          super.setUp();
40      }
41      
42      public void testTransform() throws Exception {
43  //        XMLReader parser = XMLReaderFactory
44  //            .createXMLReader("org.ccil.cowan.tagsoup.Parser");
45          XMLReader parser =
46                 XMLReaderFactory.createXMLReader("org.cyberneko.html.parsers.SAXParser");
47          parser.setFeature("http://cyberneko.org/html/features/scanner/notify-builtin-refs", false);
48          //parser.setFeature("http://xml.org/sax/features/namespaces", false);
49  
50        
51              // A TransformerHandler is a ContentHandler that will listen for
52              // SAX events, and transform them to the result.
53              // Set up a hanlder for each style sheet. 
54  
55              long startTime = System.currentTimeMillis();
56              
57              int max = 1;
58              for(int i = 0; i < max; i++) {
59  
60                  //XMLWriter writer = new XMLWriter(new StringWriter());
61                  XMLWriter writer = new XMLWriter(new OutputStreamWriter(System.out));
62  //                ContentHandler x;
63  //                SerializerToHTML writer = new SerializerToHTML();
64  //                writer.setWriter(new OutputStreamWriter(System.out));
65  
66                  LinkRewriterXmlFilter filter = new LinkRewriterXmlFilter();
67                  filter.setParent(parser);
68                  filter.setContentHandler(writer);
69                  filter.setUrlRewriter(new FullUrlRewriter("http://localhost:80/test/test.do", "/mytest/http://localhost:80/"));
70  
71                  // parser.setContentHandler(handler1);
72  
73                  // Parse the source XML, and send the parse events to the
74                  // TransformerHandler.
75                  filter.parse(new InputSource(
76                          new FileReader(new File(
77                          "./src/test/org/portletbridge/xsl/linkrewritertest.xml"))));
78  
79              }
80              
81              long finishTime = System.currentTimeMillis();
82              System.out.println("Total Time: " + (finishTime - startTime) + "ms");
83              System.out.println("Average Time: " + ((finishTime - startTime) / max));
84      }
85      
86      public static void main(String[] args) throws Exception {
87          LinkRewriterXmlFilterTest test = new LinkRewriterXmlFilterTest("LinkRewriterTest");
88          test.setUp();
89          test.testTransform();
90          test.tearDown();
91      }
92  
93      /*
94       * @see TestCase#tearDown()
95       */
96      protected void tearDown() throws Exception {
97          super.tearDown();
98      }
99  
100     /***
101      * Constructor for XslTest.
102      * @param arg0
103      */
104     public LinkRewriterXmlFilterTest(String arg0) {
105         super(arg0);
106     }
107 
108 }