diff --git a/apache-jena/cmd-maker b/apache-jena/cmd-maker index 4c957871c25..92b497babdf 100755 --- a/apache-jena/cmd-maker +++ b/apache-jena/cmd-maker @@ -17,7 +17,7 @@ ## limitations under the License. # Not xloader -## xloader (forerly "TDB 1 tdbloader2") is slightly different. +## xloader (formerly, "TDB 1 tdbloader2") is slightly different. ## The main program is not a java program ## It is split into several scripts that leverage a mixture of ## POSIX and java tools and should be maintained separately @@ -36,7 +36,6 @@ jena.schemagen arq.rdfdiff arq.sparql arq.arq -arq.juuid arq.rsparql arq.rset arq.qparse diff --git a/jena-cmds/src/main/java/org/apache/jena/cmd/CmdMain.java b/jena-cmds/src/main/java/org/apache/jena/cmd/CmdMain.java index 93340f4991a..23f01b35d82 100644 --- a/jena-cmds/src/main/java/org/apache/jena/cmd/CmdMain.java +++ b/jena-cmds/src/main/java/org/apache/jena/cmd/CmdMain.java @@ -62,27 +62,31 @@ public void mainAndExit() /** Run command */ public int mainRun(boolean exitOnSuccess, boolean exitOnFailure) { try { mainMethod(); } - catch (TerminationException ex) { System.exit(ex.getCode()); } - catch (IllegalArgumentException ex) - { + catch (TerminationException ex) { + int rc = ex.getCode(); + if ( exitOnFailure ) + System.exit(rc); + return rc; + } + catch (IllegalArgumentException ex) { ex.printStackTrace(System.err); - if ( exitOnFailure ) System.exit(1); + if ( exitOnFailure ) + System.exit(1); return 1; } - catch (CmdException ex) - { + catch (CmdException ex) { if ( ex.getMessage() != null && ex.getMessage().length() > 0 ) System.err.println(ex.getMessage()); - //ex.printStackTrace(); if ( ex.getCause() != null ) ex.getCause().printStackTrace(System.err); - if ( exitOnFailure ) System.exit(1); + if ( exitOnFailure ) + System.exit(1); return 1; } - catch (Exception ex) - { + catch (Exception ex) { ex.printStackTrace(System.err); - if ( exitOnFailure ) System.exit(2); + if ( exitOnFailure ) + System.exit(2); return 2; } if ( exitOnSuccess ) @@ -90,7 +94,6 @@ public int mainRun(boolean exitOnSuccess, boolean exitOnFailure) { return 0; } - protected abstract void exec(); protected final void mainMethod() { diff --git a/jena-cmds/src/main/java/riotcmd/CmdLangParse.java b/jena-cmds/src/main/java/riotcmd/CmdLangParse.java index 7f7e0f832ab..55d8d195493 100644 --- a/jena-cmds/src/main/java/riotcmd/CmdLangParse.java +++ b/jena-cmds/src/main/java/riotcmd/CmdLangParse.java @@ -40,6 +40,7 @@ import org.apache.jena.cmd.ArgDecl; import org.apache.jena.cmd.CmdException; import org.apache.jena.cmd.CmdMain; +import org.apache.jena.cmd.TerminationException; import org.apache.jena.irix.IRIException; import org.apache.jena.irix.IRIs; import org.apache.jena.irix.IRIxResolver; @@ -126,27 +127,13 @@ protected interface PostParseHandler { void postParse(); } - protected static class ParseRecord { - // Display name (filename as given on the command line) - final String filename; - // Resolved filename as a URL string. - final String sourceURL; - final boolean success; - final long timeMillis; - final long triples; - final long quads; - final long tuples = 0; - final ErrorHandlerCLI errHandler; - - public ParseRecord(String filename, String sourceURL, boolean successful, long timeMillis, long countTriples, long countQuads, - ErrorHandlerCLI errHandler) { - this.filename = filename; - this.sourceURL = sourceURL; - this.success = successful; - this.timeMillis = timeMillis; - this.triples = countTriples; - this.quads = countQuads; - this.errHandler = errHandler; + protected record ParseRecord(String filename, String sourceURL, boolean success, long timeMillis, + long triples, long quads, long tuples, ErrorHandlerCLI errHandler) { + // Default tuples to 0 + ParseRecord(String filename, String sourceURL, boolean success, long timeMillis, + long triples, long quads, + ErrorHandlerCLI errHandler) { + this(filename, sourceURL, success, timeMillis,triples, quads, 0L, errHandler); } } @@ -275,9 +262,11 @@ protected void exec() { } // exit(1) if there were any errors. + // pr.success is true if the indicates the parser completed it's run + // (no failure-on-error or unexpected exceptions). for ( ParseRecord pr : outcomes ) { - if ( !pr.success || pr.errHandler.hadIssues() ) - throw new CmdException(); + if ( !pr.success || pr.errHandler.hadErrors() ) + throw new TerminationException(1); } } @@ -364,14 +353,6 @@ protected ParseRecord parseRIOT(RDFParserBuilder builder, String filenameLabel, if ( passRelativeURIs ) stopOnWarnings = false; - ErrorHandlerCLI errHandler = new ErrorHandlerCLI - (ErrorHandlerFactory.stdLogger - , passRelativeURIs // Silent warnings if allowing relative URIs. - , true // Fail on error - , stopOnWarnings // Fail on warnings - ); - builder.errorHandler(errHandler); - // Make into a cmd flag. (input and output subflags?) final boolean labelsAsGiven = false; // NodeToLabel labels = SyntaxLabels.createNodeToLabel() ; @@ -381,20 +362,30 @@ protected ParseRecord parseRIOT(RDFParserBuilder builder, String filenameLabel, builder.labelToNode(LabelToNode.createUseLabelAsGiven()); // Build parser output additions. - StreamRDF s = parserOutputStream; - if ( setupRDFS != null ) { - // Remove literals as subjects - s = RDFSFactory.removeGeneralizedRDF(s); - // Generate RDFS (this feeds into the stream created above). - s = RDFSFactory.streamRDFS(s, setupRDFS); - // Parser sends data to RDFS, which goes to the filter, then to parserOutputStream + StreamRDFCounting parserOut; + { + StreamRDF s = parserOutputStream; + if ( setupRDFS != null ) { + // Remove literals as subjects + s = RDFSFactory.removeGeneralizedRDF(s); + // Generate RDFS (this feeds into the stream created above). + s = RDFSFactory.streamRDFS(s, setupRDFS); + // Parser sends data to RDFS, which goes to the filter, then to parserOutputStream + } + // If added here, count is quads and triples seen in the input. + if ( modLangParse.mergeQuads() ) + s = new QuadsToTriples(s); + parserOut = StreamRDFLib.count(s); + s = null; } - // If added here, count is quads and triples seen in the input. - if ( modLangParse.mergeQuads() ) - s = new QuadsToTriples(s); - StreamRDFCounting parserOut = StreamRDFLib.count(s); - s = null; + ErrorHandlerCLI errHandler = ErrorHandlerCLI.errorHandlerTracking(ErrorHandlerFactory.stdLogger, + passRelativeURIs, // Silent warnings if allowing relative URIs. + true, // Fail on error + stopOnWarnings, // Fail on warnings + ()->parserOut.finish() // Flush to align log messages + ); + builder.errorHandler(errHandler); boolean successful = true; modTime.startTimer(); @@ -412,8 +403,9 @@ protected ParseRecord parseRIOT(RDFParserBuilder builder, String filenameLabel, successful = false; } parserOut.finish(); - long x = modTime.endTimer(); - ParseRecord outcome = new ParseRecord(filenameLabel, sourceURL, successful, x, parserOut.countTriples(), parserOut.countQuads(), errHandler); + long elapsedTime_ms = modTime.endTimer(); + ParseRecord outcome = new ParseRecord(filenameLabel, sourceURL, successful, elapsedTime_ms, + parserOut.countTriples(), parserOut.countQuads(), errHandler); return outcome; } diff --git a/jena-cmds/src/main/java/riotcmd/ErrorHandlerCLI.java b/jena-cmds/src/main/java/riotcmd/ErrorHandlerCLI.java index 5b84ccb8c10..a8e4614b6f8 100644 --- a/jena-cmds/src/main/java/riotcmd/ErrorHandlerCLI.java +++ b/jena-cmds/src/main/java/riotcmd/ErrorHandlerCLI.java @@ -32,21 +32,29 @@ */ class ErrorHandlerCLI implements ErrorHandler { - /** Logs warnings and errors while tracking the counts of each and optionally throwing exceptions when errors and/or warnings are encounted */ - static ErrorHandlerCLI errorHandlerTracking(Logger log, boolean silentWarnings, boolean failOnError, boolean failOnWarning) - { return new ErrorHandlerCLI(log, silentWarnings, failOnError, failOnWarning); } + /** + * Logs warnings and errors while tracking the counts of each and optionally + * throwing exceptions when errors and/or warnings are encountered. + */ + static ErrorHandlerCLI errorHandlerTracking(Logger log, boolean silentWarnings, + boolean failOnError, boolean failOnWarning, Runnable onMessage) { + return new ErrorHandlerCLI(log, silentWarnings, failOnError, failOnWarning, onMessage); + } private final Logger log ; private final boolean silentWarnings; private final boolean failOnError; private final boolean failOnWarning; - private long errorCount, warningCount; + private final Runnable onMessage; + private long errorCount = 0; + private long warningCount = 0; - public ErrorHandlerCLI(Logger log, boolean silentWarnings, boolean failOnError, boolean failOnWarning) { + private ErrorHandlerCLI(Logger log, boolean silentWarnings, boolean failOnError, boolean failOnWarning, Runnable onMessage) { this.log = log ; this.silentWarnings = silentWarnings; this.failOnError = failOnError; this.failOnWarning = failOnWarning; + this.onMessage = onMessage; } /** report a warning */ @@ -91,25 +99,32 @@ boolean hadWarnings() { return this.warningCount > 0; } - boolean hadIssues() { - return hadErrors() || hadWarnings(); + private void onLogMessage() { + if ( onMessage != null ) + onMessage.run(); } /** report a warning */ private void logWarning(String message, long line, long col) { - if ( log != null ) - log.warn(fmtMessage(message, line, col)) ; + if ( log == null ) + return; + onLogMessage(); + log.warn(fmtMessage(message, line, col)) ; } /** report an error */ private void logError(String message, long line, long col) { - if ( log != null ) - log.error(fmtMessage(message, line, col)) ; + if ( log == null ) + return; + onLogMessage(); + log.error(fmtMessage(message, line, col)) ; } /** report a catastrophic error */ private void logFatal(String message, long line, long col) { - if ( log != null ) - logError(message, line, col) ; + if ( log == null ) + return; + onLogMessage(); + logError(message, line, col) ; } } diff --git a/jena-core/src/main/java/org/apache/jena/rdfxml/xmloutput/impl/Unparser.java b/jena-core/src/main/java/org/apache/jena/rdfxml/xmloutput/impl/Unparser.java index f2e4879b3ca..a97d5ebcb9c 100644 --- a/jena-core/src/main/java/org/apache/jena/rdfxml/xmloutput/impl/Unparser.java +++ b/jena-core/src/main/java/org/apache/jena/rdfxml/xmloutput/impl/Unparser.java @@ -181,7 +181,9 @@ class Unparser { while (ss.hasNext()) { Statement s = ss.nextStatement(); RDFNode obj = s.getObject(); - processRDFNode(obj, false); + boolean foundTripleTerm = processRDFNode(obj, false); + if ( foundTripleTerm ) + hasTripleTerms = true; } } finally { ss.close(); @@ -228,7 +230,8 @@ public Resource accept(Resource o) { } } - private void processRDFNode(RDFNode obj, boolean inTripleTerm) { + // Return true for "RDF 1.2 triple term found" + private boolean processRDFNode(RDFNode obj, boolean inTripleTerm) { if ( obj.isResource() ) { Resource r = obj.asResource(); increaseObjectCount(r); @@ -238,29 +241,33 @@ private void processRDFNode(RDFNode obj, boolean inTripleTerm) { } if ( obj.isStatementTerm() ) { processTripleTerm(obj.asStatementTerm()); - return; + return true; } if ( obj.isLiteral() ) { if ( obj.asLiteral().getBaseDirection() != null ) { - hasTextDirectionLiterals = true; - // We will need a namespace prefix. - // Ideally, there is one in the model can be added to rdf:RDF as usual. - itsPrefix = model.getNsURIPrefix(ITS.uri); - if ( itsPrefix == null ) { - // Not in model. - // This is added to rdf:RDF, along with [its]:version - itsInsertNs = true; - itsPrefix = syntheticNamespaceForITS(model); - prettyWriter.setNsPrefix(itsPrefix, ITS.uri); + if ( ! hasTextDirectionLiterals ) { + // Haven't encountered a text direction so far. + hasTextDirectionLiterals = true; + // We will need a namespace prefix. + // Ideally, there is one in the model can be added to rdf:RDF as usual. + itsPrefix = model.getNsURIPrefix(ITS.uri); + if ( itsPrefix == null ) { + // Not in model. + // This is added to rdf:RDF, along with [its]:version + itsInsertNs = true; + itsPrefix = syntheticNamespaceForITS(model); + prettyWriter.setNsPrefix(itsPrefix, ITS.uri); + } } } + // Does not imply rdf:version } + return false; } private void processTripleTerm(StatementTerm sTerm) { Statement tripleTerm = sTerm.asStatementTerm().getStatement(); processTripleTermsOneLevel(tripleTerm); - if ( tripleTerm.getObject().isStatementTerm() ) { StatementTerm sTerm2 = tripleTerm.getObject().asStatementTerm(); processTripleTerm(sTerm2); @@ -271,7 +278,7 @@ private void processTripleTermsOneLevel(Statement stmt) { RDFNode ttSubj = stmt.getSubject(); if ( ttSubj.isAnon() ) { Resource r = ttSubj.asResource(); - // XXX !!! twice to make sure it is printed and not compacted. + // !!! increaseObjectCount twice to make sure it is printed and not compacted. if ( r.isAnon() ) increaseObjectCount(r); increaseObjectCount(r); @@ -382,6 +389,9 @@ void setXMLBase(String b) { private Map statement2res; + // RDF 1.2 triple terms. + private boolean hasTripleTerms = false; + // ITS (text direction) // The data has text direction literals. private boolean hasTextDirectionLiterals = false; @@ -408,6 +418,16 @@ private void wRDF() { print(prettyWriter.rdfEl("RDF")); indentPlus(); printNameSpaceDefn(); + + // Code ready to be enabled. + if ( false && hasTripleTerms ) { + indentPlus(); + tab(); + print(prettyWriter.rdfEl("version")); + print(format("=%s", q("1.2"))); + indentMinus(); + } + if ( hasTextDirectionLiterals ) { indentPlus(); if ( itsInsertNs ) { @@ -457,8 +477,8 @@ private void wObjStar() { * '<' propName idAttr? parseLiteral '>' literal '' | * '<' propName idAttr? parseResource '>' propertyElt* '' | * '<' propName idRefAttr? bagIdAttr? propAttr* '/>' | - * '<' propName idAttr? parseDamlCollection '>' obj* '' [daml.2] - * '<' propName idAttr? parseTriple '>' obj '' [RDF 1.2 + * '<' propName idAttr? parseDamlCollection '>' obj* '' [daml.2 - unsupported] + * '<' propName idAttr? parseTriple '>' obj '' [RDF ] * * parseDamlCollection ::= ' parseType="rdf:collection"' * @@ -779,7 +799,7 @@ private void wLangAndBaseDirection(Literal literal) { // } print(" xml:lang=" + q(lang)); if ( baseDir != null ) { - // done in rdf:RDFs + // done in rdf:RDF //print(format(" %s:%s=%s", itsPrefix, ITS.version, q("2.0"))); print(format(" %s:%s=%s", itsPrefix, ITS.dir, q(baseDir))); }