forked from linkeddata/rdflib.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.js
More file actions
155 lines (133 loc) · 4.27 KB
/
data.js
File metadata and controls
155 lines (133 loc) · 4.27 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// General read-parse-serialize data tool
//
// Original motivation: Test rdfa parser
//
// See http://www.w3.org/TR/rdfa-syntax/ etc
//
const $rdf = require('../../lib')
const fs = require('fs')
const nock = require('nock')
const kb = $rdf.graph()
const fetcher = $rdf.fetcher(kb)
var contentType = 'text/turtle'
var base = 'http://localhost/'
var server
var fileName
var targetDocument // = $rdf.sym(base + 'stdin') // defaul URI of test data
var inDocument
var outDocument
var check = function (ok, message, status) {
if (!ok) {
console.log('Failed ' + status + ': ' + message)
process.exit(2)
}
}
var stackString = function (e) {
var str = '' + e + '\n'
if (!e.stack) {
return str + 'No stack available.\n'
}
var lines = e.stack.toString().split('\n')
var toprint = []
for (var i = 0; i < lines.length; i++) {
var line = lines[i]
if (line.indexOf('ecmaunit.js') > -1) {
// remove useless bit of traceback
break
}
if (line.charAt(0) == '(') {
line = 'function' + line
}
var chunks = line.split('@')
toprint.push(chunks)
}
// toprint.reverse(); No - I prefer the latest at the top by the error message -tbl
for (var i = 0; i < toprint.length; i++) {
str += ' ' + toprint[i][1] + '\n ' + toprint[i][0]
}
return str
}
var exitMessage = function (message) {
console.log(message)
process.exit(4)
}
var doNext = async function (remaining) {
while (remaining.length) {
// console.log("... remaining " + remaining.join(' '))
var command = remaining.shift().split('=')
var left = command[0],
right = command[1]
let doc
switch (left) {
case '-base':
base = $rdf.uri.join(right, base)
break
case '-clear':
kb = $rdf.graph()
break
case '-format':
contentType = right
break
case '-in':
let targetUri = $rdf.uri.join(right, base)
inDocument = $rdf.sym(targetUri)
let docContents = fs.readFileSync(right, 'utf8')
server = nock('http://localhost').get('/' + right).reply(200, docContents)
fetcher.nowOrWhenFetched(inDocument, {}, function (ok, body, xhr) {
check(ok, body, xhr ? xhr.status : undefined)
// console.log(kb.statementsMatching().map(ea => ea.toString() + ' why:' + ea.why))
doNext(remaining)
}) // target, kb, base, contentType, callback
return // STOP processing at this level
case '-out':
// there is an issue with jsonld. The test returns an error : process exit 1. CI fails
// await is only for jsonld serialize.
try {
// Flags:
// - 'z' used historically for RDF/XML code path
// - For Turtle outputs, use 'o' to avoid dotted local qnames and match reference fixtures
var options = {}
if ((contentType || '').indexOf('turtle') >= 0) {
options.flags = 'o'
} else if ((contentType || '').indexOf('rdf+xml') >= 0) {
options.flags = 'z'
}
var out = await $rdf.serialize(inDocument, kb, inDocument.uri, contentType, undefined, options)
} catch(e) {
exitMessage('Error in serializer: ' + e + stackString(e))
}
if (!right){
console.log('Result: ' + out)
doNext(remaining)
return
}
// doc = $rdf.sym($rdf.uri.join(right, base))
// if (doc.uri.slice(0, 7) !== 'file://') {
// exitMessage('Can only write files just now, sorry: ' + doc.uri)
// }
fileName = right
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