/
rdfxmlparser.rb
279 lines (249 loc) · 9.62 KB
/
rdfxmlparser.rb
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
require 'lib/rena'
require 'ruby-debug'
require 'xml'
include Rena
module Rena
include LibXML
class RdfXmlParser
SYNTAX_BASE = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
RDF_TYPE = SYNTAX_BASE + "type"
RDF_DESCRIPTION = SYNTAX_BASE + "Description"
attr_accessor :xml, :graph
def initialize(xml_str, uri = nil)
@excl = ["http://www.w3.org/1999/02/22-rdf-syntax-ns#resource",
"http://www.w3.org/1999/02/22-rdf-syntax-ns#nodeID",
"http://www.w3.org/1999/02/22-rdf-syntax-ns#about",
"http://www.w3.org/1999/02/22-rdf-syntax-ns#ID"]
@uri = Addressable::URI.parse(uri).to_s unless uri.nil?
@graph = Rena::Graph.new
@xml = LibXML::XML::Parser.string(xml_str).parse
@id_mapping = Hash.new
root = @xml.root
if is_rdf_root?(root)
parse_descriptions(root)
else
root.each {|n|
if is_rdf_root?(n)
parse_descriptions(n)
debugger
end
}
end
end
private
def is_rdf_root? (node)
if node.namespace_node.href == SYNTAX_BASE && node.name == "RDF"
return true
else
return false
end
end
private
def fail_check(el)
if el.attributes.get_attribute_ns(SYNTAX_BASE, "aboutEach")
raise Rena::AboutEachException
end
if el.attributes.get_attribute_ns(SYNTAX_BASE, "aboutEachPrefix")
raise Rena::AboutEachException
end
if el.attributes.get_attribute_ns(SYNTAX_BASE, "bagID")
raise "Bad BagID" unless el.attributes.get_attribute_ns(SYNTAX_BASE, "bagID").value =~ /^[a-zA-Z_][a-zA-Z0-9]*$/
end
end
def parse_subject(el)
fail_check(el)
if el.attributes.get_attribute_ns(SYNTAX_BASE, "about")
debugger if el.attributes.get_attribute_ns(SYNTAX_BASE, "about").value =~ /artist$/
return URIRef.new(base_helper(el.attributes.get_attribute_ns(SYNTAX_BASE, "about").value, el.base).to_s)
elsif el.attributes.get_attribute_ns(SYNTAX_BASE, "ID")
id = el.attributes.get_attribute_ns(SYNTAX_BASE, "ID")
if id_check?(id.value)
return url_helper("#" + id.value, "", el.base)
else
raise
end
elsif el.attributes.get_attribute_ns(SYNTAX_BASE, "nodeID")
return BNode.new(el.attributes.get_attribute_ns(SYNTAX_BASE, "nodeID").value)
else
return BNode.new
end
subject = nil
el.attributes.each_attribute do |att|
uri = url_helper(att.namespace + att.name).to_s
value = att.to_s
if uri == "http://www.w3.org/1999/02/22-rdf-syntax-ns#bagID"
raise
if name =~ /^[a-zA-Z_][a-zA-Z0-9]*$/
# TODO: do something intelligent with the bagID
else
raise
end
end
if uri == SYNTAX_BASE + "#resource" || uri == SYNTAX_BASE + "#about" #specified resource
subject = URIRef.new(base_helper(value, el.base))
end
if uri.to_s == SYNTAX_BASE + "#nodeID" #BNode with ID
# we have a BNode with an identifier. First, we need to do syntax checking.
if value =~ /^[a-zA-Z_][a-zA-Z0-9]*$/
# now we check to see if the graph has the value
return forge_bnode_from_string(value)
end
end
end
return subject
end
def forge_bnode_from_string(value)
if @graph.has_bnode_identifier?(value)
# if so, pull it in - no need to recreate objects.
subject = @graph.get_bnode_by_identifier(value)
else
# if not, create a new one.
subject = BNode.new(value)
end
return subject
end
def id_check?(id)
!(!(id =~ /^[a-zA-Z_]\w*$/))
end
def parse_object_atts (el)
if el.attributes.get_attribute_ns(SYNTAX_BASE, "resource")
return URIRef.new(base_helper(el.attributes.get_attribute_ns(SYNTAX_BASE, "resource").value, el.base).to_s)
end
end
def parse_descriptions (node, subject = nil)
node.each_element { |el|
fail_check(el)
# detect a subject
if @id_mapping[node.hash]
subject = @id_mapping[node.id]
elsif subject.nil?
subject = parse_subject(el)
end
# find a class
unless el.name == "Description" && el.namespace_node.href == SYNTAX_BASE
@graph.add_triple(subject, RDF_TYPE, url_helper(el.name, el.namespace_node.href, el.base))
end
# read each attribute that's not in SYNTAX_BASE
el.attributes.each { |att|
@graph.add_triple(subject, url_helper(att.name, att.ns.href, el.base), att.value) unless att.ns.href == SYNTAX_BASE
}
el.each_element {|child|
predicate = url_helper(child.name, child.namespace_node.href, child.base)
object = child.content
if el.attributes.get_attribute_ns(SYNTAX_BASE, "nodeID")
@graph.add_triple(subject, predicate, forge_bnode_from_string(child.attributes.get_attribute_ns(SYNTAX_BASE, "nodeID").value))
elsif child.attributes.get_attribute_ns(SYNTAX_BASE, "resource")
@graph.add_triple(subject, predicate, URIRef.new(base_helper(child.attributes.get_attribute_ns(SYNTAX_BASE, "resource").value, child.base).to_s))
end
child.each {|contents|
if contents.text? and contents.content.strip.length != 0
object = contents.content
@graph.add_triple(subject, predicate, object)
end
}
child.each_element {|cel|
object = parse_subject(cel)
if child.attributes.get_attribute_ns(SYNTAX_BASE, "parseType")
case child.attributes.get_attribute_ns(SYNTAX_BASE, "parseType").value
when "XMLLiteral"
object = Literal.typed(cel.namespaced_to_s, "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral")
@graph.add_triple(subject, predicate, object)
when "Literal"
if smells_like_xml?(cel.namespaced_to_s)
object = Literal.typed(cel.to_s, "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral")
@graph.add_triple(subject, predicate, object)
else
object = cel.to_s
@graph.add_triple(subject, predicate, object)
end
when "Resource"
object = BNode.new
@graph.add_triple(subject, predicate, object)
parse_descriptions(cel, resource)
#when "Collection";
end
else
@id_mapping[cel.hash] = object
@graph.add_triple(subject, predicate, object)
parse_descriptions(cel.parent, object)
end
}
if child.attributes.get_attribute_ns(SYNTAX_BASE, "ID")
if id_check?(child.attributes.get_attribute_ns(SYNTAX_BASE, "ID").value)
rsubject = url_helper("#" + child.attributes.get_attribute_ns(SYNTAX_BASE, "ID").value, child.base)
@graph.add_triple(rsubject, URIRef.new("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), URIRef.new("http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement"))
@graph.add_triple(rsubject, URIRef.new("http://www.w3.org/1999/02/22-rdf-syntax-ns#subject"), subject)
@graph.add_triple(rsubject, URIRef.new("http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate"), predicate)
@graph.add_triple(rsubject, URIRef.new("http://www.w3.org/1999/02/22-rdf-syntax-ns#object"), object)
else
raise
end
end
}
}
end
def levels_to_root (el, num = 0)
if el.parent == @xml.root
return num
else
levels_to_root el.parent, num + 1
end
end
protected
def parse_element (element, subject = nil, resource = false)
if subject == nil
# figure out subject
subject = self.get_uri_from_atts(element, true)
end
# type parsing
if (resource == true or element.attributes.has_key? 'about')
type = URIRef.new(element.namespace + element.name)
unless type.to_s == RDF_TYPE
@graph.add_triple(subject, RDF_DESCRIPTION, type)
end
end
# attribute parsing
element.attributes.each_attribute { |att|
uri = att.namespace + att.name
value = att.to_s
unless @excl.member? uri
@graph.add_triple(subject, uri, Literal.untyped(value))
end
}
# element parsing
element.each_element { |e|
self.parse_resource_element e, subject
}
end
def smells_like_xml?(str)
!(!(str =~ /xmlns/))
end
def base_helper(uri, base = nil)
uri = Addressable::URI.parse(uri)
if uri.relative?
if !base.nil?
uri = Addressable::URI.parse(base)
elsif !@uri.nil?
uri = Addressable::URI.parse(@uri) + uri
end
end
#debugger if @uri.to_s =~ /bbc\.co\.uk/
return uri.to_s
end
def url_helper(name, ns, base = nil)
if ns != "" and !ns.nil?
if ns.to_s.split("")[-1] == "#"
a = Addressable::URI.parse(ns) + Addressable::URI.parse("#" + name)
else
a = Addressable::URI.parse(ns) + Addressable::URI.parse(name)
end
else
a = Addressable::URI.parse(name)
end
if a.relative?
a = base_helper(a.to_s, base)
end
return URIRef.new(a.to_s)
end
end
end