<commit_before>/* $Id$
 *
 * Copyright 2009 University of Edinburgh.
 * All Rights Reserved
 */
package uk.ac.ed.ph.snuggletex.webapp;

import uk.ac.ed.ph.snuggletex.ErrorCode;
import uk.ac.ed.ph.snuggletex.internal.util.IOUtilities;
import uk.ac.ed.ph.snuggletex.utilities.MessageFormatter;
import uk.ac.ed.ph.snuggletex.utilities.SnuggleUtilities;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.PropertyResourceBundle;

/**
 * Builds a SnuggleTeX documentation page showing all of the error codes. This is called
 * as part of the webapp build process.
 *
 * @author  <NAME>
 * @version $Revision$
 */
public final class ErrorCodeDocumentBuilder {
    
    private final File outputFile;
    
    private static final Map<String, String> categoryHeadingMap;
    
    private static final String[] categoryData = {
        "TTE", "Tokenisation Errors",
        "TFE", "Token Fixing Errors",
        "TDE", "DOM Building Errors",
        "UCE", "Failures during Up-Conversion to Content MathML",
        "UME", "Failures during Up-Conversion to Maxima syntax"
    };
    
    static {
        categoryHeadingMap = new HashMap<String, String>();
        for (int i=0; i<categoryData.length; i+=2) {
            categoryHeadingMap.put(categoryData[i], categoryData[i+1]);
        }
    }
    
    public ErrorCodeDocumentBuilder(final File outputFile) {
        this.outputFile = outputFile;
    }
    
    public void run() throws IOException {
        IOUtilities.ensureFileCreated(outputFile);
        PrintWriter outputWriter = new PrintWriter(outputFile);
        outputWriter.println("\\pageId{errorCodes}");
        outputWriter.println("\n(In the tables below, \\{0\\} et\\ al\\ are placeholders for details "
                + "specific to each error instance that are substituted in when formatting error messages)");
        
        String currentCategory = null;
        String category;
        String codeName;
        PropertyResourceBundle errorMessageBundle = MessageFormatter.ERROR_MESSAGE_BUNDLE;
        for (ErrorCode errorCode : ErrorCode.values()) {
            codeName = errorCode.name();
            category = codeName.substring(0,3);
            if (!category.equals(currentCategory)) {
                /* Starting new category; finish current table if open */
                if (currentCategory!=null) {
                    outputWriter.println("\\end{tabular}");                    
                }
                currentCategory = category;
                outputWriter.println("\n\\subsection*{" + category + ": " + categoryHeadingMap.get(category));
                outputWriter.println("\\begin{tabular}{|c|l|}\n\\hline");
            }
            outputWriter.println("\\anchor{"
                    + codeName
                    + "}\\verb|"
                    + codeName
                    + "| & "
                    + SnuggleUtilities.quoteTextForInput(errorMessageBundle.getString(codeName))
                    + " \\\\ \\hline");
        }
        if (currentCategory!=null) {
            outputWriter.println("\\end{tabular}");
        }
        outputWriter.close();
    }
    
    public static void main(String[] args) throws IOException {
        if (args.length!=1) {
            System.err.println("Please supply an output file");
            System.exit(1);
        }
        new ErrorCodeDocumentBuilder(new File(args[0])).run();
    }
}
<commit_msg>Hmm... I should have tested this properly before committing r417. Fixed now!<commit_after>/* $Id$
 *
 * Copyright 2009 University of Edinburgh.
 * All Rights Reserved
 */
package uk.ac.ed.ph.snuggletex.webapp;

import uk.ac.ed.ph.snuggletex.ErrorCode;
import uk.ac.ed.ph.snuggletex.internal.util.IOUtilities;
import uk.ac.ed.ph.snuggletex.utilities.MessageFormatter;
import uk.ac.ed.ph.snuggletex.utilities.SnuggleUtilities;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.PropertyResourceBundle;

/**
 * Builds a SnuggleTeX documentation page showing all of the error codes. This is called
 * as part of the webapp build process.
 *
 * @author  <NAME>
 * @version $Revision$
 */
public final class ErrorCodeDocumentBuilder {
    
    private final File outputFile;
    
    private static final Map<String, String> categoryHeadingMap;
    
    private static final String[] categoryData = {
        "TTE", "LaTeX Parsing/Tokenisation Errors",
        "TFE", "Token Fix-up Errors",
        "TDE", "DOM Building Errors",
        "UCE", "Failures during Up-Conversion to Content MathML",
        "UME", "Failures during Up-Conversion to Maxima syntax"
    };
    
    static {
        categoryHeadingMap = new HashMap<String, String>();
        for (int i=0; i<categoryData.length; i+=2) {
            categoryHeadingMap.put(categoryData[i], categoryData[i+1]);
        }
    }
    
    public ErrorCodeDocumentBuilder(final File outputFile) {
        this.outputFile = outputFile;
    }
    
    public void run() throws IOException {
        IOUtilities.ensureFileCreated(outputFile);
        PrintWriter outputWriter = new PrintWriter(outputFile);
        outputWriter.println("\\pageId{errorCodes}");
        outputWriter.println("\n(In the tables below, \\{0\\} et\\ al\\ are placeholders for details "
                + "specific to each error instance that are substituted in when formatting error messages)");
        
        /* We just loop over each ErrorCode. Note that I'm relying on the fact that they've
         * been entered in a sensible order which makes grouping trivial here.
         */
        String currentCategory = null;
        String category;
        String codeName;
        PropertyResourceBundle errorMessageBundle = MessageFormatter.ERROR_MESSAGE_BUNDLE;
        for (ErrorCode errorCode : ErrorCode.values()) {
            codeName = errorCode.name();
            category = codeName.substring(0,3);
            if (!category.equals(currentCategory)) {
                /* Starting new category; finish current table if open */
                if (currentCategory!=null) {
                    outputWriter.println("\\end{tabular}");                    
                }
                currentCategory = category;
                outputWriter.println("\n\\subsection*{"
                        + category
                        + "xxx: "
                        + categoryHeadingMap.get(category)
                        + "}");
                outputWriter.println("\\begin{tabular}{|c|l|}\n\\hline");
            }
            outputWriter.println("\\anchor{"
                    + codeName
                    + "}\\verb|"
                    + codeName
                    + "| & "
                    + SnuggleUtilities.quoteTextForInput(errorMessageBundle.getString(codeName))
                    + " \\\\ \\hline");
        }
        if (currentCategory!=null) {
            outputWriter.println("\\end{tabular}");
        }
        outputWriter.close();
    }
    
    public static void main(String[] args) throws IOException {
        if (args.length!=1) {
            System.err.println("Please supply an output file");
            System.exit(1);
        }
        new ErrorCodeDocumentBuilder(new File(args[0])).run();
    }
}

{'fraction_non_alphanumeric': 0.1103728813559322, 'fraction_numerical': 0.003932203389830509, 'mean_word_length': 2.1575342465753424, 'pattern_counts': {'":': 1, '<': 11, '<?xml version=': 0, '>': 9, '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': '2566597', 'n_tokens_mistral': 2000, 'n_tokens_neox': 1965, 'n_words': 477}