<commit_before>package net.trajano.mojo.cleanpom;

import static java.lang.String.format;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ResourceBundle;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.StringUtils;
import org.sonatype.plexus.build.incremental.BuildContext;

/**
 * Cleans the POM.
 */
@Mojo(name = "clean", defaultPhase = LifecyclePhase.VERIFY, threadSafe = true)
public class CleanMojo extends AbstractMojo {
    /**
     * Resource bundle.
     */
    private static final ResourceBundle R = ResourceBundle
            .getBundle("META-INF/Messages");

    /**
     * Build context.
     */
    @Component
    private BuildContext buildContext;

    /**
     * The POM file to update.
     */
    @Parameter(defaultValue = "${basedir}/pom.xml", property = "cleanpom.pomFile", required = false)
    private File pomFile;

    /**
     * List of XSLT files as a comma separated value. Only one of xsltFiles or
     * xsltFileList is allowed.
     */
    @Parameter(defaultValue = "/META-INF/pom-clean.xslt", required = false, property = "cleanpom.xsltFileList")
    private String xsltFileList;

    /**
     * List of XSLT files.
     */
    @Parameter(required = false)
    private String[] xsltFiles;

    /**
     * Builds the handler chain.
     *
     * @param tf
     *            SAX transformer factory
     * @param outputStream
     *            output stream
     * @return handler chain.
     * @throws IOException
     *             I/O error when reading the XSLT file.
     * @throws TransformerException
     *             problem building the templates.
     */
    private TransformerHandler buildHandlerChain(
            final SAXTransformerFactory tf, final OutputStream outputStream)
            throws IOException, TransformerException {
        TransformerHandler handler = null;
        TransformerHandler lastHandler = null;

        for (final String xsltFile : xsltFiles) {
            final InputStream xsltStream = getClass().getResourceAsStream(
                    xsltFile.charAt(0) == '/' ? xsltFile : "/META-INF/"
                            + xsltFile);
            // The stream source needs to be defined here.
            final TransformerHandler currentHandler = tf
                    .newTransformerHandler(new StreamSource(xsltStream)); // NOPMD
            xsltStream.close();
            if (lastHandler != null) {
                // The result object needs to be created here.
                lastHandler.setResult(new SAXResult(currentHandler)); // NOPMD
                lastHandler = currentHandler;
            } else {
                lastHandler = currentHandler;
                handler = currentHandler;
            }
        }
        lastHandler.setResult(new StreamResult(outputStream));
        return handler;
    }

    /**
     * Builds the temporary file based on the {@link #pomFile}.
     *
     * @return temporary file
     * @throws MojoExecutionException
     *             wraps {@link IOException}
     */
    private File buildTempFile() throws MojoExecutionException {
        final File tempFile;
        try {
            tempFile = File.createTempFile("pom", "xml");
            tempFile.delete();
        } catch (final IOException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
        try {
            if (pomFile == null) {
                pomFile = new File("pom.xml");
            }
            FileUtils.copyFile(pomFile, tempFile);
        } catch (final IOException e) {
            throw new MojoExecutionException(format(R.getString("copyfail"),
                    pomFile, tempFile), e);
        }
        return tempFile;
    }

    /**
     * Performs the cleanup.
     *
     * @throws MojoExecutionException
     *             problem with the execution.
     */
    @Override
    public void execute() throws MojoExecutionException {
        final File tempFile = buildTempFile();
        transform(tempFile, pomFile);
    }

    /**
     * Performs the transformation.
     *
     * @param sourceFile
     *            source file
     * @param targetFile
     *            target file
     * @throws MojoExecutionException
     *             problem with the execution.
     */
    private void transform(final File sourceFile, final File targetFile)
            throws MojoExecutionException {
        try {
            final SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory
                    .newInstance();

            if (xsltFiles == null) {
                if (xsltFileList != null) {
                    xsltFiles = StringUtils.split(xsltFileList, ",");
                } else {
                    xsltFiles = new String[] { "/META-INF/pom-clean.xslt" };
                }
            }
            final OutputStream outputStream = buildContext
                    .newFileOutputStream(targetFile);
            final TransformerHandler handler = buildHandlerChain(tf,
                    outputStream);
            final Transformer transformer = tf.newTransformer();
            transformer.transform(new StreamSource(sourceFile), new SAXResult(
                    handler));
            getLog().debug(format(R.getString("donecleaning"), targetFile));
            sourceFile.delete();
            outputStream.close();
        } catch (final TransformerException e) {
            throw new MojoExecutionException(format(
                    R.getString("transformfail"), sourceFile), e);
        } catch (final IOException e) {
            throw new MojoExecutionException(format(
                    R.getString("transformfailio"), sourceFile), e);
        }
    }
}
<commit_msg>Changed to use PREPARE_PACKAGE phase<commit_after>package net.trajano.mojo.cleanpom;

import static java.lang.String.format;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ResourceBundle;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.StringUtils;
import org.sonatype.plexus.build.incremental.BuildContext;

/**
 * Cleans the POM.
 */
@Mojo(name = "clean", defaultPhase = LifecyclePhase.PREPARE_PACKAGE, threadSafe = true)
public class CleanMojo extends AbstractMojo {
    /**
     * Resource bundle.
     */
    private static final ResourceBundle R = ResourceBundle
            .getBundle("META-INF/Messages");

