Search in sources :

Example 21 with VelocityManager

use of org.xwiki.velocity.VelocityManager in project xwiki-platform by xwiki.

the class MailSenderPlugin method evaluate.

private String evaluate(String content, String name, VelocityContext vcontext, XWikiContext context) {
    StringWriter writer = new StringWriter();
    try {
        VelocityManager velocityManager = Utils.getComponent(VelocityManager.class);
        velocityManager.getVelocityEngine().evaluate(vcontext, writer, name, content);
        return writer.toString();
    } catch (Exception e) {
        LOGGER.error("Error while parsing velocity template namespace [{}]", name, e);
        Object[] args = { name };
        XWikiException xe = new XWikiException(XWikiException.MODULE_XWIKI_RENDERING, XWikiException.ERROR_XWIKI_RENDERING_VELOCITY_EXCEPTION, "Error while parsing velocity page {0}", e, args);
        return Util.getHTMLExceptionMessage(xe, context);
    }
}
Also used : StringWriter(java.io.StringWriter) VelocityManager(org.xwiki.velocity.VelocityManager) XWikiException(com.xpn.xwiki.XWikiException) MessagingException(javax.mail.MessagingException) SendFailedException(javax.mail.SendFailedException) AddressException(javax.mail.internet.AddressException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) XWikiException(com.xpn.xwiki.XWikiException)

Example 22 with VelocityManager

use of org.xwiki.velocity.VelocityManager in project xwiki-platform by xwiki.

the class DefaultOldRendering method parseContent.

@Override
public String parseContent(String content, XWikiContext xcontext) {
    try {
        if (StringUtils.isNotEmpty(content)) {
            VelocityManager velocityManager = this.velocityManagerProvider.get();
            VelocityContext velocityContext = velocityManager.getVelocityContext();
            VelocityEngine velocityEngine = velocityManager.getVelocityEngine();
            StringWriter writer = new StringWriter();
            velocityEngine.evaluate(velocityContext, writer, xcontext.getDoc().getPrefixedFullName(), content);
            return writer.toString();
        }
    } catch (XWikiVelocityException e) {
        this.logger.error("Faield to parse content [" + content + "]", e);
    }
    return "";
}
Also used : VelocityEngine(org.xwiki.velocity.VelocityEngine) XWikiVelocityException(org.xwiki.velocity.XWikiVelocityException) StringWriter(java.io.StringWriter) VelocityManager(org.xwiki.velocity.VelocityManager) VelocityContext(org.apache.velocity.VelocityContext)

Example 23 with VelocityManager

use of org.xwiki.velocity.VelocityManager in project xwiki-platform by xwiki.

the class IncludeMacroTest method runIncludeMacroWithPreVelocity.

private List<Block> runIncludeMacroWithPreVelocity(Context context, String velocity, String includedContent) throws Exception {
    VelocityManager velocityManager = getComponentManager().getInstance(VelocityManager.class);
    StringWriter writer = new StringWriter();
    velocityManager.getVelocityEngine().evaluate(velocityManager.getVelocityContext(), writer, "wiki:Space.IncludingPage", velocity);
    return runIncludeMacro(context, includedContent);
}
Also used : StringWriter(java.io.StringWriter) VelocityManager(org.xwiki.velocity.VelocityManager)

Example 24 with VelocityManager

use of org.xwiki.velocity.VelocityManager in project xwiki-platform by xwiki.

the class CacheMacroTest method executeWhenNoIdAndSameContent.

@Test
public void executeWhenNoIdAndSameContent() throws Exception {
    String expected = "beginDocument\n" + "beginMacroMarkerStandalone [velocity] [] [$var]\n" + "beginParagraph\n" + "onWord [content]\n" + "endParagraph\n" + "endMacroMarkerStandalone [velocity] [] [$var]\n" + "endDocument";
    CacheMacroParameters params = new CacheMacroParameters();
    MacroTransformationContext context = createMacroTransformationContext();
    VelocityManager velocityManager = getComponentManager().getInstance(VelocityManager.class);
    StringWriter writer = new StringWriter();
    velocityManager.getVelocityEngine().evaluate(velocityManager.getVelocityContext(), writer, "template", "#set ($var = 'content')");
    List<Block> result = this.cacheMacro.execute(params, "{{velocity}}$var{{/velocity}}", context);
    assertBlocks(expected, result, this.rendererFactory);
    // Execute a second time with a different value for the velocity $var variable to ensure the returned result
    // is the cached one.
    velocityManager.getVelocityEngine().evaluate(velocityManager.getVelocityContext(), writer, "template", "#set ($var = 'newcontent')");
    result = this.cacheMacro.execute(params, "{{velocity}}$var{{/velocity}}", context);
    assertBlocks(expected, result, this.rendererFactory);
}
Also used : CacheMacroParameters(org.xwiki.rendering.macro.cache.CacheMacroParameters) StringWriter(java.io.StringWriter) VelocityManager(org.xwiki.velocity.VelocityManager) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) Block(org.xwiki.rendering.block.Block) Test(org.junit.Test)

Example 25 with VelocityManager

use of org.xwiki.velocity.VelocityManager in project xwiki-platform by xwiki.

the class CacheMacroTest method executeWhenSameIdAndDifferentContent.

@Test
public void executeWhenSameIdAndDifferentContent() throws Exception {
    String expected = "beginDocument\n" + "beginMacroMarkerStandalone [velocity] [] [$var]\n" + "beginParagraph\n" + "onWord [content]\n" + "endParagraph\n" + "endMacroMarkerStandalone [velocity] [] [$var]\n" + "endDocument";
    CacheMacroParameters params = new CacheMacroParameters();
    params.setId("uniqueid");
    MacroTransformationContext context = createMacroTransformationContext();
    VelocityManager velocityManager = getComponentManager().getInstance(VelocityManager.class);
    StringWriter writer = new StringWriter();
    velocityManager.getVelocityEngine().evaluate(velocityManager.getVelocityContext(), writer, "template", "#set ($var = 'content')");
    List<Block> result = this.cacheMacro.execute(params, "{{velocity}}$var{{/velocity}}", context);
    assertBlocks(expected, result, this.rendererFactory);
    // Execute a second time with a different content but with the same id, to ensure the returned result
    // is the cached one.
    result = this.cacheMacro.execute(params, "whatever here...", context);
    assertBlocks(expected, result, this.rendererFactory);
}
Also used : CacheMacroParameters(org.xwiki.rendering.macro.cache.CacheMacroParameters) StringWriter(java.io.StringWriter) VelocityManager(org.xwiki.velocity.VelocityManager) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) Block(org.xwiki.rendering.block.Block) Test(org.junit.Test)

Aggregations

VelocityManager (org.xwiki.velocity.VelocityManager)31 VelocityContext (org.apache.velocity.VelocityContext)19 Test (org.junit.Test)15 VelocityEngine (org.xwiki.velocity.VelocityEngine)15 StringWriter (java.io.StringWriter)12 DocumentReference (org.xwiki.model.reference.DocumentReference)8 Reader (java.io.Reader)6 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)5 XWikiException (com.xpn.xwiki.XWikiException)4 BaseObject (com.xpn.xwiki.objects.BaseObject)4 IOException (java.io.IOException)4 Writer (java.io.Writer)4 Map (java.util.Map)4 Properties (java.util.Properties)4 Execution (org.xwiki.context.Execution)4 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)4 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Expectations (org.jmock.Expectations)3 Invocation (org.jmock.api.Invocation)3