1 5 6 package org.exoplatform.portlet.cocoon; 7 8 9 import javax.xml.transform.TransformerConfigurationException ; 10 import javax.xml.transform.TransformerException ; 11 import javax.xml.transform.stream.StreamResult ; 12 import javax.xml.transform.stream.StreamSource ; 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 org.exoplatform.services.xml.transform.trax.TRAXTransformer; 18 import org.exoplatform.services.xml.transform.trax.TRAXTemplates; 19 import org.exoplatform.services.xml.transform.trax.TRAXTransformerService; 20 import org.exoplatform.services.xml.transform.trax.Constants; 21 22 import java.io.InputStream ; 23 import java.io.OutputStream ; 24 import java.io.IOException ; 25 import org.exoplatform.services.xml.transform.NotSupportedIOTypeException; 26 import org.exoplatform.services.log.LogService; 27 import org.apache.commons.logging.Log; 28 29 39 40 public class URLRewriter { 41 public static final String PARAM_NAME_VALUE = "url"; 42 public static final String BASE_URI = "baseURI"; public static final String PARAM_NAME = "param-name"; public static final String LINK_PREFIX = "link-prefix"; 47 public static final String HTML_URL_REWRITE_STYLE = 48 Constants.XSLT_DIR + "/html-url-rewite.xsl"; 49 50 private static TRAXTemplates templates; 51 private Log log; 52 53 public URLRewriter() { 54 LogService logService = (LogService) PortalContainer.getInstance(). 55 getComponentInstanceOfType(LogService.class); 56 log = logService.getLog(this.getClass()); 57 } 58 59 60 protected TRAXTemplates getTemplates() throws 61 TransformerException , IOException { 62 if (templates == null) { 63 InputStream inputStream = 64 Thread.currentThread().getContextClassLoader(). 65 getResource(HTML_URL_REWRITE_STYLE).openStream(); 66 TRAXTransformerService traxService = 67 (TRAXTransformerService) PortalContainer.getInstance(). 68 getComponentInstanceOfType(TRAXTransformerService.class); 69 try { 70 templates = traxService.getTemplates( 71 new StreamSource (inputStream)); 72 } catch (NotSupportedIOTypeException ex) { 73 new IOException (ex.getMessage()); 74 } 75 } 76 return templates; 77 } 78 79 80 public void rewrite(InputStream input, OutputStream output, 81 String baseURI, String linkPrefix) throws 82 TransformerException , 83 TransformerConfigurationException , InstantiationException , 84 IOException { 85 86 90 HTMLTransformerService htmlService = 91 (HTMLTransformerService) PortalContainer.getInstance(). 92 getComponentInstanceOfType(HTMLTransformerService.class); 93 94 log.debug("URLRewriterNew.rewrite htmlService is null? - " + 95 (htmlService == null)); 96 97 HTMLTransformer htmlTransformer = htmlService.getTransformer(); 98 TRAXTransformer traxTransformer = getTemplates().newTransformer(); 99 100 traxTransformer.setParameter(BASE_URI, baseURI); 101 traxTransformer.setParameter(LINK_PREFIX, linkPrefix); 102 traxTransformer.setParameter(PARAM_NAME, PARAM_NAME_VALUE); 103 104 try { 105 htmlTransformer.initResult(traxTransformer.getTransformerAsResult()); 106 traxTransformer.initResult(new StreamResult (output)); 107 htmlTransformer.transform(new StreamSource (input)); 108 } catch (NotSupportedIOTypeException ex) { 109 new IOException (ex.getMessage()); 110 } 111 112 } 113 } 114 | Popular Tags |