KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portlet > cocoon > URLRewriter


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5
6 package org.exoplatform.portlet.cocoon;
7
8
9 import javax.xml.transform.TransformerConfigurationException JavaDoc;
10 import javax.xml.transform.TransformerException JavaDoc;
11 import javax.xml.transform.stream.StreamResult JavaDoc;
12 import javax.xml.transform.stream.StreamSource JavaDoc;
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 JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import org.exoplatform.services.xml.transform.NotSupportedIOTypeException;
26 import org.exoplatform.services.log.LogService;
27 import org.apache.commons.logging.Log;
28
29 /**
30  * Created by The eXo Platform SARL .
31  * <p/>
32  * Url rewriter for XHTML script
33  * HREF="original-url" will be rewrited as
34  * HREF="<portlet-url>?<href-param>=<original-url>"
35  *
36  * @author <a HREF="mailto:geaz@users.sourceforge.net">Gennady Azarenkov</a>
37  * @version $Id: URLRewriter.java 596 2005-01-28 15:45:37Z kravchuk $
38  */

39
40 public class URLRewriter {
41     public static final String JavaDoc PARAM_NAME_VALUE = "url";
42     //xsl param names
43
public static final String JavaDoc BASE_URI = "baseURI"; //PREFIX_URL = "portlet-url";
44
public static final String JavaDoc PARAM_NAME = "param-name"; //HREF_PARAM = //"href-param";
45
public static final String JavaDoc LINK_PREFIX = "link-prefix"; //HREF_CONTEXT = "href-context";
46

47     public static final String JavaDoc 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 JavaDoc, IOException JavaDoc {
62         if (templates == null) {
63             InputStream JavaDoc 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 JavaDoc(inputStream));
72             } catch (NotSupportedIOTypeException ex) {
73                 new IOException JavaDoc(ex.getMessage());
74             }
75         }
76         return templates;
77     }
78
79
80     public void rewrite(InputStream JavaDoc input, OutputStream JavaDoc output,
81                         String JavaDoc baseURI, String JavaDoc linkPrefix) throws
82             TransformerException JavaDoc,
83             TransformerConfigurationException JavaDoc, InstantiationException JavaDoc,
84             IOException JavaDoc {
85
86 // System.out.println("Content to transform : " + new String(buffer));
87
// log.debug("linkPrefix=["+currentPageURI+"]");
88
// log.debug("urlPrefix=["+urlPrefix+"]");
89

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 JavaDoc(output));
107             htmlTransformer.transform(new StreamSource JavaDoc(input));
108         } catch (NotSupportedIOTypeException ex) {
109             new IOException JavaDoc(ex.getMessage());
110         }
111
112     }
113 }
114
Popular Tags