Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudenw committed Nov 28, 2017
2 parents 041a261 + 79703f1 commit 155edb7
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ private RDFParserBuilder langTagForm(LangTagForm form) {
* </ul>
* <p>
* See also {@link #errorHandler(ErrorHandler)} to control the output. The default is to log.
* Thsi can also be used to turn warnings into exceptions.
* This can also be used to turn warnings into exceptions.
*/
public RDFParserBuilder checking(boolean flag) { this.checking = Optional.of(flag) ; return this; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public class ErrorHandlerFactory
/** Ignores warnings, throws exceptions for errors */
public static ErrorHandler errorHandlerSimple() { return new ErrorHandlerSimple() ; }

/** Logs warnings and errors while tracking the counts of each and optionally throwing exceptions when errors and/or warnings are encounted */
public static ErrorHandlerTracking errorHandlerTracking(Logger log, boolean failOnError, boolean failOnWarning) { return new ErrorHandlerTracking(log, failOnError, failOnWarning); }

/**
* An error handler that throws a {@link RiotParseException}, hence it
* exposes the details of errors.
Expand Down Expand Up @@ -221,6 +224,64 @@ public void fatal(String message, long line, long col) {
}
}

/** An error handler that logs message for errors and warnings and throw exceptions on either */
public static class ErrorHandlerTracking extends ErrorLogger implements ErrorHandler {
private final boolean failOnError, failOnWarning;
private long errorCount, warningCount;

public ErrorHandlerTracking(Logger log, boolean failOnError, boolean failOnWarning) {
super(log) ;

this.failOnError = failOnError;
this.failOnWarning = failOnWarning;
}

/** report a warning */
@Override
public void warning(String message, long line, long col) {
logWarning(message, line, col) ;
this.warningCount++;
if (this.failOnWarning)
throw new RiotException(fmtMessage(message, line, col)) ;
}

/** report an error */
@Override
public void error(String message, long line, long col) {
logError(message, line, col) ;
this.errorCount++;
if (this.failOnError)
throw new RiotException(fmtMessage(message, line, col)) ;
}

@Override
public void fatal(String message, long line, long col) {
logFatal(message, line, col) ;
this.errorCount++;
throw new RiotException(fmtMessage(message, line, col)) ;
}

public long getErrorCount() {
return this.errorCount;
}

public long getWarningCount() {
return this.warningCount;
}

public boolean hadErrors() {
return this.errorCount > 0;
}

public boolean hadWarnings() {
return this.warningCount > 0;
}

public boolean hadIssues() {
return hadErrors() || hadWarnings();
}
}

/** An error handler that logs messages for errors and warnings and attempt to carry on */
private static class ErrorHandlerWarning extends ErrorLogger implements ErrorHandler {
public ErrorHandlerWarning(Logger log)
Expand Down
10 changes: 10 additions & 0 deletions jena-cmds/src/main/java/arq/cmdline/ModLangParse.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class ModLangParse extends ModBase
private ArgDecl argSkip = new ArgDecl(ArgDecl.NoValue, "skip") ;
private ArgDecl argNoSkip = new ArgDecl(ArgDecl.NoValue, "noSkip") ;
private ArgDecl argStop = new ArgDecl(ArgDecl.NoValue, "stopOnError", "stoponerror", "stop") ;
private ArgDecl argStopWarn = new ArgDecl(ArgDecl.NoValue, "stopOnWarning", "stoponwarning", "stop-warnings") ;

private ArgDecl argBase = new ArgDecl(ArgDecl.HasValue, "base") ;

Expand All @@ -58,6 +59,7 @@ public class ModLangParse extends ModBase
private boolean explicitNoCheck = false ;
private boolean skipOnBadTerm = false ;
private boolean stopOnBadTerm = false ;
private boolean stopOnWarnings = false ;
private boolean bitbucket = false ;
private boolean strict = false ;
private boolean validate = false ;
Expand All @@ -78,6 +80,7 @@ public void registerWith(CmdGeneral cmdLine) {
// cmdLine.add(argSkip, "--noSkip", "Skip (do not output) triples failing the RDF term tests") ;
// cmdLine.add(argNoSkip, "--skip", "Include triples failing the RDF term tests (not recommended)") ;
cmdLine.add(argStop, "--stop", "Stop parsing on encountering a bad RDF term") ;
cmdLine.add(argStopWarn,"--stop-warnings", "Stop parsing on encountering a warning") ;
}

@Override
Expand Down Expand Up @@ -122,6 +125,9 @@ public void processArgs(CmdArgModule cmdLine) {

if ( cmdLine.contains(argStop) )
stopOnBadTerm = true ;

if ( cmdLine.contains(argStopWarn) )
stopOnWarnings = true;

if ( cmdLine.contains(argSink) )
bitbucket = true ;
Expand Down Expand Up @@ -161,6 +167,10 @@ public boolean skipOnBadTerm() {
public boolean stopOnBadTerm() {
return stopOnBadTerm ;
}

public boolean stopOnWarnings() {
return stopOnWarnings ;
}

public boolean toBitBucket() {
return bitbucket ;
Expand Down
35 changes: 21 additions & 14 deletions jena-cmds/src/main/java/riotcmd/CmdLangParse.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.apache.jena.riot.process.inf.InfFactory ;
import org.apache.jena.riot.process.inf.InferenceSetupRDFS ;
import org.apache.jena.riot.system.* ;
import org.apache.jena.riot.system.ErrorHandlerFactory.ErrorHandlerTracking;
import org.apache.jena.riot.tokens.Tokenizer ;
import org.apache.jena.riot.tokens.TokenizerFactory ;
import org.apache.jena.sparql.core.DatasetGraph ;
Expand Down Expand Up @@ -104,14 +105,16 @@ static class ParseRecord {
final long triples;
final long quads;
final long tuples = 0;
final ErrorHandlerTracking errHandler;

public ParseRecord(String filename, boolean successful, long timeMillis,
long countTriples, long countQuads) {
long countTriples, long countQuads, ErrorHandlerTracking errHandler) {
this.filename = filename;
this.success = successful;
this.timeMillis = timeMillis;
this.triples = countTriples;
this.quads = countQuads;
this.errHandler = errHandler;
}
}

Expand Down Expand Up @@ -173,6 +176,8 @@ protected void exec() {
long totalTriples = 0;
long totalQuads = 0;
long totalTuples = 0;
long totalErrors = 0;
long totalWarnings = 0;
boolean allSuccessful = true;

for ( ParseRecord pRec : outcomes ) {
Expand All @@ -181,9 +186,11 @@ protected void exec() {
totalTriples += pRec.triples;
totalQuads += pRec.quads;
totalTuples += pRec.tuples;
totalErrors += pRec.errHandler.getErrorCount();
totalWarnings += pRec.errHandler.getWarningCount();
allSuccessful = allSuccessful & pRec.success;
}
output("Total", true, totalTriples, totalQuads, totalTuples, totalMillis);
output("Total", true, totalTriples, totalQuads, totalTuples, totalMillis, totalErrors, totalWarnings);
}
} finally {
if ( outputWrite != System.out )
Expand All @@ -195,7 +202,7 @@ protected void exec() {

// exit(1) if there were any errors.
for ( ParseRecord pr : outcomes ) {
if ( ! pr.success )
if ( ! pr.success || pr.errHandler.hadIssues() )
throw new CmdException();
}
}
Expand Down Expand Up @@ -237,14 +244,9 @@ protected ParseRecord parseRIOT(RDFParserBuilder builder, /*Info for the Process
checking = false;
builder.checking(checking);

ErrorHandler errHandler = ErrorHandlerFactory.errorHandlerWarn ;
if ( checking ) {
if ( modLangParse.stopOnBadTerm() )
errHandler = ErrorHandlerFactory.errorHandlerStd ;
else
// Try to go on if possible. This is the default behaviour.
errHandler = ErrorHandlerFactory.errorHandlerWarn ;
}
ErrorHandlerTracking errHandler = ErrorHandlerFactory.errorHandlerTracking(ErrorHandlerFactory.stdLogger,
modLangParse.stopOnBadTerm(),
modLangParse.stopOnWarnings());

if ( modLangParse.skipOnBadTerm() ) {
// skipOnBadterm - this needs collaboration from the parser.
Expand Down Expand Up @@ -287,7 +289,7 @@ protected ParseRecord parseRIOT(RDFParserBuilder builder, /*Info for the Process
sink.finish() ;
long x = modTime.endTimer() ;
// TEMP
ParseRecord outcome = new ParseRecord(filename, successful, x, sink.countTriples(), sink.countQuads());
ParseRecord outcome = new ParseRecord(filename, successful, x, sink.countTriples(), sink.countQuads(), errHandler);
return outcome;
}

Expand Down Expand Up @@ -336,10 +338,11 @@ protected Tokenizer makeTokenizer(InputStream in) {
protected void output(ParseRecord rtn) {
output(rtn.filename, rtn.success,
rtn.triples, rtn.quads, rtn.tuples,
rtn.timeMillis) ;
rtn.timeMillis, rtn.errHandler.getErrorCount(),
rtn.errHandler.getWarningCount()) ;
}

protected void output(String label, boolean success, long numberTriples, long numberQuads, long numberTuples, long timeMillis) {
protected void output(String label, boolean success, long numberTriples, long numberQuads, long numberTuples, long timeMillis, long errorCount, long warningCount) {
double timeSec = timeMillis/1000.0 ;
long total = numberTriples + numberQuads + numberTuples;
StringBuilder sb = new StringBuilder();
Expand All @@ -355,6 +358,10 @@ protected void output(String label, boolean success, long numberTriples, long nu
} else {
appendFmt(sb, "%s : (No Output)", label) ;
}
if ( errorCount > 0 || warningCount > 0 ) {
appendFmt(sb," : %,d %s", errorCount, "errors");
appendFmt(sb," : %,d %s", warningCount, "warnings");
}
System.err.println(sb.toString());
}

Expand Down

0 comments on commit 155edb7

Please sign in to comment.