Rev 377 |
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.HashMap;
import java.util.List;
import java.util.Map;
import uk.me.parabola.imgfmt.Utils;
import uk.me.parabola.imgfmt.app.Coord;
import test.display.CommonDisplay;
import test.display.check.Log;
/**
* 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 String name
;
/**
* Print a message as an error.
*/
protected void error
(String fmt,
Object...
args) {
Log.
error(fmt, args
);
}
/**
* For testing out a hypothesis. Not counted as an error, but always displayed.
*/
protected void test
(String fmt,
Object...
args) {
Log.
test(fmt, args
);
}
/**
* Print a message as info, enabled by -v
*/
protected void info
(String fmt,
Object...
args) {
Log.
info(fmt, args
);
}
/**
* Print a message at the trace level, enabled by -vv.
*/
protected void trace
(String fmt,
Object...
args) {
Log.
trace(fmt, args
);
}
public void setVerbose
(boolean verbose
) {
Log.
setVerbose(verbose
);
}
public void setTrace
(boolean trace
) {
Log.
setTrace(trace
);
}
protected void setShowLogs
(boolean b
) {
Log.
setShowLogs(b
);
}
public void checkEqual
(Object expected,
Object found,
String fmt,
Object...
args) {
Log.
checkEqual(expected, found, fmt, args
);
}
public void checkNotZero
(int value,
String fmt,
Object...
args) {
Log.
checkNotZero(value, fmt, args
);
}
protected String fmtCoord
(Coord co
) {
if (co ==
null)
return "NOCOORD";
return String.
format("%.3f,%.3f",
Utils.
toDegrees(co.
getLatitude()), Utils.
toDegrees(co.
getLongitude()));
}
/**
* Override this to support different arguments for individual check programs.
* This implementation throws an error if the map is not empty.
* @param args Map of the arguments and values.
*/
public void extraArgs
(Map<String,
String> args
) {
//noinspection LoopStatementThatDoesntLoop
for (String arg : args.
keySet()) {
throw new UnsupportedOperationException("Unrecognised option " + arg
);
}
}
protected static void runMain
(Class<? extends CommonCheck
> cls,
String ext,
String[] args
) {
if (args.
length < 1) {
System.
err.
printf("Usage: %s -v[v] <filename>", cls.
getName());
System.
exit(1);
}
boolean verbose =
false;
boolean trace =
false;
List<String> files =
new ArrayList<>();
Map<String,
String> extraArgs =
new HashMap<>();
for (String arg : args
) {
if (arg.
equals("-v")) {
verbose =
true;
} else if (arg.
startsWith("-vv")) {
verbose =
true;
trace =
true;
} else if (arg.
startsWith("--")) {
String name = arg.
substring(2, arg.
length());
int eq = arg.
indexOf('=');
String val =
"";
if (eq
> 0) {
name = arg.
substring(2, eq
);
val = arg.
substring(eq +
1);
}
System.
out.
printf("ADDING %s=%s\n", name, val
);
extraArgs.
put(name, val
);
} else {
files.
add(arg
);
}
}
for (String file : files
) {
System.
out.
printf("---------- %s --------------------\n", file
);
try {
CommonCheck check = cls.
newInstance();
check.
setName(file
);
check.
setVerbose(verbose
);
check.
setTrace(trace
);
check.
extraArgs(extraArgs
);
check.
display(file, ext
);
} catch (InstantiationException |
IllegalAccessException e
) {
e.
printStackTrace();
}
}
// Print out a summary
if (Log.
isError())
System.
out.
printf("ERROR: total number of errors seen: %d\n", Log.
getErrorCount());
}
public void setName
(String name
) {
this.
name = name
;
}
public String getName
() {
return name
;
}
}