<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 com.xpn.xwiki.render;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xwiki.cache.Cache;
import org.xwiki.cache.CacheException;
import org.xwiki.cache.config.CacheConfiguration;
import org.xwiki.cache.eviction.LRUEvictionConfiguration;

import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.monitor.api.MonitorPlugin;
import com.xpn.xwiki.render.groovy.XWikiGroovyRenderer;
import com.xpn.xwiki.util.Util;
import com.xpn.xwiki.web.Utils;
import com.xpn.xwiki.web.XWikiRequest;

public class DefaultXWikiRenderingEngine implements XWikiRenderingEngine
{
    private static final Log LOG = LogFactory.getLog(XWikiRenderingEngine.class);

    /** The default order in which the rendering engines will be run on the input. */
    private final String[] defaultRenderingOrder =
        new String[]{"macromapping", "groovy", "velocity", "plugin", "wiki", "wikiwiki"};

    private List<XWikiRenderer> renderers = new ArrayList<XWikiRenderer>();

    private HashMap<String, XWikiRenderer> renderermap = new LinkedHashMap<String, XWikiRenderer>();

    private Cache<XWikiRenderingCache> cache;

    public DefaultXWikiRenderingEngine(XWiki xwiki, XWikiContext context) throws XWikiException
    {
        String[] renderingOrder = xwiki.getConfig().getPropertyAsList("xwiki.render.renderingorder");
        if (renderingOrder == null || renderingOrder.length == 0) {
            renderingOrder = defaultRenderingOrder;
        }

        for (int i = 0; i < renderingOrder.length; i++) {
            if (xwiki.Param("xwiki.render." + renderingOrder[i], "1").equals("1")) {

                if (renderingOrder[i].equals("macromapping")) {
                    addRenderer("mapping", new XWikiMacrosMappingRenderer(xwiki, context));

                } else if (renderingOrder[i].equals("velocity")) {
                    addRenderer("velocity", new XWikiVelocityRenderer());

                } else if (renderingOrder[i].equals("groovy")) {
                    addRenderer("groovy", new XWikiGroovyRenderer());

                } else if (renderingOrder[i].equals("plugin")) {
                    addRenderer("plugin", new XWikiPluginRenderer());

                } else if (renderingOrder[i].equals("wiki")) {
                    addRenderer("wiki", new XWikiRadeoxRenderer(false));

                } else if (renderingOrder[i].equals("wikiwiki")) {
                    if (xwiki.Param("xwiki.render.wikiwiki", "0").equals("1")) {
                        addRenderer("xwiki", new XWikiWikiBaseRenderer(true, true));
                    } else {
                        addRenderer("xwiki", new XWikiWikiBaseRenderer(false, true));
                    }
                }
            }
        }

        // If there is no wikiwiki renderer, we must add it because it's the base renderer
        if (renderermap.get("xwiki") == null) {
            addRenderer("xwiki", new XWikiWikiBaseRenderer(false, true));
        }

        initCache(context);
    }

    public void virtualInit(XWikiContext context)
    {
        XWikiMacrosMappingRenderer mmrendered = (XWikiMacrosMappingRenderer) getRenderer("mapping");
        if (mmrendered != null) {
            mmrendered.loadPreferences(context.getWiki(), context);
        }
    }

    public void initCache(XWikiContext context) throws XWikiException
    {
        int iCapacity = 100;
        try {
            String capacity = context.getWiki().Param("xwiki.render.cache.capacity");
            if (capacity != null) {
                iCapacity = Integer.parseInt(capacity);
            }
        } catch (Exception e) {
        }
        initCache(iCapacity, context);
    }

    public void initCache(int iCapacity, XWikiContext context) throws XWikiException
    {
        try {
            CacheConfiguration configuration = new CacheConfiguration();
            configuration.setConfigurationId("xwiki.renderingcache");
            LRUEvictionConfiguration lru = new LRUEvictionConfiguration();
            lru.setMaxEntries(iCapacity);
            configuration.put(LRUEvictionConfiguration.CONFIGURATIONID, lru);

            this.cache = context.getWiki().getCacheFactory().newCache(configuration);
        } catch (CacheException e) {
            throw new XWikiException(XWikiException.MODULE_XWIKI_CACHE, XWikiException.ERROR_CACHE_INITIALIZING,
                "Failed to create cache");
        }
    }

    public Cache<XWikiRenderingCache> getCache()
    {
        return this.cache;
    }

    public void addRenderer(String name, XWikiRenderer renderer)
    {
        this.renderers.add(renderer);
        this.renderermap.put(name, renderer);
    }

    public XWikiRenderer getRenderer(String name)
    {
        return this.renderermap.get(name);
    }

    public List<XWikiRenderer> getRendererList()
    {
        return new ArrayList<XWikiRenderer>(this.renderers);
    }

