use of javax.script.ScriptContext in project xwiki-platform by xwiki.
the class AbstractEditor method render.
@Override
public String render(D data, Map<String, Object> parameters) throws EditException {
ScriptContext scriptContext = this.scripts.getCurrentScriptContext();
Object currentEdit = scriptContext.getAttribute(EDIT_CONTEXT_KEY, ScriptContext.ENGINE_SCOPE);
try {
scriptContext.setAttribute(EDIT_CONTEXT_KEY, getEditContext(data, parameters), ScriptContext.ENGINE_SCOPE);
return render();
} finally {
scriptContext.setAttribute(EDIT_CONTEXT_KEY, currentEdit, ScriptContext.ENGINE_SCOPE);
}
}
use of javax.script.ScriptContext in project xwiki-platform by xwiki.
the class ViewAttachRevAction method render.
@Override
public String render(XWikiContext context) throws XWikiException {
XWikiRequest request = context.getRequest();
XWikiDocument doc = context.getDoc();
String filename;
if (context.getMode() == XWikiContext.MODE_PORTLET) {
filename = request.getParameter("filename");
} else {
filename = getFileName();
}
XWikiAttachment attachment;
if (context.getWiki().hasAttachmentRecycleBin(context) && request.getParameter("rid") != null) {
int recycleId = Integer.parseInt(request.getParameter("rid"));
attachment = new XWikiAttachment(doc, filename);
attachment = context.getWiki().getAttachmentRecycleBinStore().restoreFromRecycleBin(attachment, recycleId, context, true);
} else if (request.getParameter("id") != null) {
int id = Integer.parseInt(request.getParameter("id"));
attachment = doc.getAttachmentList().get(id);
} else {
attachment = doc.getAttachment(filename);
if (attachment == null) {
context.put("message", "attachmentdoesnotexist");
return "exception";
}
}
ScriptContext scriptContext = getCurrentScriptContext();
scriptContext.setAttribute("attachment", new Attachment((Document) scriptContext.getAttribute("doc"), attachment, context), ScriptContext.ENGINE_SCOPE);
return "viewattachrev";
}
use of javax.script.ScriptContext in project xwiki-platform by xwiki.
the class AbstractJSR223ScriptMacro method evaluateBlock.
/**
* Execute provided script and return {@link Block} based result.
*
* @param engine the script engine to use to evaluate the script.
* @param parameters the macro parameters.
* @param content the script to execute.
* @param context the context of the macro transformation.
* @return the result of script execution.
* @throws ScriptException failed to evaluate script
* @throws MacroExecutionException failed to evaluate provided content.
*/
protected List<Block> evaluateBlock(ScriptEngine engine, P parameters, String content, MacroTransformationContext context) throws ScriptException, MacroExecutionException {
List<Block> result;
ScriptContext scriptContext = getScriptContext();
Writer currentWriter = scriptContext.getWriter();
Reader currentReader = scriptContext.getReader();
Map<String, Object> currentEngineBindings = new HashMap<>(scriptContext.getBindings(ScriptContext.ENGINE_SCOPE));
// Set standard javax.script.filename property
MetaDataBlock metaDataBlock = context.getCurrentMacroBlock().getFirstBlock(new MetadataBlockMatcher(MetaData.SOURCE), Axes.ANCESTOR_OR_SELF);
if (metaDataBlock != null) {
scriptContext.setAttribute(ScriptEngine.FILENAME, metaDataBlock.getMetaData().getMetaData(MetaData.SOURCE), ScriptContext.ENGINE_SCOPE);
}
try {
StringWriter stringWriter = new StringWriter();
// set writer in script context
scriptContext.setWriter(stringWriter);
Object scriptResult = eval(content, engine, scriptContext);
result = convertScriptExecution(scriptResult, stringWriter, parameters, context);
} finally {
// restore current writer
scriptContext.setWriter(currentWriter);
// restore current reader
scriptContext.setReader(currentReader);
// restore "context" binding
restoreBinding(currentEngineBindings, scriptContext, BINDING_CONTEXT);
// restore "javax.script.filename" binding
restoreBinding(currentEngineBindings, scriptContext, ScriptEngine.FILENAME);
}
return result;
}
use of javax.script.ScriptContext in project xwiki-platform by xwiki.
the class PygmentsParser method highlight.
/**
* Return a highlighted version of the provided content.
*
* @param syntaxId the identifier of the source syntax.
* @param code the content to highlight.
* @return the highlighted version of the provided source.
* @throws ScriptException when failed to execute the script
* @throws ParseException when failed to parse the content as plain text
*/
private List<Block> highlight(String syntaxId, String code) throws ScriptException, ParseException {
BlocksGeneratorPygmentsListener listener = new BlocksGeneratorPygmentsListener(this.plainTextParser);
ScriptContext scriptContext = new SimpleScriptContext();
scriptContext.setAttribute(PY_LANGUAGE_VARNAME, syntaxId, ScriptContext.ENGINE_SCOPE);
scriptContext.setAttribute(PY_CODE_VARNAME, code, ScriptContext.ENGINE_SCOPE);
scriptContext.setAttribute(PY_STYLE_VARNAME, this.configuration.getStyle(), ScriptContext.ENGINE_SCOPE);
scriptContext.setAttribute(PY_LISTENER_VARNAME, listener, ScriptContext.ENGINE_SCOPE);
this.engine.eval(this.script, scriptContext);
List<Block> blocks;
if (scriptContext.getAttribute(PY_LEXER_VARNAME) != null) {
blocks = listener.getBlocks();
} else {
blocks = this.plainTextParser.parse(new StringReader(code)).getChildren().get(0).getChildren();
}
return blocks;
}
use of javax.script.ScriptContext 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);
}
}
Aggregations