This repository has been archived by the owner on Feb 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
SparqlXMLResultWriter.js
76 lines (66 loc) · 2.53 KB
/
SparqlXMLResultWriter.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
/*! @license MIT ©2014-2016 Miel Vander Sande, Ghent University - imec */
/* Writer that serializes a SPARQL query result application/sparql+xml */
var SparqlResultWriter = require('./SparqlResultWriter'),
_ = require('lodash'),
util = require('../util/RdfUtil'),
xml = require('xml');
function SparqlXMLResultWriter(sparqlIterator) {
SparqlResultWriter.call(this, sparqlIterator);
}
SparqlResultWriter.subclass(SparqlXMLResultWriter);
SparqlXMLResultWriter.prototype._writeHead = function (variableNames) {
// Write root element
var self = this,
root = this._root = xml.element({
_attr: { xlmns: 'http://www.w3.org/2005/sparql-results#' },
});
xml({ sparql: root }, { stream: true, indent: ' ', declaration: true })
.on('data', function (chunk) { self._push(chunk + '\n'); });
// Write head element
if (variableNames.length) {
root.push({
head: variableNames.map(function (v) {
return { variable: { _attr: { name: v } } };
}),
});
}
};
SparqlXMLResultWriter.prototype._writeBindings = function (result) {
// With the first result, write the results element
if (!this._results)
this._root.push({ results: this._results = xml.element({}) });
// Unbound variables cannot be part of XML
result = _.omit(result, function (value) {
return value === undefined || value === null;
});
// Write the result element
this._results.push({
result: _.map(result, function (value, variable) {
var xmlValue, lang, type;
if (!util.isLiteral(value))
xmlValue = util.isBlank(value) ? { bnode: value } : { uri: value };
else {
xmlValue = { literal: util.getLiteralValue(value) };
if (lang = util.getLiteralLanguage(value))
xmlValue.literal = [{ _attr: { 'xml:lang': lang } }, xmlValue.literal];
else if (type = util.getLiteralType(value))
xmlValue.literal = [{ _attr: { datatype: type } }, xmlValue.literal];
}
return { binding: [{ _attr: { name: variable.substring(1) } }, xmlValue] };
}),
});
};
SparqlXMLResultWriter.prototype._writeBoolean = function (result) {
this._root.push({ boolean: result });
};
SparqlXMLResultWriter.prototype._flush = function (done) {
// If there were no matches, the results element hasn't been created yet
if (this._empty)
this._root.push({ results: this._results = xml.element({}) });
// There's no results element for ASK queries
if (this._results)
this._results.close();
this._root.close();
done();
};
module.exports = SparqlXMLResultWriter;