001
014
015 package com.liferay.portal.velocity;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.template.Template;
020 import com.liferay.portal.kernel.template.TemplateConstants;
021 import com.liferay.portal.kernel.util.GetterUtil;
022 import com.liferay.portal.kernel.util.SetUtil;
023 import com.liferay.portal.kernel.util.Validator;
024 import com.liferay.portal.model.Theme;
025 import com.liferay.portal.service.permission.RolePermissionUtil;
026 import com.liferay.portal.template.TemplateContextHelper;
027 import com.liferay.portal.template.TemplatePortletPreferences;
028 import com.liferay.portal.theme.ThemeDisplay;
029 import com.liferay.portal.util.PropsValues;
030 import com.liferay.portal.util.WebKeys;
031
032 import java.util.Map;
033 import java.util.Set;
034
035 import javax.servlet.http.HttpServletRequest;
036
037 import org.apache.velocity.tools.generic.DateTool;
038 import org.apache.velocity.tools.generic.EscapeTool;
039 import org.apache.velocity.tools.generic.IteratorTool;
040 import org.apache.velocity.tools.generic.ListTool;
041 import org.apache.velocity.tools.generic.MathTool;
042 import org.apache.velocity.tools.generic.NumberTool;
043 import org.apache.velocity.tools.generic.SortTool;
044
045
049 public class VelocityTemplateContextHelper extends TemplateContextHelper {
050
051 @Override
052 public Set<String> getRestrictedVariables() {
053 return SetUtil.fromArray(
054 PropsValues.VELOCITY_ENGINE_RESTRICTED_VARIABLES);
055 }
056
057 @Override
058 public void prepare(Template template, HttpServletRequest request) {
059 super.prepare(template, request);
060
061
062
063 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
064 WebKeys.THEME_DISPLAY);
065
066 if (themeDisplay != null) {
067
068
069
070 template.put(
071 "init",
072 themeDisplay.getPathContext() +
073 TemplateConstants.SERVLET_SEPARATOR +
074 "/html/themes/_unstyled/templates/init.vm");
075 }
076
077
078
079 Theme theme = (Theme)request.getAttribute(WebKeys.THEME);
080
081 if ((theme == null) && (themeDisplay != null)) {
082 theme = themeDisplay.getTheme();
083 }
084
085 if (theme != null) {
086
087
088
089 String servletContextName = GetterUtil.getString(
090 theme.getServletContextName());
091
092 template.put(
093 "fullCssPath",
094 servletContextName + theme.getVelocityResourceListener() +
095 theme.getCssPath());
096
097 template.put(
098 "fullTemplatesPath",
099 servletContextName + theme.getVelocityResourceListener() +
100 theme.getTemplatesPath());
101 }
102
103
104
105 Map<String, Object> vmVariables =
106 (Map<String, Object>)request.getAttribute(WebKeys.VM_VARIABLES);
107
108 if (vmVariables != null) {
109 for (Map.Entry<String, Object> entry : vmVariables.entrySet()) {
110 String key = entry.getKey();
111 Object value = entry.getValue();
112
113 if (Validator.isNotNull(key)) {
114 template.put(key, value);
115 }
116 }
117 }
118 }
119
120 @Override
121 protected void populateExtraHelperUtilities(
122 Map<String, Object> velocityContext) {
123
124
125
126 velocityContext.put("dateTool", new DateTool());
127
128
129
130 velocityContext.put("escapeTool", new EscapeTool());
131
132
133
134 velocityContext.put("iteratorTool", new IteratorTool());
135
136
137
138 velocityContext.put("listTool", new ListTool());
139
140
141
142 velocityContext.put("mathTool", new MathTool());
143
144
145
146 velocityContext.put("numberTool", new NumberTool());
147
148
149
150 velocityContext.put(
151 "velocityPortletPreferences", new TemplatePortletPreferences());
152
153
154
155 velocityContext.put("sortTool", new SortTool());
156
157
158
159 try {
160 velocityContext.put(
161 "rolePermission", RolePermissionUtil.getRolePermission());
162 }
163 catch (SecurityException se) {
164 _log.error(se, se);
165 }
166 }
167
168 private static Log _log = LogFactoryUtil.getLog(
169 VelocityTemplateContextHelper.class);
170
171 }