/portal-impl/src/com/liferay/portal/freemarker/FreeMarkerTemplateContextHelper.java

http://github.com/liferay/liferay-portal · Java · 131 lines · 73 code · 33 blank · 25 comment · 6 complexity · 23d727859e941f9148b78cbbb89fa250 MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.portal.freemarker;
  15. import com.liferay.portal.kernel.template.TemplateResource;
  16. import com.liferay.portal.kernel.templateparser.TemplateContext;
  17. import com.liferay.portal.kernel.util.GetterUtil;
  18. import com.liferay.portal.kernel.util.SetUtil;
  19. import com.liferay.portal.kernel.util.StringPool;
  20. import com.liferay.portal.kernel.util.Validator;
  21. import com.liferay.portal.model.Theme;
  22. import com.liferay.portal.template.TemplateContextHelper;
  23. import com.liferay.portal.template.TemplatePortletPreferences;
  24. import com.liferay.portal.theme.ThemeDisplay;
  25. import com.liferay.portal.util.PropsValues;
  26. import com.liferay.portal.util.WebKeys;
  27. import freemarker.ext.beans.BeansWrapper;
  28. import java.util.Map;
  29. import java.util.Set;
  30. import javax.servlet.http.HttpServletRequest;
  31. /**
  32. * @author Mika Koivisto
  33. * @author Raymond Aug?Š
  34. */
  35. public class FreeMarkerTemplateContextHelper extends TemplateContextHelper {
  36. @Override
  37. public Map<String, Object> getHelperUtilities() {
  38. Map<String, Object> helperUtilities = super.getHelperUtilities();
  39. // Enum util
  40. helperUtilities.put(
  41. "enumUtil", BeansWrapper.getDefaultInstance().getEnumModels());
  42. // Object util
  43. helperUtilities.put("objectUtil", new LiferayObjectConstructor());
  44. // Portlet preferences
  45. helperUtilities.put(
  46. "freeMarkerPortletPreferences", new TemplatePortletPreferences());
  47. // Static class util
  48. helperUtilities.put(
  49. "staticUtil", BeansWrapper.getDefaultInstance().getStaticModels());
  50. return helperUtilities;
  51. }
  52. @Override
  53. public Set<String> getRestrictedVariables() {
  54. return SetUtil.fromArray(
  55. PropsValues.JOURNAL_TEMPLATE_FREEMARKER_RESTRICTED_VARIABLES);
  56. }
  57. @Override
  58. public void prepare(
  59. TemplateContext templateContext, HttpServletRequest request) {
  60. super.prepare(templateContext, request);
  61. // Theme display
  62. ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
  63. WebKeys.THEME_DISPLAY);
  64. if (themeDisplay != null) {
  65. Theme theme = themeDisplay.getTheme();
  66. // Full css and templates path
  67. String servletContextName = GetterUtil.getString(
  68. theme.getServletContextName());
  69. templateContext.put(
  70. "fullCssPath",
  71. StringPool.SLASH + servletContextName +
  72. theme.getFreeMarkerTemplateLoader() + theme.getCssPath());
  73. templateContext.put(
  74. "fullTemplatesPath",
  75. StringPool.SLASH + servletContextName +
  76. theme.getFreeMarkerTemplateLoader() +
  77. theme.getTemplatesPath());
  78. // Init
  79. templateContext.put(
  80. "init",
  81. StringPool.SLASH + themeDisplay.getPathContext() +
  82. TemplateResource.SERVLET_SEPARATOR +
  83. "/html/themes/_unstyled/templates/init.ftl");
  84. }
  85. // Insert custom ftl variables
  86. Map<String, Object> ftlVariables =
  87. (Map<String, Object>)request.getAttribute(WebKeys.FTL_VARIABLES);
  88. if (ftlVariables != null) {
  89. for (Map.Entry<String, Object> entry : ftlVariables.entrySet()) {
  90. String key = entry.getKey();
  91. Object value = entry.getValue();
  92. if (Validator.isNotNull(key)) {
  93. templateContext.put(key, value);
  94. }
  95. }
  96. }
  97. }
  98. }