-
Notifications
You must be signed in to change notification settings - Fork 11
/
rabel.js
113 lines (93 loc) · 3.41 KB
/
rabel.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Utility Data conversion program.
//
// Test platform for parsers and serializers
//
//
$rdf = require('rdflib');
var fs = require('fs');
var kb = $rdf.graph();
var fetcher = $rdf.fetcher(kb);
var contentType = 'text/turtle';
var base = 'file://' + process.cwd() + '/'
var uri;
var targetDocument;
var check = function(ok, message, status) {
if (!ok) {
console.log("Failed " + status + ": " + message);
process.exit(2);
}
}
var exitMessage = function(message) {
console.log(message);
process.exit(4);
}
var doNext = function(remaining) {
while (remaining.length) {
// console.log("... remaining " + remaining.join(' '));
var command = remaining.shift().split('=');
var left = command[0],
right = command[1];
switch(left) {
case '-base':
base = $rdf.uri.join(right, base)
break;
case '-clear':
kb = $rdf.graph();
break;
case '-dump':
console.log("Serialize " + targetDocument + " as " + contentType);
try {
var out = $rdf.serialize(targetDocument, kb, targetDocument.uri, contentType);
} catch(e) {
exitMessage("Error in serializer: " + e);
}
console.log("Result: " +out);
break;
case '-format':
contentType = right;
break;
case '-in':
targetDocument = $rdf.sym($rdf.uri.join(right, base))
//console.log("Document is " + targetDocument)
fetcher.nowOrWhenFetched(targetDocument, {}, function(ok, body, xhr) {
check(ok, body, xhr? xhr.status : undefined);
console.log("Loaded " + targetDocument);
doNext(remaining);
}); // target, kb, base, contentType, callback
return; // STOP processing at this level
case '-out':
doc = $rdf.sym($rdf.uri.join(right, base));
try {
var out = $rdf.serialize(targetDocument, kb, targetDocument.uri, contentType);
} catch(e) {
exitMessage("Error in serializer: " + e);
}
if (doc.uri.slice(0, 8) !== 'file:///' ) {
exitMessage("Can only write files just now, sorry: " + doc.uri);
}
var fileName = doc.uri.slice(7); //
fs.writeFile(fileName, out, function (err) {
if (err) {
exitMessage("Error writing file <"+right+"> :" + err);
}
console.log("Written " + fileName);
doNext(remaining);
});
return;
case '-size':
console.log(kb.statements.length + ' triples');
break;
case '-version':
console.log("rdflib built: " + $rdf.buildTime);
break;
default:
console.log("Unknown command: " + left);
process.exit(1);
}
}
process.exit(0);
}
doNext(process.argv.slice(2));
// {'forceContentType': 'application/rdfa'}
// http://melvincarvalho.com/
// http://schema.org/Person