Class: RDFObject::NTriplesParser

Inherits:
Parser
  • Object
show all
Defined in:
lib/rdf_objects/parsers.rb

Instance Attribute Summary

Attributes inherited from Parser

#base_uri, #collection

Instance Method Summary collapse

Methods inherited from Parser

init_parser, #initialize, parse, #sanitize_uri

Constructor Details

This class inherits a constructor from RDFObject::Parser

Instance Method Details

#data=(ntriples) ⇒ Object



222
223
224
225
226
227
228
229
230
# File 'lib/rdf_objects/parsers.rb', line 222

def data=(ntriples)
  if ntriples.is_a?(String)
    @ntriples = ntriples.split("\n")
  elsif ntriples.is_a?(Array)
    @ntriples = ntriples
  elsif ntriples.respond_to?(:read)
    @ntriples = ntriples.readlines
  end      
end

#parseObject



232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/rdf_objects/parsers.rb', line 232

def parse
  @ntriples.each do | assertion |
    next if assertion[0, 1] == "#" # Ignore comments
    triple = parse_ntriple(assertion)
    resource = @collection.find_or_create(triple[:subject])
    object = case triple[:type]
    when "literal" then triple[:object]
    when "uri" then @collection.find_or_create(triple[:object])
    when "bnode" then @collection.find_or_create(triple[:object])
    end
    resource.assert(triple[:predicate],object)
  end
  @collection
end

#parse_ntriple(ntriple) ⇒ Object



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