Search in sources :

Example 6 with XWikiVelocityException

use of org.xwiki.velocity.XWikiVelocityException 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)

Example 7 with XWikiVelocityException

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

the class WikiUIExtensionParameters method get.

/**
 * @return the parameters, after their values have been evaluated by the XWiki Velocity Engine.
 */
public Map<String, String> get() {
    boolean isCacheValid = false;
    // Even though the parameters are dynamic, we cache a rendered version of them in order to improve performance.
    // This cache has a short lifespan, it gets discarded for each new request, or if the database has been switched
    // during a request.
    int currentContextId = this.execution.getContext().hashCode();
    String currentWiki = modelContext.getCurrentEntityReference().extractReference(EntityType.WIKI).getName();
    if (currentContextId == this.previousContextId && currentWiki.equals(previousWiki) && this.evaluatedParameters != null) {
        isCacheValid = true;
    }
    if (!isCacheValid) {
        this.evaluatedParameters = new HashMap<String, String>();
        if (this.parameters.size() > 0) {
            try {
                VelocityEngine velocityEngine = this.velocityManager.getVelocityEngine();
                VelocityContext velocityContext = this.velocityManager.getVelocityContext();
                for (Map.Entry<String, String> entry : this.parameters.entrySet()) {
                    StringWriter writer = new StringWriter();
                    try {
                        String namespace = this.id + ':' + entry.getKey();
                        velocityEngine.evaluate(new VelocityContext(velocityContext), writer, namespace, entry.getValue());
                        this.evaluatedParameters.put(entry.getKey(), writer.toString());
                    } catch (XWikiVelocityException e) {
                        LOGGER.warn(String.format("Failed to evaluate UI extension data value, key [%s], value [%s]. Reason: [%s]", entry.getKey(), entry.getValue(), e.getMessage()));
                    }
                }
            } catch (XWikiVelocityException ex) {
                LOGGER.warn(String.format("Failed to get velocity engine. Reason: [%s]", ex.getMessage()));
            }
            this.previousContextId = currentContextId;
            this.previousWiki = currentWiki;
        }
    }
    return this.evaluatedParameters;
}
Also used : VelocityEngine(org.xwiki.velocity.VelocityEngine) XWikiVelocityException(org.xwiki.velocity.XWikiVelocityException) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with XWikiVelocityException

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

the class StubVelocityManager method initialize.

@Override
public void initialize() throws InitializationException {
    try {
        // Configure the Velocity Engine not to use the Resource Webapp Loader since we don't
        // need it and we would need to setup the Container component's ApplicationContext
        // otherwise.
        Properties properties = new Properties();
        properties.setProperty("resource.loader", "file");
        this.velocityEngine.initialize(properties);
    } catch (XWikiVelocityException e) {
        throw new InitializationException("Failed to initialize Velocity Engine", e);
    }
}
Also used : XWikiVelocityException(org.xwiki.velocity.XWikiVelocityException) Properties(java.util.Properties) InitializationException(org.xwiki.component.phase.InitializationException)

Example 9 with XWikiVelocityException

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

the class VelocityRenderer method render.

/**
 * Render a velocity code without messing with the document context and namespace.
 * @param code code to render
 * @return the rendered code
 * @throws IconException if problem occurs
 */
public String render(String code) throws IconException {
    // The macro namespace to use by the velocity engine, see afterwards.
    String namespace = "IconVelocityRenderer_" + Thread.currentThread().getId();
    // Create the output writer
    StringWriter output = new StringWriter();
    VelocityEngine engine = null;
    try {
        // Get the velocity engine
        engine = velocityManager.getVelocityEngine();
        // Use a new macro namespace to prevent the code redefining existing macro.
        // We use the thread name to have a unique id.
        engine.startedUsingMacroNamespace(namespace);
        // Create a new VelocityContext to prevent the code creating variables in the current context.
        // See https://jira.xwiki.org/browse/XWIKI-11400.
        // We set the current context as inner context of the new one to be able to read existing variables.
        // See https://jira.xwiki.org/browse/XWIKI-11426.
        VelocityContext context = new VelocityContext(velocityManager.getVelocityContext());
        // Render the code
        if (engine.evaluate(context, output, "DefaultIconRenderer", code)) {
            return output.toString();
        } else {
            // I don't know how to check the velocity runtime log
            throw new IconException("Failed to render the icon. See the Velocity runtime log.", null);
        }
    } catch (XWikiVelocityException e) {
        throw new IconException("Failed to render the icon.", e);
    } finally {
        // Do not forget to close the macro namespace we have created previously
        if (engine != null) {
            engine.stoppedUsingMacroNamespace(namespace);
        }
    }
}
Also used : VelocityEngine(org.xwiki.velocity.VelocityEngine) XWikiVelocityException(org.xwiki.velocity.XWikiVelocityException) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) IconException(org.xwiki.icon.IconException)

Example 10 with XWikiVelocityException

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

the class WikiUIExtensionParametersTest method getParametersWhenVelocityFails.

@Test
public void getParametersWhenVelocityFails() throws Exception {
    when(modelContext.getCurrentEntityReference()).thenReturn(new WikiReference("xwiki"));
    when(velocityEngine.evaluate(any(VelocityContext.class), any(StringWriter.class), eq("id:key"), eq("value"))).thenThrow(new XWikiVelocityException(""));
    WikiUIExtensionParameters parameters = new WikiUIExtensionParameters("id", "key=value", componentManager);
    // It should fail and put a warn in the logs
    Assert.assertEquals(null, parameters.get().get("key"));
    Assert.assertTrue(logRule.contains("Failed to evaluate UI extension data value, key [key], value [value]. Reason: []"));
}
Also used : XWikiVelocityException(org.xwiki.velocity.XWikiVelocityException) StringWriter(java.io.StringWriter) WikiUIExtensionParameters(org.xwiki.uiextension.internal.WikiUIExtensionParameters) VelocityContext(org.apache.velocity.VelocityContext) WikiReference(org.xwiki.model.reference.WikiReference) Test(org.junit.Test)

Aggregations

XWikiVelocityException (org.xwiki.velocity.XWikiVelocityException)10 VelocityContext (org.apache.velocity.VelocityContext)8 StringWriter (java.io.StringWriter)7 VelocityEngine (org.xwiki.velocity.VelocityEngine)4 Properties (java.util.Properties)2 Test (org.junit.Test)2 InitializationException (org.xwiki.component.phase.InitializationException)2 IconException (org.xwiki.icon.IconException)2 VelocityManager (org.xwiki.velocity.VelocityManager)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 StringReader (java.io.StringReader)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 LESSCompiler (org.xwiki.lesscss.compiler.LESSCompiler)1 LESSCompilerException (org.xwiki.lesscss.compiler.LESSCompilerException)1 LESSResourceReference (org.xwiki.lesscss.resources.LESSResourceReference)1 LESSResourceReferenceFactory (org.xwiki.lesscss.resources.LESSResourceReferenceFactory)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 ObjectPropertyReference (org.xwiki.model.reference.ObjectPropertyReference)1