<commit_before>////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2002  <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 antlr.RecognitionException;
import antlr.TokenStreamException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.OutputStream;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.HashSet;
import org.apache.regexp.RESyntaxException;

/**
 * This class provides the functionality to check a set of files.
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public class Checker
    implements Defn
{
    /** configuration */
    private final Configuration mConfig;

    /** cache file **/
    private final PropertyCacheFile mCache;

    /** vector of listeners */
    private final ArrayList mListeners = new ArrayList();

    /**
     * Constructs the object.
     * @param aConfig contains the configuration to check with
     * @throws RESyntaxException unable to create a regexp object
     **/
    public Checker(Configuration aConfig)
        throws RESyntaxException
    {
        mConfig = aConfig;
        mCache = new PropertyCacheFile(aConfig.getCacheFile());
        final Verifier v = new Verifier(aConfig);
        VerifierSingleton.setInstance(v);
    }

    /** Cleans up the object **/
    public void destroy()
    {
        mCache.destroy();

        // close all streamable listeners
        final Iterator it = mListeners.iterator();
        while (it.hasNext()) {
            final Object obj = it.next();
            if (obj instanceof Streamable) {
                final Streamable str = (Streamable) obj;
                final OutputStream os = str.getOutputStream();
                // close only those that can be closed...
                if ((os != System.out) && (os != System.err) && (os != null)) {
                    try  {
                        os.flush();
                        os.close();
                    }
                    catch (IOException ignored) {
                    }
                }
            }
            it.remove();
        }
    }

    /**
     * Add the listener that will be used to receive events from the audit
     * @param aListener the nosy thing
     */
    public void addListener(AuditListener aListener)
    {
        mListeners.add(aListener);
    }

    /**
     * Processes a set of files.
     * Once this is done, it is highly recommended to call for
     * the destroy method to close and remove the listeners.
     * @param aFiles the list of files to be audited.
     * @return the total number of errors found
     * @see #destroy()
     */
    public int process(String[] aFiles)
    {
        int total = 0;
        fireAuditStarted();

        /*

          If you move checkPackageHtml() around beware of the caching
          functionality of checkstyle. Make sure that package.html
          checks are not skipped because of caching. Otherwise you
          might e.g. have a package.html file, check all java files
          without errors, delete package.html and then recheck without
          errors because the html file is not covered by the cache.

        */

        total += checkPackageHtml(aFiles);
        
        for (int i = 0; i < aFiles.length; i++)
        {
            total += process(aFiles[i]);
        }
        fireAuditFinished();
        return total;
    }

    /**
     * Processes a specified file and prints out all errors found.
     * @return the number of errors found
     * @param aFileName the name of the file to process
     **/
    private int process(String aFileName)
    {
        final File f = new File(aFileName);
        final long timestamp = f.lastModified();
        if (mCache.alreadyChecked(aFileName, timestamp)) {
            return 0;
        }

        LineText[] errors;
        try {
            fireFileStarted(aFileName);
            final String[] lines = getLines(aFileName);
            VerifierSingleton.getInstance().clearMessages();
            VerifierSingleton.getInstance().setLines(lines);
            final Reader sar = new StringArrayReader(lines);
            final GeneratedJavaLexer jl = new GeneratedJavaLexer(sar);
            jl.setFilename(aFileName);
            final GeneratedJavaRecognizer jr = new GeneratedJavaRecognizer(jl);
            jr.setFilename(aFileName);
            jr.setASTNodeClass(MyCommonAST.class.getName());
            jr.compilationUnit();
            errors = VerifierSingleton.getInstance().getMessages();
        }
        catch (FileNotFoundException fnfe) {
            errors = new LineText[] {new LineText(0, "File not found!")};
        }
        catch (IOException ioe) {
            errors = new LineText[] {
                new LineText(0, "Got an IOException -" + ioe.getMessage())};
        }
        catch (RecognitionException re) {
            errors = new LineText[] {
                new LineText(0,
                             "Got a RecognitionException -" + re.getMessage())};
        }
        catch (TokenStreamException te) {
            errors = new LineText[] {
                new LineText(0,
                             "Got a TokenStreamException -" + te.getMessage())};
        }

        if (errors.length == 0) {
            mCache.checkedOk(aFileName, timestamp);
        }
        else {
            fireErrors(aFileName, errors);
        }

        fireFileFinished(aFileName);
        return errors.length;
    }

    /**
     * Checks for a package.html file for all java files in parameter list.
     * @param aFiles the filenames of the java files to check
     * @return the number of errors found
     */
    private int checkPackageHtml(String[] aFiles)
    {
        int packageHtmlErrors = 0;
        
        if (aFiles != null && mConfig.isRequirePackageHtml())
        {
            final HashSet checkedPackages = new HashSet();
            for (int i = 0; i < aFiles.length; i++)
            {
                final File file = new File(aFiles[i]);
                final File packageDir = file.getParentFile();<commit_msg>Changed a comment to C++ style<commit_after>////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2002  <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 antlr.RecognitionException;
import antlr.TokenStreamException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.OutputStream;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.HashSet;
import org.apache.regexp.RESyntaxException;

/**
 * This class provides the functionality to check a set of files.
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public class Checker
    implements Defn
{
    /** configuration */
    private final Configuration mConfig;

    /** cache file **/
    private final PropertyCacheFile mCache;

    /** vector of listeners */
    private final ArrayList mListeners = new ArrayList();

    /**
     * Constructs the object.
     * @param aConfig contains the configuration to check with
     * @throws RESyntaxException unable to create a regexp object
     **/
    public Checker(Configuration aConfig)
        throws RESyntaxException
    {
        mConfig = aConfig;
        mCache = new PropertyCacheFile(aConfig.getCacheFile());
        final Verifier v = new Verifier(aConfig);
        VerifierSingleton.setInstance(v);
    }

    /** Cleans up the object **/
    public void destroy()
    {
        mCache.destroy();

        // close all streamable listeners
        final Iterator it = mListeners.iterator();
        while (it.hasNext()) {
            final Object obj = it.next();
            if (obj instanceof Streamable) {
                final Streamable str = (Streamable) obj;
                final OutputStream os = str.getOutputStream();
                // close only those that can be closed...
                if ((os != System.out) && (os != System.err) && (os != null)) {
                    try  {
                        os.flush();
                        os.close();
                    }
                    catch (IOException ignored) {
                    }
                }
            }
            it.remove();
        }
    }

    /**
     * Add the listener that will be used to receive events from the audit
     * @param aListener the nosy thing
     */
    public void addListener(AuditListener aListener)
    {
        mListeners.add(aListener);
    }

    /**
     * Processes a set of files.
     * Once this is done, it is highly recommended to call for
     * the destroy method to close and remove the listeners.
     * @param aFiles the list of files to be audited.
     * @return the total number of errors found
     * @see #destroy()
     */
    public int process(String[] aFiles)
    {
        int total = 0;
        fireAuditStarted();

        // If you move checkPackageHtml() around beware of the caching
        // functionality of checkstyle. Make sure that package.html
        // checks are not skipped because of caching. Otherwise you
        // might e.g. have a package.html file, check all java files
        // without errors, delete package.html and then recheck without
        // errors because the html file is not covered by the cache.
        total += checkPackageHtml(aFiles);

        for (int i = 0; i < aFiles.length; i++)
        {
            total += process(aFiles[i]);
        }
        fireAuditFinished();
        return total;
    }

    /**
     * Processes a specified file and prints out all errors found.
     * @return the number of errors found
     * @param aFileName the name of the file to process
     **/
    private int process(String aFileName)
    {
        final File f = new File(aFileName);
        final long timestamp = f.lastModified();
        if (mCache.alreadyChecked(aFileName, timestamp)) {
            return 0;
        }

        LineText[] errors;
        try {
            fireFileStarted(aFileName);
            final String[] lines = getLines(aFileName);
            VerifierSingleton.getInstance().clearMessages();
            VerifierSingleton.getInstance().setLines(lines);
            final Reader sar = new StringArrayReader(lines);
            final GeneratedJavaLexer jl = new GeneratedJavaLexer(sar);
            jl.setFilename(aFileName);
            final GeneratedJavaRecognizer jr = new GeneratedJavaRecognizer(jl);
            jr.setFilename(aFileName);
            jr.setASTNodeClass(MyCommonAST.class.getName());
            jr.compilationUnit();
            errors = VerifierSingleton.getInstance().getMessages();
        }
        catch (FileNotFoundException fnfe) {
            errors = new LineText[] {new LineText(0, "File not found!")};
        }
        catch (IOException ioe) {
            errors = new LineText[] {
                new LineText(0, "Got an IOException -" + ioe.getMessage())};
        }
        catch (RecognitionException re) {
            errors = new LineText[] {
                new LineText(0,
                             "Got a RecognitionException -" + re.getMessage())};
        }
        catch (TokenStreamException te) {
            errors = new LineText[] {
                new LineText(0,
                             "Got a TokenStreamException -" + te.getMessage())};
        }

        if (errors.length == 0) {
            mCache.checkedOk(aFileName, timestamp);
        }
        else {
            fireErrors(aFileName, errors);
        }

        fireFileFinished(aFileName);
        return errors.length;
    }

    /**
     * Checks for a package.html file for all java files in parameter list.
     * @param aFiles the filenames of the java files to check
     * @return the number of errors found
     */
    private int checkPackageHtml(String[] aFiles)
    {
        int packageHtmlErrors = 0;

        if (aFiles != null && mConfig.isRequirePackageHtml())
        {
            final HashSet checkedPackages = new HashSet();
            for (int i = 0; i < aFiles.length; i++)
            {
                final File file = new File(aFiles[i]);
                final File packageDir = file.getParentFile();
{'fraction_non_alphanumeric': 0.11210762331838565, 'fraction_numerical': 0.00476457399103139, 'mean_word_length': 1.9194109224790346, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6645257', 'n_tokens_mistral': 3706, 'n_tokens_neox': 3567, 'n_words': 1260}