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;
}
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;
}
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);
}
}
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);
}
}
}
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: []"));
}
Aggregations