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
|
# File 'lib/rdf_objects/parsers.rb', line 167
def parse_ntriple(ntriple)
if ntriple.respond_to?(:force_encoding)
ntriple.force_encoding("ASCII-8BIT")
end
scanner = StringScanner.new(ntriple)
if ntriple[0,1] == "<"
subject = scanner.scan_until(/> /)
subject.sub!(/^</,'')
subject.sub!(/> $/,'')
else
subject = scanner.scan_until(/\w /)
subject.strip!
end
predicate = scanner.scan_until(/> /)
predicate.sub!(/^</,'')
predicate.sub!(/> $/,'')
if scanner.match?(/</)
tmp_object = scanner.scan_until(/>\s?\.\s*\n?$/)
tmp_object.sub!(/^</,'')
tmp_object.sub!(/>\s?\.\s*\n?$/,'')
object = self.sanitize_uri(tmp_object)
type = "uri"
elsif scanner.match?(/_:/)
object = scanner.scan_until(/\w\s?\.\s*\n?$/)
object.sub!(/\s?\.\s*\n?$/,'')
type = "bnode"
else
language = nil
data_type = nil
scanner.getch
tmp_object = scanner.scan_until(/("\s?\.\s*\n?$)|("@[A-z])|("\^\^)/)
scanner.pos=(scanner.pos-2)
tmp_object.sub!(/"..?$/,'')
if tmp_object.respond_to?(:force_encoding)
tmp_object.force_encoding('utf-8').chomp!
else
uscan = UTF8Parser.new(tmp_object)
tmp_object = uscan.parse_string.chomp
end
if scanner.match?(/@/)
scanner.getch
language = scanner.scan_until(/\s?\.\n?$/)
language.sub!(/\s?\.\n?$/,'')
language = language.to_sym
elsif scanner.match?(/\^\^/)
scanner.skip_until(/</)
data_type = scanner.scan_until(/>/)
data_type.sub!(/>$/,'')
end
object = RDF::Literal.new(tmp_object,{:datatype=>data_type,:language=>language})
type = "literal"
end
{:subject=>subject, :predicate=>predicate, :object=>object, :type=>type}
end
|