<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.tool.doc;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Iterator;
import java.util.List;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.xwiki.component.embed.EmbeddableComponentManager;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.context.ExecutionContext;
import org.xwiki.context.ExecutionContextException;
import org.xwiki.context.ExecutionContextManager;
import org.xwiki.model.reference.DocumentReference;

import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.objects.classes.BaseClass;
import com.xpn.xwiki.web.Utils;

/**
 * An abstract Mojo that knows how to load a XWikiDocument from XML and to write XML from a XWikiDocument
 * 
 * @version $Id$
 */
public abstract class AbstractDocumentMojo extends AbstractMojo
{
    /**
     * The maven project.
     * 
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    private MavenProject project;

    /**
     * The document to perform the update on
     * 
     * @parameter expression="${basedir}/src/main/resources/XWiki/XWikiPreferences"
     * @required
     */
    protected File sourceDocument;

    /**
     * The target directory to write the document back to
     * 
     * @parameter expression="${project.build.outputDirectory}"
     * @required
     */
    protected File outputDirectory;

    /**
     * An empty context that will hold the base classes encountered in the passed XML document. This is needed in order
     * not to lose the class definition when writing back to XML.
     */
    private XWikiContext context;

    public AbstractDocumentMojo() throws MojoExecutionException
    {
        this.context = createXWikiContext();
    }

    protected XWikiContext createXWikiContext() throws MojoExecutionException
    {
        EmbeddableComponentManager ecm = new EmbeddableComponentManager();
        ecm.initialize(this.getClass().getClassLoader());
        Utils.setComponentManager(ecm);

        // Context component manager is totally useless here
        ecm.unregisterComponent(ComponentManager.class, "context");

        // We need to initialize the Component Manager so that the components can be looked up
        XWikiContext xcontext = new XWikiContext();
        xcontext.put(ComponentManager.class.getName(), ecm);

        // Initialize the Container fields (request, response, session).
        ExecutionContextManager ecim = Utils.getComponent(ExecutionContextManager.class);
        try {
            ExecutionContext econtext = new ExecutionContext();

            // Bridge with old XWiki Context, required for old code.
            xcontext.declareInExecutionContext(econtext);

            ecim.initialize(econtext);
        } catch (ExecutionContextException e) {
            throw new MojoExecutionException("Failed to initialize Execution Context.", e);
        }

        return xcontext;
    }

    /**
     * Loads a XWikiDocument from a XML file
     * 
     * @param file the xml file to load
     * @return the XWiki document loaded from XML
     * @throws MojoExecutionException<commit_msg>[msic] cleanup and codestyle<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.tool.doc;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Iterator;
import java.util.List;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.xwiki.component.embed.EmbeddableComponentManager;
import org.xwiki.component.manager.ComponentLookupException;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.context.ExecutionContext;
import org.xwiki.context.ExecutionContextException;
import org.xwiki.context.ExecutionContextManager;
import org.xwiki.model.reference.DocumentReference;

import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.objects.classes.BaseClass;
import com.xpn.xwiki.web.Utils;

/**
 * An abstract Mojo that knows how to load a XWikiDocument from XML and to write XML from a XWikiDocument
 * 
 * @version $Id$
 */
public abstract class AbstractDocumentMojo extends AbstractMojo
{
    /**
     * The document to perform the update on
     * 
     * @parameter expression="${basedir}/src/main/resources/XWiki/XWikiPreferences"
     * @required
     */
    protected File sourceDocument;

    /**
     * The target directory to write the document back to
     * 
     * @parameter expression="${project.build.outputDirectory}"
     * @required
     */
    protected File outputDirectory;

    /**
     * An empty context that will hold the base classes encountered in the passed XML document. This is needed in order
     * not to lose the class definition when writing back to XML.
     */
    private XWikiContext context;

    public AbstractDocumentMojo() throws MojoExecutionException
    {
        this.context = createXWikiContext();
    }

    protected XWikiContext createXWikiContext() throws MojoExecutionException
    {
        EmbeddableComponentManager ecm = new EmbeddableComponentManager();
        ecm.initialize(this.getClass().getClassLoader());
        Utils.setComponentManager(ecm);

        // Context component manager is totally useless here
        ecm.unregisterComponent(ComponentManager.class, "context");

        // We need to initialize the Component Manager so that the components can be looked up
        XWikiContext xcontext = new XWikiContext();
        xcontext.put(ComponentManager.class.getName(), ecm);

        // Initialize the Container fields (request, response, session).
        try {
            ExecutionContextManager ecim = ecm.getInstance(ExecutionContextManager.class);

            ExecutionContext econtext = new ExecutionContext();

            // Bridge with old XWiki Context, required for old code.
            xcontext.declareInExecutionContext(econtext);

            ecim.initialize(econtext);
        } catch (ExecutionContextException | ComponentLookupException e) {
            throw new MojoExecutionException("Failed to initialize Execution Context.", e);
        }

        return xcontext;
    }

    /**
     * Loads a XWikiDocument from a XML file
     * 
     * @param file the xml file to load
     * @return the XWiki document loaded from XML
     * @throws MojoExecutionException
{'fraction_non_alphanumeric': 0.07950857672693556, 'fraction_numerical': 0.003940658321743162, 'mean_word_length': 3.4047983665135275, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '146847', 'n_tokens_mistral': 2311, 'n_tokens_neox': 2256, 'n_words': 781}