View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.cms.core.html;
17  
18  import java.util.regex.Matcher;
19  import java.util.regex.Pattern;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.seasar.tuigwaa.cms.core.CmsConstants;
23  import org.seasar.tuigwaa.cms.core.CmsEngine;
24  import org.seasar.tuigwaa.cms.core.CmsRequest;
25  import org.seasar.tuigwaa.cms.core.CmsResponse;
26  import org.seasar.tuigwaa.cms.core.wiki.base.WikiConfiguration;
27  import org.seasar.tuigwaa.plugin.AbstractPlugin;
28  import org.seasar.tuigwaa.plugin.Plugin;
29  import org.seasar.tuigwaa.plugin.PluginException;
30  import org.seasar.tuigwaa.plugin.PluginRequest;
31  
32  public class HtmlEngine implements CmsEngine {
33  
34  	private WikiConfiguration config;
35  
36  	public void setConfiguration(WikiConfiguration cfg) {
37  		this.config = cfg;
38  	}
39  
40  	private static final String[] FORBIDDEN_LETTER = { ";", "//[", "//]", "#",
41  			"////", "<", ">", "//n", "//r" };
42  
43  	private static final String ARGS = "[^"
44  			+ StringUtils.join(FORBIDDEN_LETTER, "") + "]+";
45  
46  	private Pattern inlinePattern = Pattern.compile("&amp;([a-z]*)(//((" + ARGS
47  			+ ")//))?;");
48  
49  	private Pattern blockPattern = Pattern.compile(">#([a-z]*)(//((" + ARGS
50  			+ ")//))?<");
51  
52  	public HtmlEngine() {
53  	}
54  
55  	public void service(CmsRequest request, CmsResponse response) {
56  		switch (request.getType()) {
57  		case CmsConstants.OUTPUTTYPE_HTML:
58  			doHtml(request, response);
59  			break;
60  		case CmsConstants.OUTPUTTYPE_PDF:
61  			doPdf(request, response);
62  			break;
63  		default:
64  			break;
65  		}
66  	}
67  
68  	private void doPdf(CmsRequest request, CmsResponse response) {
69  		doHtml(request, response);
70  		String htmlString = response.getString();
71  		PdfConverter converter = new PdfConverter(request, response, config);
72  		response.setBytes(converter.convertHtmlToPdf(htmlString));
73  	}
74  
75  	private void doHtml(CmsRequest request, CmsResponse response) {
76  		String content = (String) request.getPage().getContent();
77  		String[] lines = content.split(CmsConstants.LINEBREAK_CODE);
78  		String[] newLines = new String[lines.length];
79  
80  		for (int i = 0; i < lines.length; i++) {
81  			newLines[i] = parseLine(request, response, lines[i]);
82  		}
83  
84  		String html = StringUtils.join(newLines, CmsConstants.LINEBREAK_CODE);
85  		response.setString(html);
86  	}
87  
88  	private String parseLine(CmsRequest request, CmsResponse response,
89  			String line) {
90  
91  		Matcher matcher = blockPattern.matcher(line);
92  
93  		if (matcher.find()) { // Block Plugin
94  			String pluginName = matcher.group(1);
95  			String args = matcher.group(3);
96  			StringBuffer buf = new StringBuffer();
97  			servicePlugin(buf, matcher, request, response, pluginName, args,
98  					AbstractPlugin.BLOCKPLUGIN_MODE);
99  			matcher.appendTail(buf);
100 			line = buf.toString();
101 		} else {
102 			matcher = inlinePattern.matcher(line);
103 
104 			StringBuffer buf = null;
105 			while (matcher.find()) {
106 				if (buf == null) {
107 					buf = new StringBuffer();
108 				}
109 				String pluginName = matcher.group(1);
110 				String args = matcher.group(3);
111 				servicePlugin(buf, matcher, request, response, pluginName,
112 						args, AbstractPlugin.INLINEPLUGIN_MODE);
113 			}
114 			if (buf != null) {
115 				matcher.appendTail(buf);
116 				line = buf.toString();
117 			}
118 		}
119 		return line;
120 	}
121 
122 	private void servicePlugin(StringBuffer buf, Matcher matcher,
123 			CmsRequest request, CmsResponse response, String pluginName,
124 			String args, int mode) {
125 		
126 		try {
127 			
128 			
129 			Plugin plugin = config.getPlugin(pluginName);
130 			if(args == null){
131 				args = "";
132 			}
133 			PluginRequest prequest = new PluginRequest(pluginName, args.split(","));
134 			String ret = null;	ret = (String) plugin.service(request, response, prequest);
135 
136 			if (ret != null) {
137 				ret = ret.replaceAll("//$", "//////$");
138 			} else {
139 				ret = "";
140 			}
141 			if (mode == AbstractPlugin.BLOCKPLUGIN_MODE) {
142 				ret = ">" + ret + "<";
143 			}
144 			matcher.appendReplacement(buf, ret);
145 		} catch (PluginException e) {
146 			System.out.println(e.toString());
147 		} catch (Exception e){
148 			e.printStackTrace();
149 		}
150 	}
151 }