forked from linkeddata/rdflib.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdfparser.js
463 lines (446 loc) · 18 KB
/
rdfparser.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/**
* @fileoverview
* RDF/XML PARSER
*
* Version 0.1
* Parser believed to be in full positive RDF/XML parsing compliance
* with the possible exception of handling deprecated RDF attributes
* appropriately. Parser is believed to comply fully with other W3C
* and industry standards where appropriate (DOM, ECMAScript, &c.)
*
* Author: David Sheets <[email protected]>
*
* W3C® SOFTWARE NOTICE AND LICENSE
* http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
* This work (and included software, documentation such as READMEs, or
* other related items) is being provided by the copyright holders under
* the following license. By obtaining, using and/or copying this work,
* you (the licensee) agree that you have read, understood, and will
* comply with the following terms and conditions.
*
* Permission to copy, modify, and distribute this software and its
* documentation, with or without modification, for any purpose and
* without fee or royalty is hereby granted, provided that you include
* the following on ALL copies of the software and documentation or
* portions thereof, including modifications:
*
* 1. The full text of this NOTICE in a location viewable to users of
* the redistributed or derivative work.
* 2. Any pre-existing intellectual property disclaimers, notices, or terms and
* conditions. If none exist, the W3C Software Short Notice should be
* included (hypertext is preferred, text is permitted) within the body
* of any redistributed or derivative code.
* 3. Notice of any changes or modifications to the files, including the
* date changes were made. (We recommend you provide URIs to the location
* from which the code is derived.)
*
* THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT
* HOLDERS MAKE NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY OR FITNESS
* FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THE SOFTWARE OR
* DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS,
* TRADEMARKS OR OTHER RIGHTS.
*
* COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL
* OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE OR
* DOCUMENTATION.
*
* The name and trademarks of copyright holders may NOT be used in
* advertising or publicity pertaining to the software without specific,
* written prior permission. Title to copyright in this software and any
* associated documentation will at all times remain with copyright
* holders.
*/
/**
* @class Class defining an RDFParser resource object tied to an RDFStore
*
* @author David Sheets <[email protected]>
* @version 0.1
*
* @constructor
* @param {RDFStore} store An RDFStore object
*/
$rdf.RDFParser = function (store) {
var RDFParser = {}
/** Standard namespaces that we know how to handle @final
* @member RDFParser
*/
RDFParser.ns = {'RDF': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'RDFS': 'http://www.w3.org/2000/01/rdf-schema#'}
/** DOM Level 2 node type magic numbers @final
* @member RDFParser
*/
RDFParser.nodeType = {'ELEMENT': 1, 'ATTRIBUTE': 2, 'TEXT': 3,
'CDATA_SECTION': 4, 'ENTITY_REFERENCE': 5,
'ENTITY': 6, 'PROCESSING_INSTRUCTION': 7,
'COMMENT': 8, 'DOCUMENT': 9, 'DOCUMENT_TYPE': 10,
'DOCUMENT_FRAGMENT': 11, 'NOTATION': 12}
/**
* Frame class for namespace and base URI lookups
* Base lookups will always resolve because the parser knows
* the default base.
*
* @private
*/
this.frameFactory = function (parser, parent, element) {
return {'NODE': 1, 'ARC': 2, 'parent': parent, 'parser': parser, 'store': parser.store, 'element': element,
'lastChild': 0, 'base': null, 'lang': null, 'node': null, 'nodeType': null, 'listIndex': 1, 'rdfid': null, 'datatype': null, 'collection': false, /** Terminate the frame and notify the store that we're done */
'terminateFrame': function () {
if (this.collection) {
this.node.close()
}
}, /** Add a symbol of a certain type to the this frame */'addSymbol': function (type, uri) {
uri = $rdf.Util.uri.join(uri, this.base)
this.node = this.store.sym(uri)
this.nodeType = type
}, /** Load any constructed triples into the store */'loadTriple': function () {
if (this.parent.parent.collection) {
this.parent.parent.node.append(this.node)
} else {
this.store.add(this.parent.parent.node, this.parent.node, this.node, this.parser.why)
}
if (this.parent.rdfid != null) {
// reify
var triple = this.store.sym($rdf.Util.uri.join('#' + this.parent.rdfid, this.base))
this.store.add(triple, this.store.sym(RDFParser.ns.RDF + 'type'), this.store.sym(RDFParser.ns.RDF + 'Statement'), this.parser.why)
this.store.add(triple, this.store.sym(RDFParser.ns.RDF + 'subject'), this.parent.parent.node, this.parser.why)
this.store.add(triple, this.store.sym(RDFParser.ns.RDF + 'predicate'), this.parent.node, this.parser.why)
this.store.add(triple, this.store.sym(RDFParser.ns.RDF + 'object'), this.node, this.parser.why)
}
}, /** Check if it's OK to load a triple */'isTripleToLoad': function () {
return (this.parent != null && this.parent.parent != null && this.nodeType === this.NODE && this.parent.nodeType ===
this.ARC && this.parent.parent.nodeType === this.NODE)
}, /** Add a symbolic node to this frame */'addNode': function (uri) {
this.addSymbol(this.NODE, uri)
if (this.isTripleToLoad()) {
this.loadTriple()
}
}, /** Add a collection node to this frame */'addCollection': function () {
this.nodeType = this.NODE
this.node = this.store.collection()
this.collection = true
if (this.isTripleToLoad()) {
this.loadTriple()
}
}, /** Add a collection arc to this frame */'addCollectionArc': function () {
this.nodeType = this.ARC
}, /** Add a bnode to this frame */'addBNode': function (id) {
if (id != null) {
if (this.parser.bnodes[id] != null) {
this.node = this.parser.bnodes[id]
} else {
this.node = this.parser.bnodes[id] = this.store.bnode()
}
} else {
this.node = this.store.bnode()
}
this.nodeType = this.NODE
if (this.isTripleToLoad()) {
this.loadTriple()
}
}, /** Add an arc or property to this frame */'addArc': function (uri) {
if (uri === RDFParser.ns.RDF + 'li') {
uri = RDFParser.ns.RDF + '_' + this.parent.listIndex
this.parent.listIndex++
}
this.addSymbol(this.ARC, uri)
}, /** Add a literal to this frame */'addLiteral': function (value) {
if (this.parent.datatype) {
this.node = this.store.literal(value, '', this.store.sym(this.parent.datatype))
} else {
this.node = this.store.literal(value, this.lang)
}
this.nodeType = this.NODE
if (this.isTripleToLoad()) {
this.loadTriple()
}
}
}
}
// from the OpenLayers source .. needed to get around IE problems.
this.getAttributeNodeNS = function (node, uri, name) {
var attributeNode = null
if (node.getAttributeNodeNS) {
attributeNode = node.getAttributeNodeNS(uri, name)
} else {
var attributes = node.attributes
var potentialNode, fullName
for (var i = 0;i < attributes.length; ++i) {
potentialNode = attributes[i]
if (potentialNode.namespaceURI === uri) {
fullName = (potentialNode.prefix) ? (potentialNode.prefix + ':' + name) : name
if (fullName === potentialNode.nodeName) {
attributeNode = potentialNode
break
}
}
}
}
return attributeNode
}
/** Our triple store reference @private */
this.store = store /** Our identified blank nodes @private */
this.bnodes = {} /** A context for context-aware stores @private */
this.why = null /** Reification flag */
this.reify = false
/**
* Build our initial scope frame and parse the DOM into triples
* @param {DOMTree} document The DOM to parse
* @param {String} base The base URL to use
* @param {Object} why The context to which this resource belongs
*/
this.parse = function (document, base, why) {
var children = document.childNodes // clean up for the next run
this.cleanParser() // figure out the root element
var root
if (document.nodeType === RDFParser.nodeType.DOCUMENT) {
for (var c = 0;c < children.length;c++) {
if (children[c].nodeType === RDFParser.nodeType.ELEMENT) {
root = children[c]
break
}
}
} else if (document.nodeType === RDFParser.nodeType.ELEMENT) {
root = document
} else {
throw new Error("RDFParser: can't find root in " + base + '. Halting. ')
// return false
}
this.why = why // our topmost frame
var f = this.frameFactory(this)
this.base = base
f.base = base
f.lang = null // was '' but can't have langs like that 2015 (!)
this.parseDOM(this.buildFrame(f, root))
return true
}
var XMLSerializer = require('xmldom').XMLSerializer
this.parseDOM = function (frame) {
// a DOM utility function used in parsing
var rdfid
var elementURI = function (el) {
var result = ''
if (el.namespaceURI == null) {
throw new Error('RDF/XML syntax error: No namespace for ' + el.localName + ' in ' + this.base)
}
if (el.namespaceURI) {
result = result + el.namespaceURI
}
if (el.localName) {
result = result + el.localName
} else if (el.nodeName) {
if (el.nodeName.indexOf(':') >= 0)result = result + el.nodeName.split(':')[1]
else result = result + el.nodeName
}
return result
}.bind(this)
var dig = true // if we'll dig down in the tree on the next iter
while (frame.parent) {
var dom = frame.element
var attrs = dom.attributes
if (dom.nodeType === RDFParser.nodeType.TEXT || dom.nodeType === RDFParser.nodeType.CDATA_SECTION) {
// we have a literal
if (frame.parent.nodeType === frame.NODE) {
// must have had attributes, store as rdf:value
frame.addArc(RDFParser.ns.RDF + 'value')
frame = this.buildFrame(frame)
}
frame.addLiteral(dom.nodeValue)
} else if (elementURI(dom) !== RDFParser.ns.RDF + 'RDF') {
// not root
if (frame.parent && frame.parent.collection) {
// we're a collection element
frame.addCollectionArc()
frame = this.buildFrame(frame, frame.element)
frame.parent.element = null
}
if (!frame.parent || !frame.parent.nodeType || frame.parent.nodeType === frame.ARC) {
// we need a node
var about = this.getAttributeNodeNS(dom, RDFParser.ns.RDF, 'about')
rdfid = this.getAttributeNodeNS(dom, RDFParser.ns.RDF, 'ID')
if (about && rdfid) {
throw new Error('RDFParser: ' + dom.nodeName + ' has both rdf:id and rdf:about.' +
' Halting. Only one of these' + ' properties may be specified on a' + ' node.')
}
if (!about && rdfid) {
frame.addNode('#' + rdfid.nodeValue)
dom.removeAttributeNode(rdfid)
} else if (about == null && rdfid == null) {
var bnid = this.getAttributeNodeNS(dom, RDFParser.ns.RDF, 'nodeID')
if (bnid) {
frame.addBNode(bnid.nodeValue)
dom.removeAttributeNode(bnid)
} else {
frame.addBNode()
}
} else {
frame.addNode(about.nodeValue)
dom.removeAttributeNode(about)
}
// Typed nodes
var rdftype = this.getAttributeNodeNS(dom, RDFParser.ns.RDF, 'type')
if (RDFParser.ns.RDF + 'Description' !== elementURI(dom)) {
rdftype = {'nodeValue': elementURI(dom)}
}
if (rdftype != null) {
this.store.add(frame.node, this.store.sym(RDFParser.ns.RDF + 'type'), this.store.sym($rdf.Util.uri.join(rdftype.nodeValue,
frame.base)), this.why)
if (rdftype.nodeName) {
dom.removeAttributeNode(rdftype)
}
}
// Property Attributes
for (var x = attrs.length - 1; x >= 0; x--) {
this.store.add(frame.node, this.store.sym(elementURI(attrs[x])), this.store.literal(attrs[x].nodeValue,
frame.lang), this.why)
}
} else {
// we should add an arc (or implicit bnode+arc)
frame.addArc(elementURI(dom)) // save the arc's rdf:ID if it has one
if (this.reify) {
rdfid = this.getAttributeNodeNS(dom, RDFParser.ns.RDF, 'ID')
if (rdfid) {
frame.rdfid = rdfid.nodeValue
dom.removeAttributeNode(rdfid)
}
}
var parsetype = this.getAttributeNodeNS(dom, RDFParser.ns.RDF, 'parseType')
var datatype = this.getAttributeNodeNS(dom, RDFParser.ns.RDF, 'datatype')
if (datatype) {
frame.datatype = datatype.nodeValue
dom.removeAttributeNode(datatype)
}
if (parsetype) {
var nv = parsetype.nodeValue
if (nv === 'Literal') {
frame.datatype = RDFParser.ns.RDF + 'XMLLiteral' // (this.buildFrame(frame)).addLiteral(dom)
// should work but doesn't
frame = this.buildFrame(frame)
// The property value should be an XML string
// TODO: The DOM should be normalized to support === before serializing
var serializer = new XMLSerializer()
var value=''
for (var c=0; c<dom.childNodes.length; c++) {
value += serializer.serializeToString(dom.childNodes[c])
}
frame.addLiteral(value)
dig = false
} else if (nv === 'Resource') {
frame = this.buildFrame(frame, frame.element)
frame.parent.element = null
frame.addBNode()
} else if (nv === 'Collection') {
frame = this.buildFrame(frame, frame.element)
frame.parent.element = null
frame.addCollection()
}
dom.removeAttributeNode(parsetype)
}
if (attrs.length !== 0) {
var resource = this.getAttributeNodeNS(dom, RDFParser.ns.RDF, 'resource')
var bnid2 = this.getAttributeNodeNS(dom, RDFParser.ns.RDF, 'nodeID')
frame = this.buildFrame(frame)
if (resource) {
frame.addNode(resource.nodeValue)
dom.removeAttributeNode(resource)
} else {
if (bnid2) {
frame.addBNode(bnid2.nodeValue)
dom.removeAttributeNode(bnid2)
} else {
frame.addBNode()
}
}
for (var x1 = attrs.length - 1; x1 >= 0; x1--) {
var f = this.buildFrame(frame)
f.addArc(elementURI(attrs[x1]))
if (elementURI(attrs[x1]) === RDFParser.ns.RDF + 'type') {
(this.buildFrame(f)).addNode(attrs[x1].nodeValue)
} else {
(this.buildFrame(f)).addLiteral(attrs[x1].nodeValue)
}
}
} else if (dom.childNodes.length === 0) {
(this.buildFrame(frame)).addLiteral('')
}
}
} // rdf:RDF
// dig dug
dom = frame.element
while (frame.parent) {
var pframe = frame
while (dom == null) {
frame = frame.parent
dom = frame.element
}
var candidate = dom.childNodes && dom.childNodes[frame.lastChild]
if (!candidate || !dig) {
frame.terminateFrame()
if (!(frame = frame.parent)) {
break
} // done
dom = frame.element
dig = true
} else if ((candidate.nodeType !== RDFParser.nodeType.ELEMENT &&
candidate.nodeType !== RDFParser.nodeType.TEXT &&
candidate.nodeType !== RDFParser.nodeType.CDATA_SECTION) ||
((candidate.nodeType === RDFParser.nodeType.TEXT ||
candidate.nodeType === RDFParser.nodeType.CDATA_SECTION) &&
dom.childNodes.length !== 1)) {
frame.lastChild++
} else {
// not a leaf
frame.lastChild++
frame = this.buildFrame(pframe, dom.childNodes[frame.lastChild - 1])
break
}
}
} // while
}
/**
* Cleans out state from a previous parse run
* @private
*/
this.cleanParser = function () {
this.bnodes = {}
this.why = null
}
/**
* Builds scope frame
* @private
*/
this.buildFrame = function (parent, element) {
var frame = this.frameFactory(this, parent, element)
if (parent) {
frame.base = parent.base
frame.lang = parent.lang
}
if (!element || element.nodeType === RDFParser.nodeType.TEXT ||
element.nodeType === RDFParser.nodeType.CDATA_SECTION) {
return frame
}
var attrs = element.attributes
var base = element.getAttributeNode('xml:base')
if (base != null) {
frame.base = base.nodeValue
element.removeAttribute('xml:base')
}
var lang = element.getAttributeNode('xml:lang')
if (lang != null) {
frame.lang = lang.nodeValue
element.removeAttribute('xml:lang')
}
// remove all extraneous xml and xmlns attributes
for (var x = attrs.length - 1;x >= 0;x--) {
if (attrs[x].nodeName.substr(0, 3) === 'xml') {
if (attrs[x].name.slice(0, 6) === 'xmlns:') {
var uri = attrs[x].nodeValue // alert('base for namespac attr:'+this.base)
if (this.base) uri = $rdf.Util.uri.join(uri, this.base)
this.store.setPrefixForURI(attrs[x].name.slice(6), uri)
}
// alert('rdfparser: xml atribute: '+attrs[x].name) //@@
element.removeAttributeNode(attrs[x])
}
}
return frame
}
}