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.TemplateResource;
020 import com.liferay.portal.kernel.templateparser.TemplateContext;
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 Map<String, Object> getHelperUtilities() {
053 Map<String, Object> velocityContext = super.getHelperUtilities();
054
055
056
057 velocityContext.put("dateTool", new DateTool());
058
059
060
061 velocityContext.put("escapeTool", new EscapeTool());
062
063
064
065 velocityContext.put("iteratorTool", new IteratorTool());
066
067
068
069 velocityContext.put("listTool", new ListTool());
070
071
072
073 velocityContext.put("mathTool", new MathTool());
074
075
076
077 velocityContext.put("numberTool", new NumberTool());
078
079
080
081 velocityContext.put(
082 "velocityPortletPreferences", new TemplatePortletPreferences());
083
084
085
086 velocityContext.put("sortTool", new SortTool());
087
088
089
090 try {
091 velocityContext.put(
092 "rolePermission", RolePermissionUtil.getRolePermission());
093 }
094 catch (SecurityException se) {
095 _log.error(se, se);
096 }
097
098 return velocityContext;
099 }
100
101 @Override
102 public Set<String> getRestrictedVariables() {
103 return SetUtil.fromArray(
104 PropsValues.JOURNAL_TEMPLATE_VELOCITY_RESTRICTED_VARIABLES);
105 }
106
107 @Override
108 public void prepare(
109 TemplateContext templateContext, HttpServletRequest request) {
110
111 super.prepare(templateContext, request);
112
113
114
115 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
116 WebKeys.THEME_DISPLAY);
117
118 if (themeDisplay != null) {
119
120
121
122 templateContext.put(
123 "init",
124 themeDisplay.getPathContext() +
125 TemplateResource.SERVLET_SEPARATOR +
126 "/html/themes/_unstyled/templates/init.vm");
127 }
128
129
130
131 Theme theme = (Theme)request.getAttribute(WebKeys.THEME);
132
133 if ((theme == null) && (themeDisplay != null)) {
134 theme = themeDisplay.getTheme();
135 }
136
137 if (theme != null) {
138
139
140
141 String servletContextName = GetterUtil.getString(
142 theme.getServletContextName());
143
144 templateContext.put(
145 "fullCssPath",
146 servletContextName + theme.getVelocityResourceListener() +
147 theme.getCssPath());
148
149 templateContext.put(
150 "fullTemplatesPath",
151 servletContextName + theme.getVelocityResourceListener() +
152 theme.getTemplatesPath());
153 }
154
155
156
157 Map<String, Object> vmVariables =
158 (Map<String, Object>)request.getAttribute(WebKeys.VM_VARIABLES);
159
160 if (vmVariables != null) {
161 for (Map.Entry<String, Object> entry : vmVariables.entrySet()) {
162 String key = entry.getKey();
163 Object value = entry.getValue();
164
165 if (Validator.isNotNull(key)) {
166 templateContext.put(key, value);
167 }
168 }
169 }
170 }
171
172 private static Log _log = LogFactoryUtil.getLog(
173 VelocityTemplateContextHelper.class);
174
175 }