forked from bjdmeest/rdf-serializer-xml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
199 lines (140 loc) · 5.35 KB
/
index.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
var rdf = require('rdf-ext')
var inherits = require('inherits')
var AbstractSerializer = require('rdf-serializer-abstract')
var xml = require('xml')
var uniq = require('uniq')
function XMLSerializer() {
AbstractSerializer.call(this, rdf)
}
inherits(XMLSerializer, AbstractSerializer)
function serializeXML(graph, done) {
function serialize(resolve) {
var prefixes = Object.create(null)
Object.keys(rdf.prefixes).forEach(function(prefix) {
prefixes[prefix] = rdf.prefixes[prefix]
})
var subjectNodes = Object.create(null)
var blankNodes = Object.create(null)
graph.forEach(serializeTriple)
function serializeTriple(triple) {
var subject,
subjectNode;
if (triple.subject.interfaceName === 'BlankNode') {
subject = triple.subject.nominalValue
subjectNode = blankNodes[subject]
if (subjectNode === undefined)
subjectNode = blankNodes[subject] = []
}
else {
subject = triple.subject.nominalValue
subjectNode = subjectNodes[subject]
if (subjectNode === undefined)
subjectNode = subjectNodes[subject] = []
}
if(triple.predicate.interfaceName !== 'NamedNode')
throw new Error('predicate must be a NamedNode')
var predicate = triple.predicate.nominalValue
subjectNode.push({
[prefixify(predicate, true)]: serializeObject(triple.object)
})
}
function serializeObject(object) {
switch(object.interfaceName) {
case 'NamedNode':
return {
_attr: {
'rdf:resource': object.nominalValue
}
}
case 'BlankNode':
return {
_attr: {
'rdf:nodeID': "_:" + object.nominalValue
}
}
case 'Literal':
if(object.datatype.interfaceName !== 'NamedNode')
throw new Error('datatype must be a NamedNode')
var attr = {}
if(object.language)
attr['xml:lang'] = object.language
if(object.datatype.nominalValue !== 'http://www.w3.org/2001/XMLSchema#string')
attr['rdfs:Datatype'] = prefixify(object.datatype.nominalValue, false)
return [
{ _attr: attr },
object.nominalValue
]
default:
throw new Error('unknown node type: ' + object.interfaceName)
}
}
function prefixify(iri, createNew) {
for(var prefix in prefixes) {
var prefixIRI = prefixes[prefix]
if(iri.indexOf(prefixIRI) === 0) {
return prefix + ':' + iri.slice(prefixIRI.length)
}
}
if(!createNew)
return iri
var fragmentStart = iri.lastIndexOf('#')
if(fragmentStart === -1)
fragmentStart = iri.lastIndexOf('/')
if(fragmentStart === -1)
return iri
var iriPrefix = iri.substr(0, fragmentStart + 1)
for(var i = 0 ;; ++ i) {
var prefixName = 'ns' + i
if(prefixes[prefixName] === undefined) {
prefixes[prefixName] = iriPrefix
return prefixName + ':' + iri.slice(iriPrefix.length)
}
}
}
var attr = {}
for(var prefix in prefixes)
attr['xmlns:' + prefix] = prefixes[prefix]
var xmlDoc = xml({
'rdf:RDF': [ { _attr: attr } ].concat(
Object.keys(subjectNodes)
.map(function(subject) {
return {
'rdf:Description': [
{
_attr: {
'rdf:about': subject
}
}
].concat(subjectNodes[subject])
}
})
).concat(
Object.keys(blankNodes)
.map(function (subject) {
return {
'rdf:Description': [
{
_attr: {
'rdf:nodeID': "_:" + subject
}
}
].concat(blankNodes[subject])
}
})
)
}, {
declaration: true,
indent: ' '
})
if(done)
done(null, xmlDoc)
resolve(xmlDoc)
}
return new Promise(serialize)
}
XMLSerializer.prototype.serialize = serializeXML
var instance = new XMLSerializer()
for (var property in instance) {
XMLSerializer[property] = instance[property]
}
module.exports = XMLSerializer