Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 37 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,30 @@ function serializeXML(graph, done) {
})

var subjectNodes = Object.create(null)
var blankNodes = Object.create(null)

graph.forEach(serializeTriple)

function serializeTriple(triple) {

if(triple.subject.interfaceName !== 'NamedNode')
throw new Error('triple subject must be a NamedNode')
var subject,
subjectNode;
if (triple.subject.interfaceName === 'BlankNode') {
subject = triple.subject.nominalValue

var subject = triple.subject.nominalValue
subjectNode = blankNodes[subject]

var subjectNode = subjectNodes[subject]
if (subjectNode === undefined)
subjectNode = blankNodes[subject] = []
}
else {
subject = triple.subject.nominalValue

subjectNode = subjectNodes[subject]

if(subjectNode === undefined)
subjectNode = subjectNodes[subject] = []
if (subjectNode === undefined)
subjectNode = subjectNodes[subject] = []
}

if(triple.predicate.interfaceName !== 'NamedNode')
throw new Error('predicate must be a NamedNode')
Expand All @@ -60,6 +70,13 @@ function serializeXML(graph, done) {
}
}

case 'BlankNode':
return {
_attr: {
'rdf:nodeID': "_:" + object.nominalValue
}
}

case 'Literal':

if(object.datatype.interfaceName !== 'NamedNode')
Expand Down Expand Up @@ -140,6 +157,19 @@ function serializeXML(graph, done) {
].concat(subjectNodes[subject])
}
})
).concat(
Object.keys(blankNodes)
.map(function (subject) {
return {
'rdf:Description': [
{
_attr: {
'rdf:nodeID': "_:" + subject
}
}
].concat(blankNodes[subject])
}
})
)
}, {
declaration: true,
Expand All @@ -160,7 +190,7 @@ XMLSerializer.prototype.serialize = serializeXML
var instance = new XMLSerializer()

for (var property in instance) {
XMLSerializer[property] = instance[property]
XMLSerializer[property] = instance[property]
}

module.exports = XMLSerializer
Expand Down