| 1 |
// N3.js implementations of the RDF/JS core data types |
| 2 |
// See https://github.com/rdfjs/representation-task-force/blob/master/interface-spec.md |
| 3 |
|
| 4 |
import namespaces from './IRIs'; |
| 5 |
import { isDefaultGraph } from './N3Util'; |
| 6 |
const { rdf, xsd } = namespaces; |
| 7 |
|
| 8 |
// eslint-disable-next-line prefer-const |
| 9 |
let DEFAULTGRAPH; |
| 10 |
let _blankNodeCounter = 0; |
| 11 |
|
| 12 |
const escapedLiteral = /^"(.*".*)(?="[^"]*$)/; |
| 13 |
const quadId = /^<<("(?:""|[^"])*"[^ ]*|[^ ]+) ("(?:""|[^"])*"[^ ]*|[^ ]+) ("(?:""|[^"])*"[^ ]*|[^ ]+) ?("(?:""|[^"])*"[^ ]*|[^ ]+)?>>$/; |
| 14 |
|
| 15 |
// ## DataFactory singleton |
| 16 |
const DataFactory = { |
| 17 |
namedNode, |
| 18 |
blankNode, |
| 19 |
variable, |
| 20 |
literal, |
| 21 |
defaultGraph, |
| 22 |
quad, |
| 23 |
triple: quad, |
| 24 |
}; |
| 25 |
export default DataFactory; |
| 26 |
|
| 27 |
// ## Term constructor |
| 28 |
export class Term { |
| 29 |
constructor(id) { |
| 30 |
this.id = id; |
| 31 |
} |
| 32 |
|
| 33 |
// ### The value of this term |
| 34 |
get value() { |
| 35 |
return this.id; |
| 36 |
} |
| 37 |
|
| 38 |
// ### Returns whether this object represents the same term as the other |
| 39 |
equals(other) { |
| 40 |
// If both terms were created by this library, |
| 41 |
// equality can be computed through ids |
| 42 |
if (other instanceof Term) |
| 43 |
return this.id === other.id; |
| 44 |
// Otherwise, compare term type and value |
| 45 |
return !!other && this.termType === other.termType && |
| 46 |
this.value === other.value; |
| 47 |
} |
| 48 |
|
| 49 |
// ### Implement hashCode for Immutable.js, since we implement `equals` |
| 50 |
// https://immutable-js.com/docs/v4.0.0/ValueObject/#hashCode() |
| 51 |
hashCode() { |
| 52 |
return 0; |
| 53 |
} |
| 54 |
|
| 55 |
// ### Returns a plain object representation of this term |
| 56 |
toJSON() { |
| 57 |
return { |
| 58 |
termType: this.termType, |
| 59 |
value: this.value, |
| 60 |
}; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
// ## NamedNode constructor |
| 66 |
export class NamedNode extends Term { |
| 67 |
// ### The term type of this term |
| 68 |
get termType() { |
| 69 |
return 'NamedNode'; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
// ## Literal constructor |
| 74 |
export class Literal extends Term { |
| 75 |
// ### The term type of this term |
| 76 |
get termType() { |
| 77 |
return 'Literal'; |
| 78 |
} |
| 79 |
|
| 80 |
// ### The text value of this literal |
| 81 |
get value() { |
| 82 |
return this.id.substring(1, this.id.lastIndexOf('"')); |
| 83 |
} |
| 84 |
|
| 85 |
// ### The language of this literal |
| 86 |
get language() { |
| 87 |
// Find the last quotation mark (e.g., '"abc"@en-us') |
| 88 |
const id = this.id; |
| 89 |
let atPos = id.lastIndexOf('"') + 1; |
| 90 |
// If "@" it follows, return the remaining substring; empty otherwise |
| 91 |
return atPos < id.length && id[atPos++] === '@' ? id.substr(atPos).toLowerCase() : ''; |
| 92 |
} |
| 93 |
|
| 94 |
// ### The datatype IRI of this literal |
| 95 |
get datatype() { |
| 96 |
return new NamedNode(this.datatypeString); |
| 97 |
} |
| 98 |
|
| 99 |
// ### The datatype string of this literal |
| 100 |
get datatypeString() { |
| 101 |
// Find the last quotation mark (e.g., '"abc"^^http://ex.org/types#t') |
| 102 |
const id = this.id, dtPos = id.lastIndexOf('"') + 1; |
| 103 |
const char = dtPos < id.length ? id[dtPos] : ''; |
| 104 |
// If "^" it follows, return the remaining substring |
| 105 |
return char === '^' ? id.substr(dtPos + 2) : |
| 106 |
// If "@" follows, return rdf:langString; xsd:string otherwise |
| 107 |
(char !== '@' ? xsd.string : rdf.langString); |
| 108 |
} |
| 109 |
|
| 110 |
// ### Returns whether this object represents the same term as the other |
| 111 |
equals(other) { |
| 112 |
// If both literals were created by this library, |
| 113 |
// equality can be computed through ids |
| 114 |
if (other instanceof Literal) |
| 115 |
return this.id === other.id; |
| 116 |
// Otherwise, compare term type, value, language, and datatype |
| 117 |
return !!other && !!other.datatype && |
| 118 |
this.termType === other.termType && |
| 119 |
this.value === other.value && |
| 120 |
this.language === other.language && |
| 121 |
this.datatype.value === other.datatype.value; |
| 122 |
} |
| 123 |
|
| 124 |
toJSON() { |
| 125 |
return { |
| 126 |
termType: this.termType, |
| 127 |
value: this.value, |
| 128 |
language: this.language, |
| 129 |
datatype: { termType: 'NamedNode', value: this.datatypeString }, |
| 130 |
}; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
// ## BlankNode constructor |
| 135 |
export class BlankNode extends Term { |
| 136 |
constructor(name) { |
| 137 |
super(`_:${name}`); |
| 138 |
} |
| 139 |
|
| 140 |
// ### The term type of this term |
| 141 |
get termType() { |
| 142 |
return 'BlankNode'; |
| 143 |
} |
| 144 |
|
| 145 |
// ### The name of this blank node |
| 146 |
get value() { |
| 147 |
return this.id.substr(2); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
export class Variable extends Term { |
| 152 |
constructor(name) { |
| 153 |
super(`?${name}`); |
| 154 |
} |
| 155 |
|
| 156 |
// ### The term type of this term |
| 157 |
get termType() { |
| 158 |
return 'Variable'; |
| 159 |
} |
| 160 |
|
| 161 |
// ### The name of this variable |
| 162 |
get value() { |
| 163 |
return this.id.substr(1); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
// ## DefaultGraph constructor |
| 168 |
export class DefaultGraph extends Term { |
| 169 |
constructor() { |
| 170 |
super(''); |
| 171 |
return DEFAULTGRAPH || this; |
| 172 |
} |
| 173 |
|
| 174 |
// ### The term type of this term |
| 175 |
get termType() { |
| 176 |
return 'DefaultGraph'; |
| 177 |
} |
| 178 |
|
| 179 |
// ### Returns whether this object represents the same term as the other |
| 180 |
equals(other) { |
| 181 |
// If both terms were created by this library, |
| 182 |
// equality can be computed through strict equality; |
| 183 |
// otherwise, compare term types. |
| 184 |
return (this === other) || (!!other && (this.termType === other.termType)); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
// ## DefaultGraph singleton |
| 189 |
DEFAULTGRAPH = new DefaultGraph(); |
| 190 |
|
| 191 |
|
| 192 |
// ### Constructs a term from the given internal string ID |
| 193 |
export function termFromId(id, factory) { |
| 194 |
factory = factory || DataFactory; |
| 195 |
|
| 196 |
// Falsy value or empty string indicate the default graph |
| 197 |
if (!id) |
| 198 |
return factory.defaultGraph(); |
| 199 |
|
| 200 |
// Identify the term type based on the first character |
| 201 |
switch (id[0]) { |
| 202 |
case '?': |
| 203 |
return factory.variable(id.substr(1)); |
| 204 |
case '_': |
| 205 |
return factory.blankNode(id.substr(2)); |
| 206 |
case '"': |
| 207 |
// Shortcut for internal literals |
| 208 |
if (factory === DataFactory) |
| 209 |
return new Literal(id); |
| 210 |
// Literal without datatype or language |
| 211 |
if (id[id.length - 1] === '"') |
| 212 |
return factory.literal(id.substr(1, id.length - 2)); |
| 213 |
// Literal with datatype or language |
| 214 |
const endPos = id.lastIndexOf('"', id.length - 1); |
| 215 |
return factory.literal(id.substr(1, endPos - 1), |
| 216 |
id[endPos + 1] === '@' ? id.substr(endPos + 2) |
| 217 |
: factory.namedNode(id.substr(endPos + 3))); |
| 218 |
case '<': |
| 219 |
const components = quadId.exec(id); |
| 220 |
return factory.quad( |
| 221 |
termFromId(unescapeQuotes(components[1]), factory), |
| 222 |
termFromId(unescapeQuotes(components[2]), factory), |
| 223 |
termFromId(unescapeQuotes(components[3]), factory), |
| 224 |
components[4] && termFromId(unescapeQuotes(components[4]), factory) |
| 225 |
); |
| 226 |
default: |
| 227 |
return factory.namedNode(id); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
// ### Constructs an internal string ID from the given term or ID string |
| 232 |
export function termToId(term) { |
| 233 |
if (typeof term === 'string') |
| 234 |
return term; |
| 235 |
if (term instanceof Term && term.termType !== 'Quad') |
| 236 |
return term.id; |
| 237 |
if (!term) |
| 238 |
return DEFAULTGRAPH.id; |
| 239 |
|
| 240 |
// Term instantiated with another library |
| 241 |
switch (term.termType) { |
| 242 |
case 'NamedNode': return term.value; |
| 243 |
case 'BlankNode': return `_:${term.value}`; |
| 244 |
case 'Variable': return `?${term.value}`; |
| 245 |
case 'DefaultGraph': return ''; |
| 246 |
case 'Literal': return `"${term.value}"${ |
| 247 |
term.language ? `@${term.language}` : |
| 248 |
(term.datatype && term.datatype.value !== xsd.string ? `^^${term.datatype.value}` : '')}`; |
| 249 |
case 'Quad': |
| 250 |
// To identify RDF* quad components, we escape quotes by doubling them. |
| 251 |
// This avoids the overhead of backslash parsing of Turtle-like syntaxes. |
| 252 |
return `<<${ |
| 253 |
escapeQuotes(termToId(term.subject)) |
| 254 |
} ${ |
| 255 |
escapeQuotes(termToId(term.predicate)) |
| 256 |
} ${ |
| 257 |
escapeQuotes(termToId(term.object)) |
| 258 |
}${ |
| 259 |
(isDefaultGraph(term.graph)) ? '' : ` ${termToId(term.graph)}` |
| 260 |
}>>`; |
| 261 |
default: throw new Error(`Unexpected termType: ${term.termType}`); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
// ## Quad constructor |
| 267 |
export class Quad extends Term { |
| 268 |
constructor(subject, predicate, object, graph) { |
| 269 |
super(''); |
| 270 |
this._subject = subject; |
| 271 |
this._predicate = predicate; |
| 272 |
this._object = object; |
| 273 |
this._graph = graph || DEFAULTGRAPH; |
| 274 |
} |
| 275 |
|
| 276 |
// ### The term type of this term |
| 277 |
get termType() { |
| 278 |
return 'Quad'; |
| 279 |
} |
| 280 |
|
| 281 |
get subject() { |
| 282 |
return this._subject; |
| 283 |
} |
| 284 |
|
| 285 |
get predicate() { |
| 286 |
return this._predicate; |
| 287 |
} |
| 288 |
|
| 289 |
get object() { |
| 290 |
return this._object; |
| 291 |
} |
| 292 |
|
| 293 |
get graph() { |
| 294 |
return this._graph; |
| 295 |
} |
| 296 |
|
| 297 |
// ### Returns a plain object representation of this quad |
| 298 |
toJSON() { |
| 299 |
return { |
| 300 |
termType: this.termType, |
| 301 |
subject: this._subject.toJSON(), |
| 302 |
predicate: this._predicate.toJSON(), |
| 303 |
object: this._object.toJSON(), |
| 304 |
graph: this._graph.toJSON(), |
| 305 |
}; |
| 306 |
} |
| 307 |
|
| 308 |
// ### Returns whether this object represents the same quad as the other |
| 309 |
equals(other) { |
| 310 |
return !!other && this._subject.equals(other.subject) && |
| 311 |
this._predicate.equals(other.predicate) && |
| 312 |
this._object.equals(other.object) && |
| 313 |
this._graph.equals(other.graph); |
| 314 |
} |
| 315 |
} |
| 316 |
export { Quad as Triple }; |
| 317 |
|
| 318 |
// ### Escapes the quotes within the given literal |
| 319 |
export function escapeQuotes(id) { |
| 320 |
return id.replace(escapedLiteral, (_, quoted) => `"${quoted.replace(/"/g, '""')}`); |
| 321 |
} |
| 322 |
|
| 323 |
// ### Unescapes the quotes within the given literal |
| 324 |
export function unescapeQuotes(id) { |
| 325 |
return id.replace(escapedLiteral, (_, quoted) => `"${quoted.replace(/""/g, '"')}`); |
| 326 |
} |
| 327 |
|
| 328 |
// ### Creates an IRI |
| 329 |
function namedNode(iri) { |
| 330 |
return new NamedNode(iri); |
| 331 |
} |
| 332 |
|
| 333 |
// ### Creates a blank node |
| 334 |
function blankNode(name) { |
| 335 |
return new BlankNode(name || `n3-${_blankNodeCounter++}`); |
| 336 |
} |
| 337 |
|
| 338 |
// ### Creates a literal |
| 339 |
function literal(value, languageOrDataType) { |
| 340 |
// Create a language-tagged string |
| 341 |
if (typeof languageOrDataType === 'string') |
| 342 |
return new Literal(`"${value}"@${languageOrDataType.toLowerCase()}`); |
| 343 |
|
| 344 |
// Automatically determine datatype for booleans and numbers |
| 345 |
let datatype = languageOrDataType ? languageOrDataType.value : ''; |
| 346 |
if (datatype === '') { |
| 347 |
// Convert a boolean |
| 348 |
if (typeof value === 'boolean') |
| 349 |
datatype = xsd.boolean; |
| 350 |
// Convert an integer or double |
| 351 |
else if (typeof value === 'number') { |
| 352 |
if (Number.isFinite(value)) |
| 353 |
datatype = Number.isInteger(value) ? xsd.integer : xsd.double; |
| 354 |
else { |
| 355 |
datatype = xsd.double; |
| 356 |
if (!Number.isNaN(value)) |
| 357 |
value = value > 0 ? 'INF' : '-INF'; |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
// Create a datatyped literal |
| 363 |
return (datatype === '' || datatype === xsd.string) ? |
| 364 |
new Literal(`"${value}"`) : |
| 365 |
new Literal(`"${value}"^^${datatype}`); |
| 366 |
} |
| 367 |
|
| 368 |
// ### Creates a variable |
| 369 |
function variable(name) { |
| 370 |
return new Variable(name); |
| 371 |
} |
| 372 |
|
| 373 |
// ### Returns the default graph |
| 374 |
function defaultGraph() { |
| 375 |
return DEFAULTGRAPH; |
| 376 |
} |
| 377 |
|
| 378 |
// ### Creates a quad |
| 379 |
function quad(subject, predicate, object, graph) { |
| 380 |
return new Quad(subject, predicate, object, graph); |
| 381 |
} |