    public List<String> getRendererNames()
    {
        return new LinkedList<String>(this.renderermap.keySet());
    }<commit_msg>[misc] Fix deprecated use<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 com.xpn.xwiki.render;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xwiki.cache.Cache;
import org.xwiki.cache.CacheException;
import org.xwiki.cache.CacheManager;
import org.xwiki.cache.config.CacheConfiguration;
import org.xwiki.cache.eviction.LRUEvictionConfiguration;

import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.monitor.api.MonitorPlugin;
import com.xpn.xwiki.render.groovy.XWikiGroovyRenderer;
import com.xpn.xwiki.util.Util;
import com.xpn.xwiki.web.Utils;
import com.xpn.xwiki.web.XWikiRequest;

public class DefaultXWikiRenderingEngine implements XWikiRenderingEngine
{
    private static final Log LOG = LogFactory.getLog(XWikiRenderingEngine.class);

    /** The default order in which the rendering engines will be run on the input. */
    private final String[] defaultRenderingOrder =
        new String[]{"macromapping", "groovy", "velocity", "plugin", "wiki", "wikiwiki"};

    private List<XWikiRenderer> renderers = new ArrayList<XWikiRenderer>();

    private HashMap<String, XWikiRenderer> renderermap = new LinkedHashMap<String, XWikiRenderer>();

    private Cache<XWikiRenderingCache> cache;

    public DefaultXWikiRenderingEngine(XWiki xwiki, XWikiContext context) throws XWikiException
    {
        String[] renderingOrder = xwiki.getConfig().getPropertyAsList("xwiki.render.renderingorder");
        if (renderingOrder == null || renderingOrder.length == 0) {
            renderingOrder = defaultRenderingOrder;
        }

        for (int i = 0; i < renderingOrder.length; i++) {
            if (xwiki.Param("xwiki.render." + renderingOrder[i], "1").equals("1")) {

                if (renderingOrder[i].equals("macromapping")) {
                    addRenderer("mapping", new XWikiMacrosMappingRenderer(xwiki, context));

                } else if (renderingOrder[i].equals("velocity")) {
                    addRenderer("velocity", new XWikiVelocityRenderer());

                } else if (renderingOrder[i].equals("groovy")) {
                    addRenderer("groovy", new XWikiGroovyRenderer());

                } else if (renderingOrder[i].equals("plugin")) {
                    addRenderer("plugin", new XWikiPluginRenderer());

                } else if (renderingOrder[i].equals("wiki")) {
                    addRenderer("wiki", new XWikiRadeoxRenderer(false));

                } else if (renderingOrder[i].equals("wikiwiki")) {
                    if (xwiki.Param("xwiki.render.wikiwiki", "0").equals("1")) {
                        addRenderer("xwiki", new XWikiWikiBaseRenderer(true, true));
                    } else {
                        addRenderer("xwiki", new XWikiWikiBaseRenderer(false, true));
                    }
                }
            }
        }

        // If there is no wikiwiki renderer, we must add it because it's the base renderer
        if (renderermap.get("xwiki") == null) {
            addRenderer("xwiki", new XWikiWikiBaseRenderer(false, true));
        }

        initCache(context);
    }

    public void virtualInit(XWikiContext context)
    {
        XWikiMacrosMappingRenderer mmrendered = (XWikiMacrosMappingRenderer) getRenderer("mapping");
        if (mmrendered != null) {
            mmrendered.loadPreferences(context.getWiki(), context);
        }
    }

    public void initCache(XWikiContext context) throws XWikiException
    {
        int iCapacity = 100;
        try {
            String capacity = context.getWiki().Param("xwiki.render.cache.capacity");
            if (capacity != null) {
                iCapacity = Integer.parseInt(capacity);
            }
        } catch (Exception e) {
        }
        initCache(iCapacity, context);
    }

    public void initCache(int iCapacity, XWikiContext context) throws XWikiException
    {
        try {
            CacheConfiguration configuration = new CacheConfiguration();
            configuration.setConfigurationId("xwiki.renderingcache");
            LRUEvictionConfiguration lru = new LRUEvictionConfiguration();
            lru.setMaxEntries(iCapacity);
            configuration.put(LRUEvictionConfiguration.CONFIGURATIONID, lru);

            this.cache = Utils.getComponent(CacheManager.class).createNewCache(configuration);
        } catch (CacheException e) {
            throw new XWikiException(XWikiException.MODULE_XWIKI_CACHE, XWikiException.ERROR_CACHE_INITIALIZING,
                "Failed to create cache");
        }
    }

    public Cache<XWikiRenderingCache> getCache()
    {
        return this.cache;
    }

    public void addRenderer(String name, XWikiRenderer renderer)
    {
        this.renderers.add(renderer);
        this.renderermap.put(name, renderer);
    }

    public XWikiRenderer getRenderer(String name)
    {
        return this.renderermap.get(name);
    }

    public List<XWikiRenderer> getRendererList()
    {
        return new ArrayList<XWikiRenderer>(this.renderers);
    }

    public List<String> getRendererNames()
    {
        return new LinkedList<String>(this.renderermap.keySet());
    }
{'fraction_non_alphanumeric': 0.10105701473414477, 'fraction_numerical': 0.003523382447149263, 'mean_word_length': 2.896723868954758, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 23, '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': '2567955', 'n_tokens_mistral': 3550, 'n_tokens_neox': 3431, 'n_words': 863}