Subversion Repositories display

Rev

Rev 307 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * Copyright (C) 2008 Steve Ratcliffe
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 or
 * version 2 as published by the Free Software Foundation.
 *
 * This program 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 General Public License for more details.
 */

package test.check;

import java.util.ArrayList;
import java.util.List;

import test.display.CommonDisplay;

/**
 * A common class for all check programs.
 *
 * It is intended that the code that reads files and checks them is independent of
 * the code in mkgmap as much as possible. Some of the section reading classes in mkgmap
 * can be used, but in general we shouldn't add functionality to those classes for the
 * sole use of the display project.
 *
 * @author Steve Ratcliffe
 */

public abstract class CommonCheck extends CommonDisplay {

        private boolean verbose;
        private boolean trace;
        private int errorCount;

        private static String ensureNewLine(String fmt) {
                if (fmt.charAt(fmt.length() - 1) == '\n') return fmt;
                else return fmt + '\n';
        }

        /**
         * Print a message as an error.
         */

        protected void error(String fmt, Object... args) {
                errorCount++;
                System.out.printf("ERROR: " + ensureNewLine(fmt), args);
        }

        /**
         * Print a message as info, enabled by -v
         */

        protected void info(String fmt, Object... args) {
                if (verbose)
                        System.out.printf(ensureNewLine(fmt), args);
        }

        /**
         * Print a message at the trace level, enabled by -vv.
         */

        protected void trace(String fmt, Object... args) {
                if (trace)
                        System.out.printf(ensureNewLine(fmt), args);
        }

        public void setVerbose(boolean verbose) {
                this.verbose = verbose;
        }

        public void setTrace(boolean trace) {
                this.trace = trace;
        }

        protected static void runMain(Class<? extends CommonCheck> cls, String ext, String[] args) {

                if (args.length < 1) {
                        System.err.printf("Usage: %s -v <filename>", cls.getName());
                        System.exit(1);
                }

                boolean verbose = false;
                boolean trace = false;
                List<String> files = new ArrayList<String>();

                for (String arg : args) {
                        if (arg.equals("-v")) {
                                verbose = true;
                        } else if (arg.startsWith("-vv")) {
                                verbose = true;
                                trace = true;
                        } else {
                                files.add(arg);
                        }
                }

                int totalErrors = 0;

                for (String file : files) {
                        System.out.printf("---------- %s --------------------\n", file);
                        try {
                                CommonCheck check = cls.newInstance();
                                check.setVerbose(verbose);
                                check.setTrace(trace);
                                check.display(file, ext);

                                totalErrors += check.errorCount;

                        } catch (InstantiationException e) {
                                e.printStackTrace();
                        } catch (IllegalAccessException e) {
                                e.printStackTrace();
                        }
                }

                // Print out a summary
                if (totalErrors > 0)
                        System.out.printf("ERROR: total number of errors seen: %d\n", totalErrors);
        }
}