<commit_before>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.rendering.internal.macro.script;

import java.io.StringWriter;
import java.util.List;

import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import org.apache.commons.lang.StringUtils;
import org.xwiki.bridge.DocumentAccessBridge;
import org.xwiki.rendering.block.Block;
import org.xwiki.rendering.macro.MacroExecutionException;
import org.xwiki.rendering.macro.descriptor.DefaultMacroDescriptor;
import org.xwiki.rendering.macro.script.AbstractScriptMacro;
import org.xwiki.rendering.macro.script.ScriptMacroParameters;
import org.xwiki.rendering.transformation.MacroTransformationContext;
import org.xwiki.script.ScriptContextManager;

/**
 * Execute script in provided script language.
 * 
 * @version $Id$
 * @since 1.7M3
 */
public class ScriptMacro extends AbstractScriptMacro<ScriptMacroParameters>
{
    /**
     * The description of the macro.
     */
    private static final String DESCRIPTION = "Execute script in provided script language.";

    /**
     * Used to get the current script context to give to script engine evaluation method.
     */
    private ScriptContextManager scriptContextManager;

    /**
     * Used to find if the current document's author has programming rights.
     */
    private DocumentAccessBridge documentAccessBridge;

    /**
     * The identifier of the script language. If null, {@link ScriptMacroParameters#getLanguage()} as to be not null.
     */
    private String language;

    /**
     * Create and initialize the descriptor of the macro.
     */
    public ScriptMacro()
    {
        super(new DefaultMacroDescriptor(DESCRIPTION, ScriptMacroParameters.class));
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.xwiki.rendering.macro.Macro#supportsInlineMode()
     */
    public boolean supportsInlineMode()
    {
        return true;
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.xwiki.rendering.macro.script.AbstractScriptMacro#execute(java.lang.Object, java.lang.String,
     *      org.xwiki.rendering.transformation.MacroTransformationContext)
     */
    @Override
    public List<Block> execute(ScriptMacroParameters parameters, String content, MacroTransformationContext context)
        throws MacroExecutionException
    {
        if (!canExecuteScript()) {
            throw new MacroExecutionException("You don't have the right to execute this script");
        }

        return super.execute(parameters, content, context);
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.xwiki.rendering.macro.script.AbstractScriptMacro#evaluate(java.lang.Object, java.lang.String,
     *      org.xwiki.rendering.transformation.MacroTransformationContext)
     */
    @Override
    protected String evaluate(ScriptMacroParameters parameters, String content, MacroTransformationContext context)
        throws MacroExecutionException
    {
        if (StringUtils.isEmpty(content)) {
            return "";
        }

        String engineName;

        // 1) resolve script engine name
        if (this.language == null) {
            String macroName = context.getCurrentMacroBlock().getName().toLowerCase();

            if (macroName.equals("script")) {
                engineName = parameters.getLanguage();
            } else {
                engineName = macroName;
            }
        } else {
            engineName = this.language;
        }

        String scriptResult;
        if (engineName != null) {
            // 2) execute script
            try {
                ScriptEngineManager sem = new ScriptEngineManager(this.getClass().getClassLoader());
                ScriptEngine engine = sem.getEngineByName(engineName);
                if (engine != null) {
                    ScriptContext scriptContext = this.scriptContextManager.getScriptContext();

                    StringWriter stringWriter = new StringWriter();

                    // set writer in script context
                    scriptContext.setWriter(stringWriter);

                    if (engine instanceof Compilable) {
                        CompiledScript compiledScript = getCompiledScript(content, (Compilable) engine);
<commit_msg>Fix wrong modification in previous commit<commit_after>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.rendering.internal.macro.script;

import java.io.StringWriter;
import java.util.List;

import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import org.apache.commons.lang.StringUtils;
import org.xwiki.bridge.DocumentAccessBridge;
import org.xwiki.rendering.block.Block;
import org.xwiki.rendering.macro.MacroExecutionException;
import org.xwiki.rendering.macro.descriptor.DefaultMacroDescriptor;
import org.xwiki.rendering.macro.script.AbstractScriptMacro;
import org.xwiki.rendering.macro.script.ScriptMacroParameters;
import org.xwiki.rendering.transformation.MacroTransformationContext;
import org.xwiki.script.ScriptContextManager;

/**
 * Execute script in provided script language.
 * 
 * @version $Id$
 * @since 1.7M3
 */
public class ScriptMacro extends AbstractScriptMacro<ScriptMacroParameters>
{
    /**
     * The description of the macro.
     */
    private static final String DESCRIPTION = "Execute script in provided script language.";

    /**
     * Used to get the current script context to give to script engine evaluation method.
     */
    private ScriptContextManager scriptContextManager;

    /**
     * Used to find if the current document's author has programming rights.
     */
    private DocumentAccessBridge documentAccessBridge;

    /**
     * The identifier of the script language. If null, {@link ScriptMacroParameters#getLanguage()} as to be not null.
     */
    private String language;

    /**
     * Create and initialize the descriptor of the macro.
     */
    public ScriptMacro()
    {
        super(new DefaultMacroDescriptor(DESCRIPTION, ScriptMacroParameters.class));
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.xwiki.rendering.macro.Macro#supportsInlineMode()
     */
    public boolean supportsInlineMode()
    {
        return true;
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.xwiki.rendering.macro.script.AbstractScriptMacro#execute(java.lang.Object, java.lang.String,
     *      org.xwiki.rendering.transformation.MacroTransformationContext)
     */
    @Override
    public List<Block> execute(ScriptMacroParameters parameters, String content, MacroTransformationContext context)
        throws MacroExecutionException
    {
        if (!canExecuteScript()) {
            throw new MacroExecutionException("You don't have the right to execute this script");
        }

        return super.execute(parameters, content, context);
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.xwiki.rendering.macro.script.AbstractScriptMacro#evaluate(java.lang.Object, java.lang.String,
     *      org.xwiki.rendering.transformation.MacroTransformationContext)
     */
    @Override
    protected String evaluate(ScriptMacroParameters parameters, String content, MacroTransformationContext context)
        throws MacroExecutionException
    {
        if (StringUtils.isEmpty(content)) {
            return "";
        }

        String engineName;

        // 1) resolve script engine name
        if (this.language == null) {
            String macroName = context.getCurrentMacroBlock().getName().toLowerCase();

            if (macroName.equals("script")) {
                engineName = parameters.getLanguage();
            } else {
                engineName = macroName;
            }
        } else {
            engineName = this.language;
        }

        String scriptResult;
        if (engineName != null) {
            // 2) execute script
            try {
                ScriptEngineManager sem = new ScriptEngineManager();
                ScriptEngine engine = sem.getEngineByName(engineName);
                if (engine != null) {
                    ScriptContext scriptContext = this.scriptContextManager.getScriptContext();

                    StringWriter stringWriter = new StringWriter();

                    // set writer in script context
                    scriptContext.setWriter(stringWriter);

                    if (engine instanceof Compilable) {
                        CompiledScript compiledScript = getCompiledScript(content, (Compilable) engine);

{'fraction_non_alphanumeric': 0.08426103646833014, 'fraction_numerical': 0.00345489443378119, 'mean_word_length': 2.842551622418879, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4338793', 'n_tokens_mistral': 2703, 'n_tokens_neox': 2598, 'n_words': 783}