private final ValidationService validationService;
public BelCheck(String[] args) {
super(args);
final SimpleOutput reportable = new SimpleOutput();
reportable.setErrorStream(System.err);
reportable.setOutputStream(System.out);
setReportable(reportable);
printApplicationInfo("BEL Check Utility");
initializeSystemConfiguration();
validator = createValidator();
converter = createConverter();
belValidator = new BELValidatorServiceImpl();
belConverter = new BELConverterServiceImpl();
final CacheableResourceService cache =
new DefaultCacheableResourceService();
final CacheLookupService cacheLookup = new DefaultCacheLookupService();
annoDefService = new DefaultAnnotationDefinitionService(
cache, cacheLookup);
final NamespaceIndexerService nsindexer =
new NamespaceIndexerServiceImpl();
final NamespaceService nsService = new DefaultNamespaceService(
cache, cacheLookup, nsindexer);
final SemanticService semantics = new SemanticServiceImpl(nsService);
final AnnotationService annotationService =
new DefaultAnnotationService();
validationService = new DefaultValidationService(
nsService, semantics, annotationService);
boolean pedantic = hasOption(PEDANTIC_OPTION);
boolean permissive = hasOption(PERMISSIVE_OPTION);
boolean verbose = hasOption(StandardOptions.LONG_OPT_VERBOSE);
boolean quiet = hasOption(QUIET_OPTION);
boolean summary = hasOption(SUMMARY_OPTION);
if (pedantic && permissive) {
fatal(CHECK_PEDANTIC_PERMISSIVE_ERROR);
}
List<String> fileArgs = getExtraneousArguments();
if (!hasItems(fileArgs)) {
// print out the usage if no arguments were given
printUsage();
reportable.error("\n");
reportable.error(NO_DOCUMENT_FILES);
end();
}
if (fileArgs.size() > 1) {
fatal("Only a single document can be specified.");
}
String fileArg = fileArgs.get(0);
if (!isBELDocument(fileArg)) {
final String error = fileArg + " is not a BEL document.";
reportable.error(error);
bail(ExitCode.GENERAL_FAILURE);
return;
}
File file = new File(fileArg);
if (!readable(file)) {
final String error = Strings.INPUT_FILE_UNREADABLE + file;
reportable.error(error);
bail(ExitCode.GENERAL_FAILURE);
return;
}
try {
final String abspath = file.getAbsolutePath();
if (verbose) {
reportable.output("Validating BEL Document: " + abspath);
}
int numWarnings = 0;
int numErrors = 0;
final Document document;
if (isXBEL(file)) {
List<ValidationError> validationErrors = validator
.validateWithErrors(file);
// if validation errors exist then report and fail document
if (hasItems(validationErrors)) {
numErrors += validationErrors.size();
if (!quiet) {
reportValidationError(validationErrors);
}
if (summary) {
printSummary(fileArg, numWarnings, numErrors);
}
bail(ExitCode.VALIDATION_FAILURE);
return;
}
document = converter.toCommon(file);
} else if (isBELScript(file)) {
List<ValidationError> validationErrors =
new ArrayList<ValidationError>();
BELParseResults results = belValidator
.validateBELScript(file);
// if validation errors exist then report and fail document
if (hasItems(results.getSyntaxErrors())) {
for (BELParseErrorException syntaxError : results
.getSyntaxErrors()) {
validationErrors.add(new ValidationError(file
.getAbsolutePath(), syntaxError
.getMessage(),
syntaxError.getLine(), syntaxError
.getCharacter()));
}
numErrors += validationErrors.size();
if (!quiet) {
reportValidationError(validationErrors);
}
if (summary) {
printSummary(fileArg, numWarnings, numErrors);
}
bail(ExitCode.VALIDATION_FAILURE);
return;
}
document = belConverter.toCommon(results.getDocument());
} else {
// unreachable; checked with isBelDocument
fatal("Unsupported document type");
return;
}
int numAnnoDefErrors =
validateAnnotationDefinitions(document, quiet, permissive,
verbose);
if (permissive) {
numWarnings += numAnnoDefErrors;
} else {
numErrors += numAnnoDefErrors;
}
ValidationResult vr = validationService.validate(document);
for (String e : vr.getErrors()) {
if (permissive) {
numWarnings++;
if (!quiet) {
reportable.warning("VALIDATION WARNING: " + e);
}
} else {
numErrors++;
if (!quiet) {
reportable.error("VALIDATION ERROR: " + e);
}
}
}
for (String w : vr.getWarnings()) {
if (pedantic) {
numErrors++;
if (!quiet) {
reportable.error("VALIDATION ERROR: " + w);
}
} else {
numWarnings++;
if (!quiet) {
reportable.warning("VALIDATION WARNING: " + w);
}
}
}
if (summary) {
printSummary(fileArg, numWarnings, numErrors);
}
if (numErrors == 0) {
reportable.output(ALL_DOCUMENTS_PASSED_VALIDATION);
bail(ExitCode.SUCCESS);
} else {
bail(ExitCode.VALIDATION_FAILURE);
}
} catch (Exception e) {
reportable.error("Failed to import BEL Document.");
reportable.error("Reason: " + e.getMessage());
bail(ExitCode.GENERAL_FAILURE);
}
}