    /**
     * Build context.
     */
    @Component
    private BuildContext buildContext;

    /**
     * The POM file to update.
     */
    @Parameter(defaultValue = "${basedir}/pom.xml", property = "cleanpom.pomFile", required = false)
    private File pomFile;

    /**
     * List of XSLT files as a comma separated value. Only one of xsltFiles or
     * xsltFileList is allowed.
     */
    @Parameter(defaultValue = "/META-INF/pom-clean.xslt", required = false, property = "cleanpom.xsltFileList")
    private String xsltFileList;

    /**
     * List of XSLT files.
     */
    @Parameter(required = false)
    private String[] xsltFiles;

    /**
     * Builds the handler chain.
     *
     * @param tf
     *            SAX transformer factory
     * @param outputStream
     *            output stream
     * @return handler chain.
     * @throws IOException
     *             I/O error when reading the XSLT file.
     * @throws TransformerException
     *             problem building the templates.
     */
    private TransformerHandler buildHandlerChain(
            final SAXTransformerFactory tf, final OutputStream outputStream)
                    throws IOException, TransformerException {
        TransformerHandler handler = null;
        TransformerHandler lastHandler = null;

        for (final String xsltFile : xsltFiles) {
            final InputStream xsltStream = getClass().getResourceAsStream(
                    xsltFile.charAt(0) == '/' ? xsltFile : "/META-INF/"
                            + xsltFile);
            // The stream source needs to be defined here.
            final TransformerHandler currentHandler = tf
                    .newTransformerHandler(new StreamSource(xsltStream)); // NOPMD
            xsltStream.close();
            if (lastHandler != null) {
                // The result object needs to be created here.
                lastHandler.setResult(new SAXResult(currentHandler)); // NOPMD
                lastHandler = currentHandler;
            } else {
                lastHandler = currentHandler;
                handler = currentHandler;
            }
        }
        lastHandler.setResult(new StreamResult(outputStream));
        return handler;
    }

    /**
     * Builds the temporary file based on the {@link #pomFile}.
     *
     * @return temporary file
     * @throws MojoExecutionException
     *             wraps {@link IOException}
     */
    private File buildTempFile() throws MojoExecutionException {
        final File tempFile;
        try {
            tempFile = File.createTempFile("pom", "xml");
            tempFile.delete();
        } catch (final IOException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
        try {
            if (pomFile == null) {
                pomFile = new File("pom.xml");
            }
            FileUtils.copyFile(pomFile, tempFile);
        } catch (final IOException e) {
            throw new MojoExecutionException(format(R.getString("copyfail"),
                    pomFile, tempFile), e);
        }
        return tempFile;
    }

    /**
     * Performs the cleanup.
     *
     * @throws MojoExecutionException
     *             problem with the execution.
     */
    @Override
    public void execute() throws MojoExecutionException {
        final File tempFile = buildTempFile();
        transform(tempFile, pomFile);
    }

    /**
     * Performs the transformation.
     *
     * @param sourceFile
     *            source file
     * @param targetFile
     *            target file
     * @throws MojoExecutionException
     *             problem with the execution.
     */
    private void transform(final File sourceFile, final File targetFile)
            throws MojoExecutionException {
        try {
            final SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory
                    .newInstance();

            if (xsltFiles == null) {
                if (xsltFileList != null) {
                    xsltFiles = StringUtils.split(xsltFileList, ",");
                } else {
                    xsltFiles = new String[] { "/META-INF/pom-clean.xslt" };
                }
            }
            final OutputStream outputStream = buildContext
                    .newFileOutputStream(targetFile);
            final TransformerHandler handler = buildHandlerChain(tf,
                    outputStream);
            final Transformer transformer = tf.newTransformer();
            transformer.transform(new StreamSource(sourceFile), new SAXResult(
                    handler));
            getLog().debug(format(R.getString("donecleaning"), targetFile));
            sourceFile.delete();
            outputStream.close();
        } catch (final TransformerException e) {
            throw new MojoExecutionException(format(
                    R.getString("transformfail"), sourceFile), e);
        } catch (final IOException e) {
            throw new MojoExecutionException(format(
                    R.getString("transformfailio"), sourceFile), e);
        }
    }
}

{'fraction_non_alphanumeric': 0.09235275979031761, 'fraction_numerical': 0.0001541782300339192, 'mean_word_length': 2.122262334536703, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 22}, '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': '6435862', 'n_tokens_mistral': 3444, 'n_tokens_neox': 3337, 'n_words': 804}