1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.tuigwaa.plugin.basic;
17
18 import java.io.IOException;
19 import java.io.StringWriter;
20 import java.util.Arrays;
21 import java.util.HashMap;
22 import java.util.Map;
23 import java.util.Properties;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.velocity.Template;
28 import org.apache.velocity.VelocityContext;
29 import org.apache.velocity.app.Velocity;
30 import org.apache.velocity.exception.MethodInvocationException;
31 import org.apache.velocity.exception.ParseErrorException;
32 import org.apache.velocity.exception.ResourceNotFoundException;
33 import org.apache.velocity.runtime.resource.loader.FileResourceLoader;
34 import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
35 import org.seasar.tuigwaa.cms.core.CmsRequest;
36 import org.seasar.tuigwaa.cms.core.CmsResponse;
37 import org.seasar.tuigwaa.plugin.AbstractTgwPlugin;
38 import org.seasar.tuigwaa.plugin.PluginException;
39 import org.seasar.tuigwaa.plugin.PluginRequest;
40 import org.seasar.tuigwaa.plugin.PluginService;
41 import org.seasar.tuigwaa.util.TgwContext;
42 import org.seasar.tuigwaa.view.DataViewUtils;
43
44 import com.isenshi.util.CalendarUtils;
45 import com.isenshi.util.CharUtil;
46 import com.isenshi.util.ResourceUtils;
47
48 public class VelocityPlugin extends AbstractTgwPlugin {
49
50 private Log log = LogFactory.getLog(getClass());
51
52 private static final String ENCODING = "Windows-31J";
53
54 private static final String DIR_VELOCITYFILE = "org/seasar/tuigwaa/plugin/velocity/";
55
56 private PluginService pluginService = (PluginService) getService(PluginService.class);
57
58 private Map templateMap = new HashMap();
59
60 private Map pathMap = new HashMap();
61
62 public VelocityPlugin() {
63 Properties props = new Properties();
64 props.put("resource.loader", "CLASSPATH");
65 props.put("CLASSPATH.resource.loader.class", FileResourceLoader.class
66 .getName());
67 props.put("CLASSPATH.resource.loader.path", ResourceUtils.getPath(""));
68 try {
69
70 Velocity.init(props);
71 } catch (IOException e) {
72
73 log.warn("veloctiy plugin load execpetion" + e.getMessage());
74 } catch (Exception e) {
75
76 log.warn("velocity not initialized" + e.getMessage());
77 }
78 }
79
80 public String doHTMLView(CmsRequest request, CmsResponse response,
81 PluginRequest prequest) throws PluginException {
82 return doHTMLViewBindingObject(request, response, prequest, null);
83 }
84
85 public String doHTMLViewBindingObject(CmsRequest request,
86 CmsResponse response, PluginRequest prequest, Object outputDto)
87 throws PluginException {
88
89 String pluginName = prequest.getName();
90 return doHTMLViewBindingObject(request, response, prequest, outputDto,
91 getPath(pluginName));
92 }
93
94 public String doHTMLViewBindingObject(CmsRequest request,
95 CmsResponse response, PluginRequest prequest, Object outputDto,
96 String path) throws PluginException {
97 VelocityContext context = createContext(request, response, prequest,
98 outputDto);
99 Template template = getTemplate(path);
100 return merge(template, context);
101 }
102
103 public void putPath(String pluginName, Class clazz) {
104 String path = clazz.getName().replaceAll("//.", "///");
105 pathMap.put(pluginName, path + ".vm");
106 }
107
108 private String getPath(String pluginName) {
109 String path = (String) pathMap.get(pluginName);
110 if (path == null) {
111 return DIR_VELOCITYFILE + pluginName + ".vm";
112 }
113 return path;
114 }
115
116 private VelocityContext createContext(CmsRequest req, CmsResponse res,
117 PluginRequest prequest, Object obj) {
118 VelocityContext context = new VelocityContext();
119 if (prequest.getArgs() != null) {
120 context.put("args", Arrays.asList(prequest.getArgs()));
121 }
122
123
124 String message = pluginService.getMessage(getClass());
125 context.put("message", message);
126 context.put("actionPath", getActionPath());
127 context.put("hidden", getHiddenElements(req));
128
129
130 context.put("pagePath", req.getPage().getResource().getPath());
131 context.put("encodedPagePath", CharUtil.urlEncode(req.getPage()
132 .getResource().getPath()));
133 context.put("mainPagePath", req.getMainPagePath());
134 context.put("encodedMainPagePath", CharUtil.urlEncode(req
135 .getMainPagePath()));
136 context.put("contextPath", TgwContext.getContextPath());
137 context.put("siteName", req.getSiteName());
138 context.put("bean", obj);
139 context.put("request", SingletonS2ContainerFactory.getContainer()
140 .getRequest());
141
142 context.put("charutil", new CharUtil());
143 context.put("dataviewutils", new DataViewUtils());
144 context.put("calendarutils", new CalendarUtils());
145
146 return context;
147 }
148
149 protected String merge(String path, CmsRequest req, CmsResponse res,
150 PluginRequest preq, Object dto) {
151 Template template = getTemplate(path);
152 VelocityContext context = createContext(req, res, preq, dto);
153 return merge(template, context);
154 }
155
156 protected String merge(Template template, VelocityContext context) {
157 if (template == null) {
158 return "";
159 }
160 StringWriter writer = new StringWriter();
161 try {
162 template.merge(context, writer);
163 } catch (ResourceNotFoundException e) {
164 e.printStackTrace();
165 } catch (ParseErrorException e) {
166 e.printStackTrace();
167 } catch (MethodInvocationException e) {
168 e.printStackTrace();
169 } catch (Exception e) {
170 e.printStackTrace();
171 }
172 String ret = writer.toString();
173 writer.flush();
174 return ret;
175 }
176
177 private Template getTemplate(String path) {
178 Template template = (Template) templateMap.get(path);
179 if (template == null) {
180 try {
181 template = Velocity.getTemplate(path, ENCODING);
182 } catch (ResourceNotFoundException e) {
183 e.printStackTrace();
184 } catch (ParseErrorException e) {
185 e.printStackTrace();
186 } catch (Exception e) {
187 e.printStackTrace();
188 }
189 }
190 return template;
191 }
192 }