1   package org.portletbridge.portlet;
2   
3   import java.io.StringReader;
4   import java.io.StringWriter;
5   
6   import javax.xml.transform.Transformer;
7   import javax.xml.transform.TransformerException;
8   import javax.xml.transform.TransformerFactory;
9   import javax.xml.transform.sax.SAXSource;
10  import javax.xml.transform.stream.StreamResult;
11  import javax.xml.transform.stream.StreamSource;
12  
13  import junit.framework.TestCase;
14  
15  import org.cyberneko.html.parsers.SAXParser;
16  import org.xml.sax.InputSource;
17  import org.xml.sax.SAXNotRecognizedException;
18  import org.xml.sax.SAXNotSupportedException;
19  
20  public class XslTest extends TestCase {
21      public void testXsl() throws TransformerException, SAXNotRecognizedException, SAXNotSupportedException {
22          TransformerFactory tfactory = TransformerFactory.newInstance();
23          Transformer transformer = tfactory.newTransformer(new StreamSource(new StringReader("" +
24                  "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">" +
25                  "<xsl:output method=\"html\"" + 
26                  "    encoding=\"UTF-8\"" + 
27                  "    indent=\"yes\"" + 
28                  "    standalone=\"no\"" + 
29                  "    omit-xml-declaration=\"yes\"/>" + 
30                  "   <xsl:template match=\"//TITLE\">" +
31                  "   <a href=\"http://www.slashdot.org\"><xsl:value-of select=\".\"/></a>" +
32                  "   </xsl:template>" +
33                  "</xsl:stylesheet>")));
34          StringWriter writer = new StringWriter();
35          SAXParser parser = new SAXParser();
36          //parser.setFeature("http://cyberneko.org/html/features/override-namespaces", true);
37          //parser.setFeature("http://cyberneko.org/html/features/insert-namespaces", true);  
38          transformer.transform(new SAXSource(parser, new InputSource(
39                  new StringReader("" + 
40                  // "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" +
41                  // "\r\n<HTML xmlns=\"http://www.w3.org/1999/xhtml\">" +
42                  "<HTML>" + 
43                  "<HEAD><TITLE>TEST</TITLE></HEAD>" +
44                  "<BODY>" +
45                  "<FORM>" +
46                  "<INPUT type=\"image\" src=\"blah\"/>" +
47                  "<INPUT type=\"text\" src=\"blah\"/>" +
48                  "Blah" + 
49                  "</FORM>" +
50                  "</BODY>" +
51                  "</HTML>"))), new StreamResult(writer));
52          writer.flush();
53          System.out.println(writer.getBuffer().toString());
54          
55      }
56  }