Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions apache-jena/cmd-maker
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,7 +36,6 @@ jena.schemagen
arq.rdfdiff
arq.sparql
arq.arq
arq.juuid
arq.rsparql
arq.rset
arq.qparse
Expand Down
27 changes: 15 additions & 12 deletions jena-cmds/src/main/java/org/apache/jena/cmd/CmdMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,35 +62,38 @@ 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 )
System.exit(0);
return 0;
}


protected abstract void exec();

protected final void mainMethod() {
Expand Down
82 changes: 37 additions & 45 deletions jena-cmds/src/main/java/riotcmd/CmdLangParse.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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() ;
Expand All @@ -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();
Expand All @@ -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;
}

Expand Down
41 changes: 28 additions & 13 deletions jena-cmds/src/main/java/riotcmd/ErrorHandlerCLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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) ;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -382,6 +389,9 @@ void setXMLBase(String b) {

private Map<Statement, Resource> statement2res;

// RDF 1.2 triple terms.
private boolean hasTripleTerms = false;

// ITS (text direction)
// The data has text direction literals.
private boolean hasTextDirectionLiterals = false;
Expand All @@ -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 ) {
Expand Down Expand Up @@ -457,8 +477,8 @@ private void wObjStar() {
* '<' propName idAttr? parseLiteral '>' literal '</' propName '>' |
* '<' propName idAttr? parseResource '>' propertyElt* '</' propName '>' |
* '<' propName idRefAttr? bagIdAttr? propAttr* '/>' |
* '<' propName idAttr? parseDamlCollection '>' obj* '</' propName '>' [daml.2]
* '<' propName idAttr? parseTriple '>' obj '</' propName '>' [RDF 1.2
* '<' propName idAttr? parseDamlCollection '>' obj* '</' propName '>' [daml.2 - unsupported]
* '<' propName idAttr? parseTriple '>' obj '</' propName '>' [RDF ]
*
* parseDamlCollection ::= ' parseType="rdf:collection"'
*
Expand Down Expand Up @@ -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)));
}
Expand Down