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.Source ; 10 11 import org.exoplatform.container.PortalContainer; 12 import org.exoplatform.services.xml.transform.trax.TRAXTransformer; 13 import org.exoplatform.services.xml.transform.trax.TRAXTransformerService; 14 import org.exoplatform.services.xml.transform.trax.TRAXTemplates; 15 import org.exoplatform.services.xml.transform.html.HTMLTransformerService; 16 import org.exoplatform.services.xml.transform.html.HTMLTransformer; 17 import org.exoplatform.services.xml.transform.trax.Constants; 18 19 import java.io.FileInputStream ; 20 import java.io.FileOutputStream ; 21 import java.io.OutputStream ; 22 import java.io.InputStream ; 23 import org.apache.commons.logging.Log; 24 25 28 public class TestPipe extends BaseTest { 29 30 private HTMLTransformer htmlTransformer; 31 private TRAXTemplates traxTemplates; 32 private TRAXTransformerService traxService; 33 private Log log; 34 35 public void setUp() throws Exception { 36 log = getLog(); 37 38 HTMLTransformerService htmlService = 40 (HTMLTransformerService) PortalContainer.getInstance(). 41 getComponentInstanceOfType(HTMLTransformerService.class); 42 assertNotNull("htmlService", htmlService); 43 htmlTransformer = htmlService.getTransformer(); 44 assertNotNull("get html transformer", htmlTransformer); 45 46 traxService = (TRAXTransformerService) PortalContainer.getInstance(). 48 getComponentInstanceOfType(TRAXTransformerService.class); 49 50 String XSL_URL = Constants.XSLT_DIR + "/html-url-rewite.xsl"; 52 InputStream xslInputStream = Thread.currentThread(). 53 getContextClassLoader().getResourceAsStream( 54 XSL_URL); 55 assertNotNull("empty xsl", xslInputStream); 56 Source xslSource = new StreamSource (xslInputStream); 57 assertNotNull("get xsl source", xslSource); 58 traxTemplates = traxService.getTemplates(xslSource); 60 assertNotNull("get trax Templates", traxTemplates); 61 } 62 63 public void testTidyAndXsl() throws Exception { 64 FileInputStream inputFileInputStream = 66 new FileInputStream ("tmp/rss-out.html"); 67 assertTrue("Empty input file",inputFileInputStream.available() > 0); 68 69 70 String OUTPUT_FILENAME = "tmp/rss-out_"+getTimeStamp()+"_tydy_xsl.xml"; 72 OutputStream outputFileOutputStream = 73 new FileOutputStream (OUTPUT_FILENAME); 74 TRAXTransformer traxTransformer = traxTemplates.newTransformer(); 75 76 htmlTransformer.initResult( 78 traxTransformer.getTransformerAsResult()); 79 traxTransformer.initResult( 80 new StreamResult (outputFileOutputStream)); 81 htmlTransformer.transform(new StreamSource (inputFileInputStream)); 82 83 inputFileInputStream.close(); 84 85 FileInputStream outputFileInputStream = 87 new FileInputStream (OUTPUT_FILENAME); 88 89 assertTrue("Output is empty", outputFileInputStream.available() > 0); 90 outputFileInputStream.close(); 91 } 92 93 public void testXslAndXsl() throws Exception { 94 FileInputStream inputFileInputStream = 96 new FileInputStream ("tmp/rss-out.xhtml"); 97 assertTrue("Empty input file",inputFileInputStream.available() > 0); 98 99 String OUTPUT_FILENAME = "tmp/rss-out_"+getTimeStamp()+"_xsl_xsl.xml"; 101 OutputStream outputFileOutputStream = 102 new FileOutputStream (OUTPUT_FILENAME); 103 104 TRAXTransformer traxTransformer1 = traxTemplates.newTransformer(); 105 TRAXTransformer traxTransformer2 = traxTemplates.newTransformer(); 106 107 assertNotNull("pipe supported ", 108 traxTransformer2.getTransformerAsResult()); 109 110 traxTransformer1.initResult( 112 traxTransformer2.getTransformerAsResult()); 113 114 traxTransformer2.initResult( 115 new StreamResult (outputFileOutputStream)); 116 117 traxTransformer1.transform(new StreamSource (inputFileInputStream)); 118 119 inputFileInputStream.close(); 120 outputFileOutputStream.flush(); 121 outputFileOutputStream.close(); 122 123 FileInputStream outputFileInputStream = 125 new FileInputStream (OUTPUT_FILENAME); 126 127 assertTrue("Output is empty", outputFileInputStream.available() > 0); 128 validateXML(outputFileInputStream); 130 outputFileInputStream.close(); 131 132 } 133 134 public void testTidyAndXslWithEmptySource() throws Exception { 135 136 137 java.io.ByteArrayOutputStream output = 138 new java.io.ByteArrayOutputStream (); 139 InputStream input = new java.io.ByteArrayInputStream (new byte[]{}); 140 141 TRAXTransformer traxTransformer = traxTemplates.newTransformer(); 142 143 htmlTransformer.initResult( 145 traxTransformer.getTransformerAsResult()); 146 traxTransformer.initResult( 147 new StreamResult (output)); 148 htmlTransformer.transform(new StreamSource (input)); 149 150 assertTrue("Output is not empty", output.toByteArray().length == 0); 151 } 152 153 154 155 } 156 | Popular Tags |