Search in sources :

Example 1 with VelocityMacroFilter

use of org.xwiki.rendering.macro.velocity.filter.VelocityMacroFilter in project xwiki-platform by xwiki.

the class VelocityMacro method getFilter.

/**
 * @param parameters the velocity macros parameters
 * @return the velocity content filter
 * @since 2.0M1
 */
private VelocityMacroFilter getFilter(VelocityMacroParameters parameters) {
    String filterName = parameters.getFilter();
    if (StringUtils.isEmpty(filterName)) {
        filterName = this.configuration.getFilter();
        if (StringUtils.isEmpty(filterName)) {
            filterName = null;
        }
    }
    VelocityMacroFilter filter = null;
    if (filterName != null) {
        try {
            filter = getComponentManager().getInstance(VelocityMacroFilter.class, filterName);
        } catch (ComponentLookupException e) {
            this.logger.error("Can't find velocity macro filter", e);
        }
    }
    return filter;
}
Also used : VelocityMacroFilter(org.xwiki.rendering.macro.velocity.filter.VelocityMacroFilter) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Example 2 with VelocityMacroFilter

use of org.xwiki.rendering.macro.velocity.filter.VelocityMacroFilter in project xwiki-platform by xwiki.

the class VelocityMacro method evaluateString.

@Override
protected String evaluateString(VelocityMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
    String result = "";
    try {
        VelocityContext velocityContext = this.velocityManager.getCurrentVelocityContext();
        VelocityMacroFilter filter = getFilter(parameters);
        String cleanedContent = content;
        // Execute pre filter
        if (filter != null) {
            cleanedContent = filter.before(cleanedContent, velocityContext);
        }
        StringWriter writer = new StringWriter();
        // Use the Transformation id as the name passed to the Velocity Engine. This name is used internally
        // by Velocity as a cache index key for caching macros.
        String key = context.getTransformationContext().getId();
        if (key == null) {
            key = "unknown namespace";
        }
        // Execute Velocity context
        this.velocityManager.evaluate(writer, key, new StringReader(cleanedContent));
        result = writer.toString();
        // Execute post filter
        if (filter != null) {
            result = filter.after(result, velocityContext);
        }
    } catch (XWikiVelocityException e) {
        throw new MacroExecutionException("Failed to evaluate Velocity Macro for content [" + content + "]", e);
    }
    return result;
}
Also used : VelocityMacroFilter(org.xwiki.rendering.macro.velocity.filter.VelocityMacroFilter) XWikiVelocityException(org.xwiki.velocity.XWikiVelocityException) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) StringReader(java.io.StringReader) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException)

Aggregations

VelocityMacroFilter (org.xwiki.rendering.macro.velocity.filter.VelocityMacroFilter)2 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 VelocityContext (org.apache.velocity.VelocityContext)1 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)1 MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)1 XWikiVelocityException (org.xwiki.velocity.XWikiVelocityException)1