<commit_before>////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2008  <NAME>
//
// This library 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 library 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 library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle;

import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.ResourceBundle;

import com.puppycrawl.tools.checkstyle.api.AuditEvent;
import com.puppycrawl.tools.checkstyle.api.AuditListener;
import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
import com.puppycrawl.tools.checkstyle.api.SeverityLevel;

/**
 * Simple XML logger.
 * It outputs everything in UTF-8 (default XML encoding is UTF-8) in case
 * we want to localize error messages or simply that filenames are
 * localized and takes care about escaping as well.

 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public class XMLLogger
    extends AutomaticBean
    implements AuditListener
{
    /** decimal radix */
    private static final int BASE_10 = 10;

    /** hex radix */
    private static final int BASE_16 = 16;

    /** close output stream in auditFinished */
    private boolean mCloseStream;

    /** helper writer that allows easy encoding and printing */
    private PrintWriter mWriter;

    /** some known entities to detect */
    private static final String[] ENTITIES = {"gt", "amp", "lt", "apos",
                                              "quot", };

    /**
     * Creates a new <code>XMLLogger</code> instance.
     * Sets the output to a defined stream.
     * @param aOS the stream to write logs to.
     * @param aCloseStream close aOS in auditFinished
     */
    public XMLLogger(OutputStream aOS, boolean aCloseStream)
    {
        setOutputStream(aOS);
        mCloseStream = aCloseStream;
    }

    /**
     * sets the OutputStream
     * @param aOS the OutputStream to use
     **/
    private void setOutputStream(OutputStream aOS)
    {
        try {
            final OutputStreamWriter osw = new OutputStreamWriter(aOS, "UTF-8");
            mWriter = new PrintWriter(osw);
        }
        catch (final UnsupportedEncodingException e) {
            // unlikely to happen...
            throw new ExceptionInInitializerError(e);
        }
    }

    /** {@inheritDoc} */
    public void auditStarted(AuditEvent aEvt)
    {
        mWriter.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

        final ResourceBundle compilationProperties =
            ResourceBundle.getBundle("checkstylecompilation");
        final String version =
            compilationProperties.getString("checkstyle.compile.version");

        mWriter.println("<checkstyle version=\"" + version + "\">");
    }

    /** {@inheritDoc} */
    public void auditFinished(AuditEvent aEvt)
    {
        mWriter.println("</checkstyle>");
        if (mCloseStream) {
            mWriter.close();
        }
        else {
            mWriter.flush();
        }
    }

    /** {@inheritDoc} */
    public void fileStarted(AuditEvent aEvt)
    {
        mWriter.println("<file name=\"" + aEvt.getFileName() + "\">");
    }

    /** {@inheritDoc} */
    public void fileFinished(AuditEvent aEvt)
    {
        mWriter.println("</file>");
    }

    /** {@inheritDoc} */
    public void addError(AuditEvent aEvt)
    {
        if (!SeverityLevel.IGNORE.equals(aEvt.getSeverityLevel())) {
            mWriter.print("<error" + " line=\"" + aEvt.getLine() + "\"");
            if (aEvt.getColumn() > 0) {
                mWriter.print(" column=\"" + aEvt.getColumn() + "\"");
            }
            mWriter.print(" severity=\""
                + aEvt.getSeverityLevel().getName()
                + "\"");
            mWriter.print(" message=\""
                + encode(aEvt.getMessage())
                + "\"");
            mWriter.println(" source=\""
                + encode(aEvt.getSourceName())
                + "\"/>");<commit_msg>[ 2438924 ] XMLogger produces invalid XML<commit_after>////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2008  <NAME>
//
// This library 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 library 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 library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle;

import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.ResourceBundle;

import com.puppycrawl.tools.checkstyle.api.AuditEvent;
import com.puppycrawl.tools.checkstyle.api.AuditListener;
import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
import com.puppycrawl.tools.checkstyle.api.SeverityLevel;

/**
 * Simple XML logger.
 * It outputs everything in UTF-8 (default XML encoding is UTF-8) in case
 * we want to localize error messages or simply that filenames are
 * localized and takes care about escaping as well.

 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public class XMLLogger
    extends AutomaticBean
    implements AuditListener
{
    /** decimal radix */
    private static final int BASE_10 = 10;

    /** hex radix */
    private static final int BASE_16 = 16;

    /** close output stream in auditFinished */
    private boolean mCloseStream;

    /** helper writer that allows easy encoding and printing */
    private PrintWriter mWriter;

    /** some known entities to detect */
    private static final String[] ENTITIES = {"gt", "amp", "lt", "apos",
                                              "quot", };

    /**
     * Creates a new <code>XMLLogger</code> instance.
     * Sets the output to a defined stream.
     * @param aOS the stream to write logs to.
     * @param aCloseStream close aOS in auditFinished
     */
    public XMLLogger(OutputStream aOS, boolean aCloseStream)
    {
        setOutputStream(aOS);
        mCloseStream = aCloseStream;
    }

    /**
     * sets the OutputStream
     * @param aOS the OutputStream to use
     **/
    private void setOutputStream(OutputStream aOS)
    {
        try {
            final OutputStreamWriter osw = new OutputStreamWriter(aOS, "UTF-8");
            mWriter = new PrintWriter(osw);
        }
        catch (final UnsupportedEncodingException e) {
            // unlikely to happen...
            throw new ExceptionInInitializerError(e);
        }
    }

    /** {@inheritDoc} */
    public void auditStarted(AuditEvent aEvt)
    {
        mWriter.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

        final ResourceBundle compilationProperties =
            ResourceBundle.getBundle("checkstylecompilation");
        final String version =
            compilationProperties.getString("checkstyle.compile.version");

        mWriter.println("<checkstyle version=\"" + version + "\">");
    }

    /** {@inheritDoc} */
    public void auditFinished(AuditEvent aEvt)
    {
        mWriter.println("</checkstyle>");
        if (mCloseStream) {
            mWriter.close();
        }
        else {
            mWriter.flush();
        }
    }

    /** {@inheritDoc} */
    public void fileStarted(AuditEvent aEvt)
    {
        mWriter.println("<file name=\"" + encode(aEvt.getFileName()) + "\">");
    }

    /** {@inheritDoc} */
    public void fileFinished(AuditEvent aEvt)
    {
        mWriter.println("</file>");
    }

    /** {@inheritDoc} */
    public void addError(AuditEvent aEvt)
    {
        if (!SeverityLevel.IGNORE.equals(aEvt.getSeverityLevel())) {
            mWriter.print("<error" + " line=\"" + aEvt.getLine() + "\"");
            if (aEvt.getColumn() > 0) {
                mWriter.print(" column=\"" + aEvt.getColumn() + "\"");
            }
            mWriter.print(" severity=\""
                + aEvt.getSeverityLevel().getName()
                + "\"");
            mWriter.print(" message=\""
                + encode(aEvt.getMessage())
                + "\"");
            mWriter.println(" source=\""
                + encode(aEvt.getSourceName())
                + "\"/>");
{'fraction_non_alphanumeric': 0.14705882352941177, 'fraction_numerical': 0.008802816901408451, 'mean_word_length': 2.175600131535679, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 2, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5934937', 'n_tokens_mistral': 2692, 'n_tokens_neox': 2593, 'n_words': 775}