Search in sources :

Example 21 with ScriptContext

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);
    }
}
Also used : ScriptContext(javax.script.ScriptContext)

Example 22 with ScriptContext

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";
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ScriptContext(javax.script.ScriptContext) Attachment(com.xpn.xwiki.api.Attachment) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Document(com.xpn.xwiki.api.Document)

Example 23 with ScriptContext

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;
}
Also used : StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) MetadataBlockMatcher(org.xwiki.rendering.block.match.MetadataBlockMatcher) Block(org.xwiki.rendering.block.Block) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) ScriptContext(javax.script.ScriptContext) Reader(java.io.Reader) StringWriter(java.io.StringWriter) Writer(java.io.Writer) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock)

Example 24 with ScriptContext

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;
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) StringReader(java.io.StringReader) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Block(org.xwiki.rendering.block.Block) NewLineBlock(org.xwiki.rendering.block.NewLineBlock)

Example 25 with ScriptContext

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);
    }
}
Also used : XWikiURLFactory(com.xpn.xwiki.web.XWikiURLFactory) ExternalServletURLFactory(com.xpn.xwiki.web.ExternalServletURLFactory) NotificationException(org.xwiki.notifications.NotificationException) XWikiContext(com.xpn.xwiki.XWikiContext) ScriptContext(javax.script.ScriptContext) MutableRenderingContext(org.xwiki.rendering.internal.transformation.MutableRenderingContext) NotificationException(org.xwiki.notifications.NotificationException)

Aggregations

ScriptContext (javax.script.ScriptContext)124 SimpleScriptContext (javax.script.SimpleScriptContext)81 Bindings (javax.script.Bindings)33 Test (org.junit.Test)30 SimpleBindings (javax.script.SimpleBindings)28 Test (org.junit.jupiter.api.Test)19 ScriptException (javax.script.ScriptException)17 ScriptEngine (javax.script.ScriptEngine)16 HashMap (java.util.HashMap)13 CompiledScript (javax.script.CompiledScript)11 IOException (java.io.IOException)8 Map (java.util.Map)8 ScriptEngineManager (javax.script.ScriptEngineManager)8 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)7 Test (org.testng.annotations.Test)7 StringWriter (java.io.StringWriter)6 NashornScriptEngine (jdk.nashorn.api.scripting.NashornScriptEngine)6 XWikiException (com.xpn.xwiki.XWikiException)5 Reader (java.io.Reader)5 StringReader (java.io.StringReader)5