sparqlxml-parse
Advanced tools
Comparing version 2.0.2 to 2.1.0
@@ -29,2 +29,3 @@ /// <reference types="node" /> | ||
private stackEquals; | ||
private stackBeginsWith; | ||
} | ||
@@ -31,0 +32,0 @@ /** |
@@ -39,2 +39,3 @@ "use strict"; | ||
let currentText = ''; | ||
let currentQuotedTriples = []; | ||
parser.on("error", errorListener); | ||
@@ -56,4 +57,19 @@ parser.on("opentag", tag => { | ||
currentText = ''; | ||
currentQuotedTriples = []; | ||
} | ||
else if (this.stackEquals(stack, ['sparql', 'results', 'result', 'binding'])) { | ||
else if (tag.name === 'triple' && this.stackBeginsWith(stack, ['sparql', 'results', 'result'])) { | ||
currentQuotedTriples.push({ components: {} }); | ||
} | ||
else if (stack[stack.length - 1] === 'triple' && this.stackBeginsWith(stack, ['sparql', 'results', 'result', 'binding'])) { | ||
currentBindingType = ''; | ||
currentBindingAnnotation = ''; | ||
currentText = ''; | ||
if (!['subject', 'predicate', 'object'].includes(tag.name)) { | ||
errorListener(new Error(`Illegal quoted triple component '${tag.name}' found on line ${parser.line + 1}`)); | ||
} | ||
else { | ||
currentQuotedTriples[currentQuotedTriples.length - 1].currentComponent = tag.name; | ||
} | ||
} | ||
else if (this.stackBeginsWith(stack, ['sparql', 'results', 'result', 'binding'])) { | ||
currentBindingType = tag.name; | ||
@@ -80,4 +96,5 @@ if ('xml:lang' in tag.attributes) { | ||
} | ||
if (this.stackEquals(stack, ['sparql', 'results', 'result', 'binding'])) { | ||
const key = this.prefixVariableQuestionMark ? ('?' + currentBindingName) : currentBindingName; | ||
if (this.stackBeginsWith(stack, ['sparql', 'results', 'result', 'binding'])) { | ||
// Determine current RDF term value | ||
let term; | ||
if (!currentBindingName && currentBindingType) { | ||
@@ -87,13 +104,38 @@ errorListener(new Error(`Terms should have a name on line ${parser.line + 1}`)); | ||
else if (currentBindingType === 'uri') { | ||
currentBindings[key] = this.dataFactory.namedNode(currentText); | ||
term = this.dataFactory.namedNode(currentText); | ||
} | ||
else if (currentBindingType === 'bnode') { | ||
currentBindings[key] = this.dataFactory.blankNode(currentText); | ||
term = this.dataFactory.blankNode(currentText); | ||
} | ||
else if (currentBindingType === 'literal') { | ||
currentBindings[key] = this.dataFactory.literal(currentText, currentBindingAnnotation); | ||
term = this.dataFactory.literal(currentText, currentBindingAnnotation); | ||
} | ||
else if (stack[stack.length - 1] === 'triple') { | ||
const currentQuotedTriple = currentQuotedTriples.pop(); | ||
if (currentQuotedTriple && currentQuotedTriple.components.subject && currentQuotedTriple.components.predicate && currentQuotedTriple.components.object) { | ||
term = this.dataFactory.quad(currentQuotedTriple.components.subject, currentQuotedTriple.components.predicate, currentQuotedTriple.components.object); | ||
} | ||
else { | ||
errorListener(new Error(`Incomplete quoted triple on line ${parser.line + 1}`)); | ||
} | ||
} | ||
else if (currentBindingType) { | ||
errorListener(new Error(`Invalid term type '${currentBindingType}' on line ${parser.line + 1}`)); | ||
} | ||
if (term) { | ||
if (currentQuotedTriples.length > 0) { | ||
// If we're in a quoted triple, store the term inside the active quoted triple | ||
const currentQuotedTriple = currentQuotedTriples[currentQuotedTriples.length - 1]; | ||
if (currentQuotedTriple.components[currentQuotedTriple.currentComponent]) { | ||
errorListener(new Error(`The ${currentQuotedTriple.currentComponent} in a quoted triple on line ${parser.line + 1} was already defined before`)); | ||
} | ||
currentQuotedTriple.components[currentQuotedTriple.currentComponent] = term; | ||
} | ||
else { | ||
// Store the value in the current bindings object | ||
const key = this.prefixVariableQuestionMark ? ('?' + currentBindingName) : currentBindingName; | ||
currentBindings[key] = term; | ||
} | ||
} | ||
currentBindingType = undefined; | ||
} | ||
@@ -103,3 +145,3 @@ stack.pop(); | ||
parser.on("text", text => { | ||
if (this.stackEquals(stack, ['sparql', 'results', 'result', 'binding', currentBindingType])) { | ||
if (this.stackBeginsWith(stack, ['sparql', 'results', 'result', 'binding']) && stack[stack.length - 1] === currentBindingType) { | ||
currentText = text; | ||
@@ -157,4 +199,7 @@ } | ||
} | ||
stackBeginsWith(a, b) { | ||
return a.length >= b.length && b.every((v, i) => a[i] === v); | ||
} | ||
} | ||
exports.SparqlXmlParser = SparqlXmlParser; | ||
//# sourceMappingURL=SparqlXmlParser.js.map |
{ | ||
"name": "sparqlxml-parse", | ||
"version": "2.0.2", | ||
"version": "2.1.0", | ||
"description": "Parses SPARQL XML query results", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -8,3 +8,3 @@ # SPARQL-Results+XML Parse | ||
A utility package that allows you to parse [SPARQL XML](https://www.w3.org/TR/rdf-sparql-XMLres/) results | ||
in a convenient [RDFJS](http://rdf.js.org/)-based datastructure. | ||
in a convenient [RDF/JS](http://rdf.js.org/)-based datastructure. | ||
@@ -46,2 +46,17 @@ For example, the following SPARQL XML result can be converted as follows: | ||
</result> | ||
<result> | ||
<binding name="book"> | ||
<triple> | ||
<subject> | ||
<uri>http://example.org/bob</uri> | ||
</subject> | ||
<predicate> | ||
<uri>http://example.org/name</uri> | ||
</predicate> | ||
<object> | ||
<literal datatype='http://example.org/Type'>Bob</literal> | ||
</object> | ||
</triple> | ||
</binding> | ||
</result> | ||
</results> | ||
@@ -58,7 +73,8 @@ </sparql> | ||
{ '?book': namedNode('http://example.org/book/book4') }, | ||
{ '?book': namedNode('http://example.org/book/book5') }, | ||
{ '?book': namedNode('http://example.org/book/book5') }, | ||
{ '?book': quad(namedNode('http://example.org/bob'), namedNode('http://example.org/name'), literal('Bob', namedNode('http://example.org/Type'))) }, | ||
] | ||
``` | ||
Where `namedNode` is an RDFJS named node. | ||
Where `namedNode` is an RDF/JS named node, `quad` is an RDF/JS quad/triple, and `literal` is an RDF/JS literal. | ||
@@ -65,0 +81,0 @@ This library automatically converts all SPARQL XML result values to their respective RDFJS type. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36919
265
147
9