Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #34038

MapBuilder

From Popping mad <rainbow@colition.gov>
Newsgroups comp.lang.java.programmer
Subject MapBuilder
Date 2016-03-28 13:39 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <ndbc6e$k7m$4@reader1.panix.com> (permalink)

Show all headers | View raw


Morning

I'm having trouble analyzing this code because I don't see any docs 
for this MapBuilder class.  Any idea what that is?

~~ris

import org.antlr.v4.runtime.*;

import java.io.*;
import java.util.Arrays;
import java.util.HashMap;

public class ConfigTest extends BaseErrorListener {

    public static void main(String[] args) {
        System.exit(new ConfigTest().runAllTests());
    }

    HashMap<String,String> readConf(String filename) throws IOException {
        // Instantiate the lexer and parser generated by Config.g4
        ANTLRInputStream input = new ANTLRInputStream(new FileReader(filename));
        ConfigLexer lexer = new ConfigLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        ConfigParser parser = new ConfigParser(tokens);
        // Call our syntaxError method when there are parse errors
        parser.addErrorListener(this);
        // Instantiate the visitor
        MapBuilder builder = new MapBuilder();  <<===========================HERE
        currentFail = false; // No parse failures yet, but if syntaxError is
                             // called, currentFail will be set to true.
        // Do the parse and then visit the tree with the MapBuilder.
        parser.top().accept(builder);
        // Return the HashMap containing the configuration data.
        return builder.map;
    }

    final String testsDir = "tests";
    ConfFileFilter confFilter = new ConfFileFilter();
    int numTests;
    int numFails;
    boolean currentFail = false;

    int runAllTests() {
        numTests = numFails = 0;
        for(String test : listTests()) {
            runOneTest(test);
        }
        return report();
    }

    String[] listTests() {
        String[] fs = new File(testsDir).list(confFilter);
        Arrays.sort(fs);
        for(int i = 0; i < fs.length; i++) {
            fs[i] = confFilter.baseName(fs[i]);
        }
        return fs;
    }

    void announce(String testName) {
        numTests++;
        System.err.println("========= #" + numTests + ": " + testName);
    }

    int report() {
        System.err.printf("\n!!!!!!!!! %d failures out of %d tests\n",
                numFails, numTests);
        return numFails;
    }

    void runOneTest(String testName) {
        announce(testName);
        String confFile = testsDir + File.separator + testName + ".conf";
        String dataFile = testsDir + File.separator + testName + ".dat";
        try {
            HashMap<String,String> actual = readConf(confFile);
            System.err.println("  actual: " + actual);
            ObjectInputStream iis = new ObjectInputStream(new FileInputStream(dataFile));
            Object expected = iis.readObject();
            System.err.println("expected: " + expected);
            if(actual.equals(expected) && !currentFail) {
                System.err.println("    PASS");
            }
            else {
                numFails++;
                System.err.println("    FAIL");
            }
        }
        catch (Exception e) {
            numFails++;
            e.printStackTrace(System.err);
        }
    }

    @Override
    public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
        currentFail = true;
        super.syntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, e);
    }
}

Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

MapBuilder Popping mad <rainbow@colition.gov> - 2016-03-28 13:39 +0000
  Re: MapBuilder Eric Douglas <e.d.programmer@gmail.com> - 2016-03-28 07:30 -0700
    Re: MapBuilder Popping mad <rainbow@colition.gov> - 2016-03-28 16:25 +0000
      Re: MapBuilder Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-03-28 14:53 -0400

csiph-web