01: package com.opensymphony.sitemesh.compatability;
02:
03: import com.opensymphony.module.sitemesh.Factory;
04: import com.opensymphony.module.sitemesh.HTMLPage;
05: import com.opensymphony.module.sitemesh.Page;
06: import com.opensymphony.module.sitemesh.PageParser;
07: import com.opensymphony.module.sitemesh.filter.HttpContentType;
08: import com.opensymphony.sitemesh.Content;
09: import com.opensymphony.sitemesh.SiteMeshContext;
10: import com.opensymphony.sitemesh.webapp.SiteMeshWebAppContext;
11: import com.opensymphony.sitemesh.ContentProcessor;
12:
13: import javax.servlet.http.HttpServletRequest;
14: import java.io.IOException;
15:
16: /**
17: * Adapts a SiteMesh 2 {@link PageParser} to a SiteMesh 3 {@link ContentProcessor}.
18: *
19: * @author Joe Walnes
20: * @since SiteMesh 3
21: */
22: public class PageParser2ContentProcessor implements ContentProcessor {
23:
24: private final Factory factory;
25:
26: public PageParser2ContentProcessor(Factory factory) {
27: this .factory = factory;
28: }
29:
30: public boolean handles(SiteMeshContext context) {
31: SiteMeshWebAppContext webAppContext = (SiteMeshWebAppContext) context;
32: return !factory.isPathExcluded(extractRequestPath(webAppContext
33: .getRequest()));
34: }
35:
36: private String extractRequestPath(HttpServletRequest request) {
37: String servletPath = request.getServletPath();
38: String pathInfo = request.getPathInfo();
39: String query = request.getQueryString();
40: return (servletPath == null ? "" : servletPath)
41: + (pathInfo == null ? "" : pathInfo)
42: + (query == null ? "" : ("?" + query));
43: }
44:
45: public boolean handles(String contentType) {
46: return factory.shouldParsePage(contentType);
47: }
48:
49: public Content build(char[] data, SiteMeshContext context)
50: throws IOException {
51: HttpContentType httpContentType = new HttpContentType(context
52: .getContentType());
53: PageParser pageParser = factory.getPageParser(httpContentType
54: .getType());
55: Page page = pageParser.parse(data);
56: return new HTMLPage2Content((HTMLPage) page);
57: }
58: }
|