2014-04-28 11:51:16 +02:00
|
|
|
#!/usr/bin/env node
|
2015-11-25 13:49:22 +01:00
|
|
|
|
2015-11-25 11:23:16 +01:00
|
|
|
/*! @license ©2014 Miel Vander Sande - Multimedia Lab / iMinds / Ghent University */
|
|
|
|
/* Command-line utility to validate Turtle files. */
|
2014-04-28 11:51:16 +02:00
|
|
|
|
2014-04-28 15:08:22 +02:00
|
|
|
var N3 = require('n3'),
|
2015-11-25 13:49:22 +01:00
|
|
|
fs = require('fs'),
|
|
|
|
N3Util = N3.Util,
|
2015-11-26 10:00:40 +01:00
|
|
|
http = require('http');
|
2015-11-25 13:49:22 +01:00
|
|
|
url = require('url'),
|
|
|
|
fs = require('fs'),
|
|
|
|
validate = require('./lib/validator.js');
|
2014-04-18 17:11:43 +02:00
|
|
|
|
2014-04-28 15:08:22 +02:00
|
|
|
var help = function () {
|
|
|
|
// In all other cases, let's help the user and return some help
|
|
|
|
console.log('RDF NTriples/Turtle validator using Ruben Verborgh\'s N3 nodejs library');
|
|
|
|
console.log('© 2014 - MMLab - Ghent University - iMinds');
|
|
|
|
console.log('Source code: https://github.com/MMLab/TurtleValidator');
|
|
|
|
console.log('');
|
|
|
|
console.log('Examples:');
|
|
|
|
console.log('');
|
|
|
|
console.log(' $ ttl <path-to-file>');
|
|
|
|
console.log(' $ curl http://data.linkeddatafragments.org/dbpedia -H "accept: text/turtle" | ttl');
|
|
|
|
console.log(' $ ttl http://triples.demo.thedatatank.com/demo.ttl');
|
2017-11-02 14:05:58 +01:00
|
|
|
console.log(' $ ttl <path-to-file> <path-to-second-file>');
|
2014-04-22 11:53:53 +02:00
|
|
|
};
|
|
|
|
|
2015-11-25 13:49:22 +01:00
|
|
|
var args = process.argv.slice(2);
|
|
|
|
|
2017-11-02 14:05:58 +01:00
|
|
|
if (args.length > 0 && (args[0] === "-h" || args[0] === "--help"))
|
2015-11-25 13:49:22 +01:00
|
|
|
return help();
|
|
|
|
|
|
|
|
if (args.length === 0) {
|
|
|
|
validate(process.stdin, showValidation);
|
|
|
|
} else if (args.length > 0) {
|
2017-11-02 14:05:58 +01:00
|
|
|
args.forEach(validateArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
function validateArgument(arg) {
|
2015-11-25 13:49:22 +01:00
|
|
|
// Create a stream from the file, whether it is a local file or a http stream
|
2017-11-02 14:05:58 +01:00
|
|
|
var parsedUrl = url.parse(arg);
|
2015-11-26 10:00:40 +01:00
|
|
|
switch (parsedUrl.protocol) {
|
|
|
|
case 'https:':
|
|
|
|
http = require('https');
|
|
|
|
case 'http:':
|
2015-11-25 13:49:22 +01:00
|
|
|
http.get(parsedUrl.href, function (res) {
|
|
|
|
validate(res, showValidation);
|
|
|
|
}).on('error', function (e) {
|
|
|
|
console.log("Got error: " + e.message);
|
2015-07-14 18:16:11 +02:00
|
|
|
});
|
2015-11-26 10:00:40 +01:00
|
|
|
break;
|
|
|
|
case null:
|
2015-11-25 13:49:22 +01:00
|
|
|
validate(fs.createReadStream(parsedUrl.href), showValidation);
|
2015-11-26 10:00:40 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.log('Cannot access %s: "%s" not supported', parsedUrl.href, parsedUrl.protocol)
|
|
|
|
}
|
2015-11-25 13:49:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Use stdio as an input stream
|
|
|
|
function showValidation(feedback) {
|
|
|
|
feedback.errors.forEach(function (error) {
|
|
|
|
console.log(error);
|
2015-07-14 18:16:11 +02:00
|
|
|
});
|
2015-11-25 13:49:22 +01:00
|
|
|
feedback.warnings.forEach(function (warning) {
|
|
|
|
console.log(warning);
|
2014-04-28 15:08:22 +02:00
|
|
|
});
|
2015-11-25 13:49:22 +01:00
|
|
|
console.log("Validator finished with " + feedback.warnings.length + " warnings and " + feedback.errors.length + " errors.");
|
2014-04-28 15:08:22 +02:00
|
|
|
}
|