turtle-web-editor/TurtleValidator.js

68 lines
2.2 KiB
JavaScript
Raw Normal View History

2014-04-28 11:51:16 +02:00
#!/usr/bin/env node
2015-11-25 13:49:22 +01:00
2018-06-14 13:19:16 +02:00
/*! @license ©2014 Miel Vander Sande - IDLab / imec / Ghent University */
2015-11-25 11:23:16 +01:00
/* Command-line utility to validate Turtle files. */
2014-04-28 11:51:16 +02:00
2018-06-14 13:19:16 +02:00
var fs = require('fs'),
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');
2018-06-14 13:19:16 +02:00
console.log('© 2014 - IDLab - Ghent University - imec');
2014-04-28 15:08:22 +02:00
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>');
};
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-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) {
2018-06-14 13:19:16 +02:00
console.log('ERROR: ' + error);
});
2015-11-25 13:49:22 +01:00
feedback.warnings.forEach(function (warning) {
2018-06-14 13:19:16 +02:00
console.log('WARNING: ' + 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
}