sparqlxml-parse
Advanced tools
Comparing version 1.5.0 to 2.0.0
export * from "./lib/SparqlXmlParser"; | ||
export * from "./lib/SparqlXmlBindingsTransformer"; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
@@ -14,3 +18,2 @@ if (k2 === undefined) k2 = k; | ||
__exportStar(require("./lib/SparqlXmlParser"), exports); | ||
__exportStar(require("./lib/SparqlXmlBindingsTransformer"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -22,14 +22,9 @@ /// <reference types="node" /> | ||
/** | ||
* Convert a SPARQL XML result binding to a bindings object. | ||
* @param rawBindings A SPARQL XML result binding. | ||
* @return {IBindings} A bindings object. | ||
*/ | ||
parseXmlBindings(rawBindings: any): IBindings; | ||
/** | ||
* Convert a SPARQL XML boolean response stream to a promise resolving to a boolean. | ||
* This will reject if the given response was not a valid boolean response. | ||
* @param {NodeJS.ReadableStream} sparqlResponseStream A SPARQL XML response stream. | ||
* @return {NodeJS.ReadableStream} A stream of bindings. | ||
* @return {Promise<boolean>} The response boolean. | ||
*/ | ||
parseXmlBooleanStream(sparqlResponseStream: NodeJS.ReadableStream): Promise<boolean>; | ||
private stackEquals; | ||
} | ||
@@ -36,0 +31,0 @@ /** |
@@ -5,5 +5,4 @@ "use strict"; | ||
const rdf_data_factory_1 = require("rdf-data-factory"); | ||
const SparqlXmlBindingsTransformer_1 = require("./SparqlXmlBindingsTransformer"); | ||
// tslint:disable-next-line:no-var-requires | ||
const XmlNode = require('sax-stream'); | ||
const saxes_1 = require("saxes"); | ||
const readable_stream_1 = require("readable-stream"); | ||
/** | ||
@@ -29,75 +28,115 @@ * Parser for the SPARQL Query Results XML format. | ||
parseXmlResultsStream(sparqlResponseStream) { | ||
// Collect variables | ||
const errorListener = (error) => resultStream.emit('error', error); | ||
sparqlResponseStream.on('error', errorListener); | ||
const parser = new saxes_1.SaxesParser(); | ||
const stack = []; | ||
const variables = []; | ||
sparqlResponseStream | ||
.pipe(XmlNode({ strict: true, tag: 'variable' })) | ||
.on('data', (node) => variables.push(this.dataFactory.variable(node.attribs.name))) | ||
.on('error', () => { return; }) // Ignore errors, they will emitted in the results | ||
.on('finish', () => resultStream.emit('variables', variables)); | ||
// Collect results | ||
let currentBindings = {}; | ||
let currentBindingName = ''; | ||
let currentBindingType = ''; | ||
let currentBindingAnnotation; | ||
let currentText = ''; | ||
parser.on("error", errorListener); | ||
parser.on("opentag", tag => { | ||
if (tag.name === "variable" && this.stackEquals(stack, ['sparql', 'head'])) { | ||
variables.push(this.dataFactory.variable(tag.attributes.name)); | ||
} | ||
else if (tag.name === 'result' && this.stackEquals(stack, ['sparql', 'results'])) { | ||
currentBindings = {}; | ||
} | ||
else if (tag.name === 'binding' && this.stackEquals(stack, ['sparql', 'results', 'result'])) { | ||
currentBindingName = tag.attributes.name || ''; | ||
currentBindingType = ''; | ||
currentBindingAnnotation = ''; | ||
currentText = ''; | ||
} | ||
else if (this.stackEquals(stack, ['sparql', 'results', 'result', 'binding'])) { | ||
currentBindingType = tag.name; | ||
if ('xml:lang' in tag.attributes) { | ||
currentBindingAnnotation = tag.attributes['xml:lang']; | ||
} | ||
else if ('datatype' in tag.attributes) { | ||
currentBindingAnnotation = this.dataFactory.namedNode(tag.attributes.datatype); | ||
} | ||
else { | ||
currentBindingAnnotation = undefined; | ||
} | ||
} | ||
stack.push(tag.name); | ||
}); | ||
parser.on("closetag", tag => { | ||
if (this.stackEquals(stack, ['sparql', 'head'])) { | ||
resultStream.emit("variables", variables); | ||
} | ||
if (this.stackEquals(stack, ['sparql', 'results', 'result'])) { | ||
resultStream.push(currentBindings); | ||
} | ||
if (this.stackEquals(stack, ['sparql', 'results', 'result', 'binding'])) { | ||
const key = this.prefixVariableQuestionMark ? ('?' + currentBindingName) : currentBindingName; | ||
if (!currentBindingName && currentBindingType) { | ||
errorListener(new Error(`Terms should have a name on line ${parser.line + 1}`)); | ||
} | ||
else if (currentBindingType === 'uri') { | ||
currentBindings[key] = this.dataFactory.namedNode(currentText); | ||
} | ||
else if (currentBindingType === 'bnode') { | ||
currentBindings[key] = this.dataFactory.blankNode(currentText); | ||
} | ||
else if (currentBindingType === 'literal') { | ||
currentBindings[key] = this.dataFactory.literal(currentText, currentBindingAnnotation); | ||
} | ||
else if (currentBindingType) { | ||
errorListener(new Error(`Invalid term type '${currentBindingType}' on line ${parser.line + 1}`)); | ||
} | ||
} | ||
stack.pop(); | ||
}); | ||
parser.on("text", text => { | ||
if (this.stackEquals(stack, ['sparql', 'results', 'result', 'binding', currentBindingType])) { | ||
currentText = text; | ||
} | ||
}); | ||
const resultStream = sparqlResponseStream | ||
.pipe(XmlNode({ strict: true, tag: 'result' })) | ||
.on('error', (error) => resultStream.emit('error', error)) | ||
.pipe(new SparqlXmlBindingsTransformer_1.SparqlXmlBindingsTransformer(this)); | ||
// Propagate errors | ||
sparqlResponseStream.on('error', (error) => resultStream.emit('error', error)); | ||
.pipe(new readable_stream_1.Transform({ | ||
objectMode: true, | ||
transform(chunk, encoding, callback) { | ||
parser.write(chunk); | ||
callback(); | ||
} | ||
})); | ||
return resultStream; | ||
} | ||
/** | ||
* Convert a SPARQL XML result binding to a bindings object. | ||
* @param rawBindings A SPARQL XML result binding. | ||
* @return {IBindings} A bindings object. | ||
*/ | ||
parseXmlBindings(rawBindings) { | ||
const bindings = {}; | ||
if (rawBindings.children) { | ||
const bindingsArray = Array.isArray(rawBindings.children.binding) | ||
? rawBindings.children.binding : [rawBindings.children.binding]; | ||
for (const binding of bindingsArray) { | ||
if (binding.attribs && binding.children) { | ||
const key = binding.attribs.name; | ||
let term = null; | ||
if (binding.children.bnode) { | ||
term = this.dataFactory.blankNode(binding.children.bnode.value); | ||
} | ||
else if (binding.children.literal) { | ||
const value = binding.children.literal.value || ""; | ||
const attribs = binding.children.literal.attribs; | ||
if (attribs && attribs['xml:lang']) { | ||
term = this.dataFactory.literal(value, attribs['xml:lang']); | ||
} | ||
else if (attribs && attribs.datatype) { | ||
term = this.dataFactory.literal(value, this.dataFactory.namedNode(attribs.datatype)); | ||
} | ||
else { | ||
term = this.dataFactory.literal(value); | ||
} | ||
} | ||
else { | ||
term = this.dataFactory.namedNode(binding.children.uri.value); | ||
} | ||
bindings[this.prefixVariableQuestionMark ? ('?' + key) : key] = term; | ||
} | ||
} | ||
} | ||
return bindings; | ||
} | ||
/** | ||
* Convert a SPARQL XML boolean response stream to a promise resolving to a boolean. | ||
* This will reject if the given response was not a valid boolean response. | ||
* @param {NodeJS.ReadableStream} sparqlResponseStream A SPARQL XML response stream. | ||
* @return {NodeJS.ReadableStream} A stream of bindings. | ||
* @return {Promise<boolean>} The response boolean. | ||
*/ | ||
parseXmlBooleanStream(sparqlResponseStream) { | ||
return new Promise((resolve, reject) => { | ||
sparqlResponseStream.on('error', reject); | ||
const parser = new saxes_1.SaxesParser(); | ||
const stack = []; | ||
parser.on("error", reject); | ||
parser.on("opentag", tag => { | ||
stack.push(tag.name); | ||
}); | ||
parser.on("closetag", _ => { | ||
stack.pop(); | ||
}); | ||
parser.on("text", text => { | ||
if (this.stackEquals(stack, ['sparql', 'boolean'])) { | ||
resolve(text === 'true'); | ||
} | ||
}); | ||
sparqlResponseStream | ||
.pipe(XmlNode({ strict: true, tag: 'boolean' })) | ||
.on('error', reject) | ||
.on('data', (node) => resolve(node.value === 'true')) | ||
.on('data', d => parser.write(d)) | ||
.on('end', () => reject(new Error('No valid ASK response was found.'))); | ||
}); | ||
} | ||
stackEquals(a, b) { | ||
return a.length === b.length && a.every((v, i) => b[i] === v); | ||
} | ||
} | ||
exports.SparqlXmlParser = SparqlXmlParser; | ||
//# sourceMappingURL=SparqlXmlParser.js.map |
{ | ||
"name": "sparqlxml-parse", | ||
"version": "1.5.0", | ||
"version": "2.0.0", | ||
"description": "Parses SPARQL XML query results", | ||
@@ -33,7 +33,7 @@ "keywords": [ | ||
"devDependencies": { | ||
"@types/jest": "^26.0.0", | ||
"@types/jest": "^28.0.0", | ||
"@types/minimist": "^1.2.0", | ||
"arrayify-stream": "^1.0.0", | ||
"arrayify-stream": "^2.0.0", | ||
"coveralls": "^3.0.0", | ||
"jest": "^26.0.0", | ||
"jest": "^28.0.0", | ||
"jest-rdf": "^1.7.0", | ||
@@ -43,6 +43,9 @@ "manual-git-changelog": "^1.0.0", | ||
"streamify-string": "^1.0.1", | ||
"ts-jest": "^26.0.0", | ||
"ts-jest": "^28.0.0", | ||
"ts-loader": "^9.3.1", | ||
"tslint": "^6.0.0", | ||
"tslint-eslint-rules": "^5.3.1", | ||
"typescript": "^4.3.5" | ||
"typescript": "^4.3.5", | ||
"webpack": "^5.73.0", | ||
"webpack-cli": "^4.10.0" | ||
}, | ||
@@ -52,3 +55,3 @@ "jest": { | ||
"ts-jest": { | ||
"tsConfig": "test/tsconfig.json" | ||
"tsconfig": "test/tsconfig.json" | ||
} | ||
@@ -85,6 +88,9 @@ }, | ||
"@rdfjs/types": "*", | ||
"@types/node": "^13.1.0", | ||
"@types/readable-stream": "^2.3.13", | ||
"buffer": "^6.0.3", | ||
"rdf-data-factory": "^1.1.0", | ||
"sax-stream": "^1.2.3" | ||
} | ||
"readable-stream": "^4.0.0", | ||
"saxes": "^6.0.0" | ||
}, | ||
"sideEffects": false | ||
} |
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
21257
10
240
6
16
+ Addedbuffer@^6.0.3
+ Addedreadable-stream@^4.0.0
+ Addedsaxes@^6.0.0
+ Added@types/node@22.10.2(transitive)
+ Added@types/readable-stream@2.3.15(transitive)
+ Addedabort-controller@3.0.0(transitive)
+ Addedbase64-js@1.5.1(transitive)
+ Addedbuffer@6.0.3(transitive)
+ Addedevent-target-shim@5.0.1(transitive)
+ Addedevents@3.3.0(transitive)
+ Addedieee754@1.2.1(transitive)
+ Addedprocess@0.11.10(transitive)
+ Addedreadable-stream@4.6.0(transitive)
+ Addedsafe-buffer@5.1.25.2.1(transitive)
+ Addedsaxes@6.0.0(transitive)
+ Addedstring_decoder@1.3.0(transitive)
+ Addedundici-types@6.20.0(transitive)
+ Addedxmlchars@2.2.0(transitive)
- Removed@types/node@^13.1.0
- Removedsax-stream@^1.2.3
- Removed@types/node@13.13.52(transitive)
- Removeddebug@2.6.9(transitive)
- Removedms@2.0.0(transitive)
- Removedsax@1.4.1(transitive)
- Removedsax-stream@1.3.0(transitive)