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.trax.Constants; 16 17 import java.io.FileInputStream ; 18 import java.io.FileOutputStream ; 19 import java.io.ByteArrayOutputStream ; 20 import java.io.OutputStream ; 21 import java.io.InputStream ; 22 23 import org.apache.commons.logging.Log; 24 25 28 public class TestXsl extends BaseTest { 29 private TRAXTransformerService traxService; 30 private Log log; 31 32 public void setUp() throws Exception { 33 log = getLog(); 34 traxService = 35 (TRAXTransformerService) PortalContainer.getInstance(). 36 getComponentInstanceOfType(TRAXTransformerService.class); 37 assertNotNull("traxService", traxService); 38 40 41 } 42 43 44 public void testSimpleXslt() throws Exception { 45 46 47 String OUTPUT_FILENAME = "tmp/rss-out_"+getTimeStamp()+"_xsl.xml"; 48 49 FileInputStream inputFileInputStream = 50 new FileInputStream ("tmp/rss-out.xhtml"); 51 assertTrue("Empty input file",inputFileInputStream.available() > 0); 52 53 OutputStream outputFileOutputStream = 55 new FileOutputStream (OUTPUT_FILENAME); 56 57 58 String XSL_URL = Constants.XSLT_DIR+"/html-url-rewite.xsl"; 60 InputStream xslInputStream = Thread.currentThread(). 61 getContextClassLoader().getResourceAsStream(XSL_URL); 62 assertNotNull("empty xsl",xslInputStream); 63 Source xslSource = new StreamSource (xslInputStream); 64 assertNotNull("get xsl source",xslSource); 65 66 TRAXTransformer traxTransformer = traxService.getTransformer(xslSource); 68 assertNotNull("get transformer",traxTransformer); 69 70 traxTransformer.initResult(new StreamResult (outputFileOutputStream)); 71 traxTransformer.transform(new StreamSource (inputFileInputStream)); 72 73 inputFileInputStream.close(); 74 outputFileOutputStream.close(); 75 76 FileInputStream outputFileInputStream = 78 new FileInputStream (OUTPUT_FILENAME); 79 80 assertTrue("Output is empty", outputFileInputStream.available() > 0); 81 outputFileInputStream.close(); 82 83 84 } 85 86 public void testXsltUseTemplates() throws Exception { 87 FileInputStream inputFileInputStream = 89 new FileInputStream ("tmp/rss-out.xhtml"); 90 assertTrue("Empty input file",inputFileInputStream.available() > 0); 91 92 ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream (); 94 95 String XSL_URL = Constants.XSLT_DIR+"/html-url-rewite.xsl"; 97 InputStream xslInputStream = Thread.currentThread(). 98 getContextClassLoader().getResourceAsStream(XSL_URL); 99 assertNotNull("empty xsl",xslInputStream); 100 Source xslSource = new StreamSource (xslInputStream); 101 assertNotNull("get xsl source",xslSource); 102 103 TRAXTemplates traxTemplates = traxService.getTemplates(xslSource); 105 assertNotNull("get templates",traxTemplates); 106 107 TRAXTransformer traxTransformer = traxTemplates.newTransformer(); 109 assertNotNull("get transformer",traxTransformer); 110 111 traxTransformer.initResult(new StreamResult (byteOutputStream)); 113 traxTransformer.transform(new StreamSource (inputFileInputStream)); 114 inputFileInputStream.close(); 115 116 assertTrue("Output is empty", byteOutputStream.size() > 0); 117 118 120 TRAXTransformer traxOtherTransformer = traxTemplates.newTransformer(); 121 assertNotNull("get Other transformer",traxOtherTransformer); 122 123 FileInputStream inputOtherFileInputStream = 124 new FileInputStream ("tmp/rss-out.xhtml"); 125 assertTrue("Empty input other file", 126 inputOtherFileInputStream.available() > 0); 127 128 ByteArrayOutputStream byteOtherOutputStream = new ByteArrayOutputStream (); 129 130 traxOtherTransformer.initResult(new StreamResult (byteOtherOutputStream)); 131 traxOtherTransformer.transform(new StreamSource (inputOtherFileInputStream)); 132 inputOtherFileInputStream.close(); 133 assertTrue("Output other is empty", byteOutputStream.size() > 0); 134 135 } 136 137 138 139 140 } 141 | Popular Tags |