diff --git a/TurtleValidator.js b/TurtleValidator.js index 488a28b..87d6fbc 100755 --- a/TurtleValidator.js +++ b/TurtleValidator.js @@ -4,41 +4,8 @@ var N3 = require('n3'), fs = require('fs'), N3Util = N3.Util, http = require('http'), - fs = require('fs'); - -var validateStream = function (turtleStream) { - var parser = N3.Parser(); - var errorCount = 0, warningCount = 0; - var regexp = { - 'dateTime' : /^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[0-1]|0[1-9]|[1-2][0-9])?T(2[0-3]|[0-1][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)??(Z|[+-](?:2[0-3]|[0-1][0-9]):[0-5][0-9])?$/, - 'double' : /[-+]?\d*([.]\d+)?/, - 'float' : /[-+]?\d*[.]\d+/, - 'int' : /^[-+]?(0|[1-9]\d*)$/ - }; - - parser.parse(turtleStream, function(error, triple, prefixes) { - if (error) { - console.log(error); - errorCount++; - } - - if (triple) { - if (N3Util.isLiteral(triple.object)) { - var value = N3Util.getLiteralValue(triple.object); - var type = N3Util.getLiteralType(triple.object); - - type = type.replace('http://www.w3.org/2001/XMLSchema#', ''); - - if (regexp[type] && !regexp[type].test(value)) { - console.log('WARNING: xsd:', type, 'does not validate for literal. {', triple.subject, triple.predicate, triple.object, '}'); - warningCount++; - } - } - } else { - console.log('Validation done:', errorCount, 'errors and', warningCount ,'warnings found.'); - } - }); -}; + fs = require('fs'), + validate = require('./lib/validator.js'); var help = function () { // In all other cases, let's help the user and return some help @@ -57,7 +24,15 @@ if (process.argv[2] && (process.argv[2] === "-h" || process.argv[2] === "--help" help(); } else if (process.argv.length === 2) { // Use stdio as an input stream - validateStream(process.stdin); + validate(process.stdin, function (feedback) { + feedback.errors.forEach(function (error) { + console.log(error); + }); + feedback.warnings.forEach(function (warning) { + console.log(warning); + }); + console.log("Validator finished with " + feedback.warnings.length + " warnings and " + feedback.errors.length + " errors."); + }); } else if (process.argv.length === 3 ) { // Create a stream from the file, whether it is a local file or a http stream var filename = process.argv[2]; diff --git a/lib/validator.js b/lib/validator.js new file mode 100644 index 0000000..ba19f86 --- /dev/null +++ b/lib/validator.js @@ -0,0 +1,41 @@ +var N3 = require('n3'), + N3Util = N3.Util; + +var validate = function (turtleStream, callback) { + var parser = N3.Parser(); + var errorCount = 0, warningCount = 0; + var regexp = { + 'dateTime' : /^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[0-1]|0[1-9]|[1-2][0-9])?T(2[0-3]|[0-1][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)??(Z|[+-](?:2[0-3]|[0-1][0-9]):[0-5][0-9])?$/, + 'double' : /[-+]?\d*([.]\d+)?/, + 'float' : /[-+]?\d*[.]\d+/, + 'int' : /^[-+]?(0|[1-9]\d*)$/ + }; + + var feedback = { warnings : [], errors : []}; + + parser.parse(turtleStream, function(error, triple, prefixes) { + if (error) { + feedback.errors.push(error); + } + + if (triple) { + if (N3Util.isLiteral(triple.object)) { + var value = N3Util.getLiteralValue(triple.object); + var type = N3Util.getLiteralType(triple.object); + + type = type.replace('http://www.w3.org/2001/XMLSchema#', ''); + if (regexp[type] && !regexp[type].test(value)) { + feedback.warnings.push('WARNING: xsd:', type, 'does not validate for literal. {', triple.subject, triple.predicate, triple.object, '}'); + } + } + } else { + callback(feedback); + } + }); +}; + +if (typeof window !== 'undefined') { + window.validate = validate; +} + +module.exports = validate;