Example usage for com.liferay.portal.kernel.util StringUtil replaceWithStringBundler

List of usage examples for com.liferay.portal.kernel.util StringUtil replaceWithStringBundler

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.util StringUtil replaceWithStringBundler.

Prototype

public static StringBundler replaceWithStringBundler(String s, String begin, String end,
        Map<String, StringBundler> values) 

Source Link

Document

Replaces all occurrences of the keywords found in the substring, defined by the beginning and ending strings, with the new values.

Usage

From source file:com.liferay.portlet.layoutconfiguration.util.RuntimePortletImpl.java

License:Open Source License

public void processTemplate(ServletContext servletContext, HttpServletRequest request,
        HttpServletResponse response, PageContext pageContext, JspWriter jspWriter, String portletId,
        String velocityTemplateId, String velocityTemplateContent) throws Exception {

    if (Validator.isNull(velocityTemplateContent)) {
        return;// www .  j  ava2 s  .  co  m
    }

    TemplateProcessor processor = new TemplateProcessor(servletContext, request, response, portletId);

    VelocityContext velocityContext = VelocityEngineUtil.getWrappedStandardToolsContext();

    velocityContext.put("processor", processor);

    // Velocity variables

    VelocityVariablesUtil.insertVariables(velocityContext, request);

    // liferay:include tag library

    UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();

    MethodHandler methodHandler = new MethodHandler(_initMethodKey, servletContext, request,
            new PipingServletResponse(response, unsyncStringWriter), pageContext);

    Object velocityTaglib = methodHandler.invoke(true);

    velocityContext.put("taglibLiferay", velocityTaglib);
    velocityContext.put("theme", velocityTaglib);

    try {
        VelocityEngineUtil.mergeTemplate(velocityTemplateId, velocityTemplateContent, velocityContext,
                unsyncStringWriter);
    } catch (Exception e) {
        _log.error(e, e);

        throw e;
    }

    String output = unsyncStringWriter.toString();

    Map<Portlet, Object[]> portletsMap = processor.getPortletsMap();

    Map<String, StringBundler> contentsMap = new HashMap<String, StringBundler>(portletsMap.size());

    for (Map.Entry<Portlet, Object[]> entry : portletsMap.entrySet()) {
        Portlet portlet = entry.getKey();
        Object[] value = entry.getValue();

        String queryString = (String) value[0];
        String columnId = (String) value[1];
        Integer columnPos = (Integer) value[2];
        Integer columnCount = (Integer) value[3];

        UnsyncStringWriter portletUnsyncStringWriter = new UnsyncStringWriter();

        PipingServletResponse pipingServletResponse = new PipingServletResponse(response,
                portletUnsyncStringWriter);

        processPortlet(servletContext, request, pipingServletResponse, portlet, queryString, columnId,
                columnPos, columnCount, null, true);

        contentsMap.put(portlet.getPortletId(), portletUnsyncStringWriter.getStringBundler());
    }

    StringBundler sb = StringUtil.replaceWithStringBundler(output, "[$TEMPLATE_PORTLET_", "$]", contentsMap);

    sb.writeTo(jspWriter);
}