use of org.xwiki.rendering.internal.transformation.MutableRenderingContext in project xwiki-platform by xwiki.
the class DefaultWikiComponentMethodExecutor method execute.
@Override
public Object execute(Method method, Object[] args, DocumentReference componentDocumentReference, XDOM xdom, Syntax syntax, Map<String, Object> methodContext) throws WikiComponentRuntimeException {
// Prepare and put the method context in the XWiki Context
Map<Object, Object> xwikiContext = (Map<Object, Object>) execution.getContext().getProperty("xwikicontext");
this.prepareMethodContext(methodContext, args);
xwikiContext.put("method", methodContext);
// Save current context document, to put it back after the execution.
Object contextDoc = xwikiContext.get(XWIKI_CONTEXT_DOC_KEY);
try {
// component document and not the context one.
try {
xwikiContext.put(XWIKI_CONTEXT_DOC_KEY, dab.getDocumentInstance(componentDocumentReference));
} catch (Exception e) {
throw new WikiComponentRuntimeException(String.format("Failed to load wiki component document [%s]", componentDocumentReference), e);
}
// We need to clone the xdom to avoid transforming the original and make it useless after the first
// transformation
XDOM transformedXDOM = xdom.clone();
// Perform internal macro transformations
try {
TransformationContext transformationContext = new TransformationContext(transformedXDOM, syntax);
transformationContext.setId(method.getClass().getName() + "#" + method.getName());
((MutableRenderingContext) renderingContext).transformInContext(macroTransformation, transformationContext, transformedXDOM);
} catch (TransformationException e) {
throw new WikiComponentRuntimeException(String.format("Error while executing wiki component macro transformation for method [%s]", method.getName()), e);
}
if (!method.getReturnType().getName().equals("void")) {
if (methodContext.get(OUTPUT_KEY) != null && ((WikiMethodOutputHandler) methodContext.get(OUTPUT_KEY)).getValue() != null) {
return method.getReturnType().cast(((WikiMethodOutputHandler) methodContext.get(OUTPUT_KEY)).getValue());
} else {
return this.castRenderedContent(transformedXDOM, method);
}
} else {
return null;
}
} finally {
if (contextDoc != null) {
xwikiContext.put(XWIKI_CONTEXT_DOC_KEY, contextDoc);
}
}
}
use of org.xwiki.rendering.internal.transformation.MutableRenderingContext in project xwiki-platform by xwiki.
the class EmailTemplateRenderer method executeTemplate.
/**
* Execute a template.
*
* @param event composite event to render
* @param userId id of the user who will receive the email
* @param template the template to use
* @param syntax syntax of the template and of the output
* @return the rendered template
* @throws NotificationException if something wrong happens
*/
public Block executeTemplate(CompositeEvent event, String userId, Template template, Syntax syntax) throws NotificationException {
XWikiContext context = contextProvider.get();
XWikiURLFactory originalURLFactory = context.getURLFactory();
ScriptContext scriptContext = scriptContextManager.getScriptContext();
try {
// Bind the event to some variable in the velocity context
scriptContext.setAttribute(EVENT_BINDING_NAME, event, ScriptContext.ENGINE_SCOPE);
scriptContext.setAttribute(USER_BINDING_NAME, userId, ScriptContext.ENGINE_SCOPE);
// Use the external URL factory to generate full URLs
context.setURLFactory(new ExternalServletURLFactory(context));
// Set the given syntax in the rendering context
if (renderingContext instanceof MutableRenderingContext) {
((MutableRenderingContext) renderingContext).push(null, null, syntax, null, false, syntax);
}
// Render the template or fallback to the default one
return templateManager.execute(template);
} catch (Exception e) {
throw new NotificationException("Failed to render the notification.", e);
} finally {
// Cleaning the rendering context
if (renderingContext instanceof MutableRenderingContext) {
((MutableRenderingContext) renderingContext).pop();
}
// Cleaning the URL factory
context.setURLFactory(originalURLFactory);
// Cleaning the velocity context
scriptContext.removeAttribute(EVENT_BINDING_NAME, ScriptContext.ENGINE_SCOPE);
scriptContext.removeAttribute(USER_BINDING_NAME, ScriptContext.ENGINE_SCOPE);
}
}
use of org.xwiki.rendering.internal.transformation.MutableRenderingContext in project xwiki-platform by xwiki.
the class XWikiAction method renderInit.
private void renderInit(XWikiContext xcontext) throws Exception {
RenderingContext renderingContext = Utils.getComponent(RenderingContext.class);
MutableRenderingContext mutableRenderingContext = renderingContext instanceof MutableRenderingContext ? (MutableRenderingContext) renderingContext : null;
if (mutableRenderingContext != null) {
mutableRenderingContext.push(renderingContext.getTransformation(), renderingContext.getXDOM(), renderingContext.getDefaultSyntax(), "init.vm", renderingContext.isRestricted(), Syntax.XHTML_1_0);
}
xcontext.getResponse().setStatus(503);
xcontext.getResponse().setContentType("text/html; charset=UTF-8");
try {
Utils.getComponent(TemplateManager.class).render("init.vm", xcontext.getResponse().getWriter());
} finally {
if (mutableRenderingContext != null) {
mutableRenderingContext.pop();
}
}
xcontext.getResponse().flushBuffer();
xcontext.setFinished(true);
}
use of org.xwiki.rendering.internal.transformation.MutableRenderingContext in project xwiki-platform by xwiki.
the class InternalTemplateManager method evaluateContent.
private void evaluateContent(Template template, TemplateContent content, Writer writer) throws Exception {
// 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 namespace = this.renderingContext.getTransformationId();
boolean renderingContextPushed = false;
if (namespace == null) {
namespace = template.getId() != null ? template.getId() : "unknown namespace";
if (this.renderingContext instanceof MutableRenderingContext) {
// Make the current velocity template id available
((MutableRenderingContext) this.renderingContext).push(this.renderingContext.getTransformation(), this.renderingContext.getXDOM(), this.renderingContext.getDefaultSyntax(), namespace, this.renderingContext.isRestricted(), this.renderingContext.getTargetSyntax());
renderingContextPushed = true;
}
}
this.progress.startStep(template, "template.evaluateContent.message", "Evaluate content of template with id [{}]", template.getId());
try {
this.velocityManager.evaluate(writer, namespace, new StringReader(content.getContent()));
} finally {
// Get rid of temporary rendering context
if (renderingContextPushed) {
((MutableRenderingContext) this.renderingContext).pop();
}
this.progress.endStep(template);
}
}
use of org.xwiki.rendering.internal.transformation.MutableRenderingContext in project xwiki-platform by xwiki.
the class DefaultVelocityEvaluator method evaluateVelocity.
@Override
public String evaluateVelocity(String content, String namespace, VelocityContext vcontext) throws XWikiException {
StringWriter writer = new StringWriter();
boolean renderingContextPushed = false;
try {
// Switch current namespace if needed
String currentNamespace = renderingContext.getTransformationId();
if (namespace != null && !StringUtils.equals(namespace, currentNamespace)) {
if (renderingContext instanceof MutableRenderingContext) {
// Make the current velocity template id available
((MutableRenderingContext) renderingContext).push(renderingContext.getTransformation(), renderingContext.getXDOM(), renderingContext.getDefaultSyntax(), namespace, renderingContext.isRestricted(), renderingContext.getTargetSyntax());
renderingContextPushed = true;
}
}
velocityManager.getVelocityEngine().evaluate(vcontext, writer, namespace, content);
return writer.toString();
} catch (Exception e) {
Object[] args = { namespace };
throw new XWikiException(XWikiException.MODULE_XWIKI_RENDERING, XWikiException.ERROR_XWIKI_RENDERING_VELOCITY_EXCEPTION, "Error while parsing velocity page {0}", e, args);
} finally {
// Get rid of temporary rendering context
if (renderingContextPushed) {
((MutableRenderingContext) this.renderingContext).pop();
}
}
}
Aggregations