-
Notifications
You must be signed in to change notification settings - Fork 7
/
normalize_update_rdf.rb
executable file
·215 lines (167 loc) · 5.01 KB
/
normalize_update_rdf.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
#!/usr/bin/env ruby
# Copyright (C) 2007 by Massimiliano Mirra
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# Author: Massimiliano Mirra, <bard [at] hyperstruct [dot] net>
require 'xml/libxslt'
require 'strscan'
INDENT = ' '
NS_EM = 'http://www.mozilla.org/2004/em-rdf#'
class String
def begins_with?(prefix)
prefix == slice(0,prefix.length)
end
def ends_with?(suffix)
suffix == slice(-suffix.length..-1)
end
end
######################################################################
# Main interface
def normalize_update_rdf(filename, ext_id, &block)
serialize_resource(parse_nt(rdf2nt(filename)), ext_id, &block)
end
######################################################################
# Implementation
# Convert RDF into n-triples form, for easier parsing
def rdf2nt(filename)
xslt = XML::XSLT.new
xslt.xml = filename
xslt.xsl = File.dirname(__FILE__) + '/rdf2nt.xsl'
xslt.serve
end
def parse_nt(ntriples_dump)
triples = { }
ntriples_dump.split("\n").each do |line|
s = StringScanner.new(line)
subj = s.scan(/_:id\d+|<[^>]+>/).gsub(/^<|>$/, '')
s.skip(/\s+/)
pred = s.scan(/<[^>]+>/).gsub(/^<|>$/, '')
s.skip(/\s+/)
obj = s.scan(/_:id\d+|"[^"]*"|<[^>]+>/).gsub(/^<|>$/, '').gsub(/^"|"$/, '')
triples[subj] ||= []
triples[subj] << [subj, pred, obj]
s.skip(/\s+/)
s.scan(/\./) or raise RuntimeError
end
triples
end
# Translated from mccoy/chrome/content/rdfserializer.js
def serialize_resource(ds, subj, indent = '', &block)
if is_seq(ds, subj)
type = 'Seq'
container = make_seq(ds, subj)
elsif is_alt(ds, subj)
type = 'Alt'
container = make_alt(ds, subj)
elsif is_bag(ds, subj)
type = 'Bag'
container = make_bag(ds, subj)
else
type = 'Description'
container = nil
end
result = indent + '<RDF:' + type
unless is_anonymous_resource(subj)
result << ' about="' << subj << '"'
end
result << ">\n"
if container
result << serialize_container_items(ds, container, indent + INDENT, &block)
end
result << serialize_resource_properties(ds, subj, indent + INDENT, &block)
result << indent << '</RDF:' << type << ">\n"
result
end
def serialize_resource_properties(ds, subj, indent, &block)
result = ''
if not ds[subj]
return result
end
items = []
for resource in ds[subj]
subj, pred, obj = resource
if block_given?
obj = yield(subj, pred, obj) || obj
end
pred.begins_with?(NS_EM) or next
#pred.ends_with?('#signature') and next
prop = pred.slice(NS_EM.length..-1)
if obj.begins_with?('_')
item = indent + '<em:' + prop + ">\n"
item << serialize_resource(ds, obj, indent + INDENT, &block)
item << indent << "</em:" + prop + ">\n"
items.push(item)
else
items.push(indent + '<em:' + prop + '>' + escape_entities(value_of(obj)) + '</em:' + prop + ">\n")
end
end
result << items.sort.join
result
end
def escape_entities(s)
s # XXX todo
end
def value_of(s)
s # XXX todo
end
def serialize_container_items(ds, container, indent, &block)
result = ''
for resource in container
subj, pred, obj = resource
result << indent << "<RDF:li>\n"
result << serialize_resource(ds, subj, indent + INDENT, &block)
result << indent << "</RDF:li>\n"
end
result
end
def is_anonymous_resource(subj)
subj[0,1] == '_'
end
def is_seq(ds, subj)
ds[subj] &&
ds[subj].any? {|subj, pred, obj| obj == "http://www.w3.org/1999/02/22-rdf-syntax-ns#Seq"}
end
def is_alt(ds, subj)
ds[subj] &&
ds[subj].any? {|subj, pred, obj| obj == "http://www.w3.org/1999/02/22-rdf-syntax-ns#Alt"}
end
def is_bag(ds, subj)
ds[subj] &&
ds[subj].any? {|subj, pred, obj| obj == "http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag"}
end
def make_seq(ds, subj)
ds[subj].select do |subj, pred, obj|
obj != "http://www.w3.org/1999/02/22-rdf-syntax-ns#Seq"
end.map do |subj, pred, obj|
obj
end
end
def make_alt(ds, resource)
raise Error.new('Not implemented')
end
def make_bag(ds, resource)
raise Error.new('Not implemented')
end
######################################################################
# Run as standalone
if __FILE__ == $0
filename, ext_id = *ARGV
unless filename and ext_id
STDERR.puts "#{File.basename($0)}: no update.rdf given."
abort
end
puts normalize_update_rdf(filename, ext_id)
end