-
Notifications
You must be signed in to change notification settings - Fork 11
/
TurtleValidator.js
executable file
·70 lines (62 loc) · 2.24 KB
/
TurtleValidator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env node
/*! @license ©2014 Miel Vander Sande - IDLab / imec / Ghent University */
/* Command-line utility to validate Turtle files. */
var fs = require('fs'),
http = require('http');
url = require('url'),
fs = require('fs'),
validate = require('./lib/validator.js');
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 - IDLab - Ghent University - imec');
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');
console.log(' $ ttl <path-to-file> <path-to-second-file>');
};
var args = process.argv.slice(2);
if (args.length > 0 && (args[0] === "-h" || args[0] === "--help"))
return help();
if (args.length === 0) {
validate(process.stdin, showValidation);
} else if (args.length > 0) {
args.forEach(validateArgument);
}
function validateArgument(arg) {
// Create a stream from the file, whether it is a local file or a http stream
var parsedUrl = url.parse(arg);
switch (parsedUrl.protocol) {
case 'https:':
http = require('https');
case 'http:':
http.get(parsedUrl.href, function (res) {
validate(res, showValidation);
}).on('error', function (e) {
console.log("Got error: " + e.message);
process.exit(1);
});
break;
case null:
validate(fs.createReadStream(arg), showValidation);
break;
default:
console.log('Cannot access %s: "%s" not supported', parsedUrl.href, parsedUrl.protocol)
process.exit(1);
}
}
// Use stdio as an input stream
function showValidation(feedback) {
feedback.errors.forEach(function (error) {
console.log('ERROR: ' + error);
});
feedback.warnings.forEach(function (warning) {
console.log('WARNING: ' + warning);
});
console.log("Validator finished with " + feedback.warnings.length + " warnings and " + feedback.errors.length + " errors.");
process.exit(feedback.errors.length > 0 ? 2 : 0);
}