1 5 package org.exoplatform.services.xml.transform; 6 7 import javax.xml.transform.stream.StreamResult ; 8 import javax.xml.transform.stream.StreamSource ; 9 import javax.xml.transform.sax.TransformerHandler ; 10 import javax.xml.transform.sax.SAXTransformerFactory ; 11 import javax.xml.transform.sax.SAXResult ; 12 13 import org.exoplatform.container.PortalContainer; 14 15 import org.exoplatform.services.xml.transform.html.HTMLTransformer; 16 import org.exoplatform.services.xml.transform.html.HTMLTransformerService; 17 import java.io.FileInputStream ; 18 import java.io.FileOutputStream ; 19 import java.io.OutputStream ; 20 21 import java.util.Properties ; 22 23 import org.exoplatform.services.log.LogService; 24 import org.exoplatform.container.RootContainer; 25 import org.apache.commons.logging.Log; 26 27 28 31 public class TestTidy extends BaseTest { 32 private HTMLTransformer htmlTransformer; 33 34 private Log log; 35 36 public void setUp() throws Exception { 37 log = getLog(); 38 HTMLTransformerService htmlService = 39 (HTMLTransformerService) PortalContainer.getInstance(). 40 getComponentInstanceOfType(HTMLTransformerService.class); 41 assertNotNull("htmlService", htmlService); 42 htmlTransformer = htmlService.getTransformer(); 43 44 } 45 46 47 public void testTidy() throws Exception { 48 try { 49 String OUTPUT_FILENAME = "tmp/rss-out_"+getTimeStamp()+"_xhtml.xhtml"; 50 51 FileInputStream inputFileInputStream = 52 new FileInputStream ("tmp/rss-out.html"); 53 54 OutputStream outputFileOutputStream = 56 new FileOutputStream (OUTPUT_FILENAME); 57 58 htmlTransformer.initResult(new StreamResult (outputFileOutputStream)); 59 htmlTransformer.transform(new StreamSource (inputFileInputStream)); 60 61 outputFileOutputStream.close(); 62 63 FileInputStream outputFileInputStream = 65 new FileInputStream (OUTPUT_FILENAME); 66 67 assertTrue("Output is empty", outputFileInputStream.available() > 0); 68 69 validateXML(outputFileInputStream); 71 72 } catch (Exception e) { 73 fail("testTidy() ERROR: " + e.toString()); 74 } 75 } 76 77 public void testSAXResultType() throws Exception { 78 79 FileInputStream inputFileInputStream = 80 new FileInputStream ("tmp/rss-out.html"); 81 assertTrue("Empty input file",inputFileInputStream.available() > 0); 82 83 TransformerHandler transformHandler = ((SAXTransformerFactory ) SAXTransformerFactory.newInstance()). 86 newTransformerHandler(); 87 88 String OUTPUT_FILENAME = "tmp/rss-out_"+getTimeStamp()+"_html2sax.xhtml"; 89 OutputStream output = new FileOutputStream (OUTPUT_FILENAME); 90 91 transformHandler.setResult(new StreamResult (output)); 92 93 SAXResult saxResult = new SAXResult (transformHandler); 94 htmlTransformer.initResult(saxResult); 95 htmlTransformer.transform(new StreamSource (inputFileInputStream)); 96 97 98 output.flush(); 99 output.close(); 100 FileInputStream outputFileInputStream = 102 new FileInputStream (OUTPUT_FILENAME); 103 assertTrue("Output is empty", outputFileInputStream.available() > 0); 104 validateXML(outputFileInputStream); 106 } 107 108 109 public void testProps() throws Exception { 110 try { 111 Properties props = htmlTransformer.getOutputProperties(); 112 113 assertEquals(props.getProperty("quiet"), "true"); 114 props.setProperty("quiet", "false"); 115 116 htmlTransformer.setOutputProperties(props); 117 assertEquals(htmlTransformer.getOutputProperties().getProperty( 118 "quiet"), "false"); 119 120 } catch (Exception e) { 121 fail("testProps() ERROR: " + e.toString()); 122 } 123 124 } 125 126 127 } 128 | Popular Tags |