Class: RDFObject::NTriplesParser

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line) ⇒ NTriplesParser

Returns a new instance of NTriplesParser.



62
63
64
65
# File 'lib/rdf_objects/parsers.rb', line 62

def initialize(line)
  @ntriple = line
  parse_ntriple
end

Instance Attribute Details

#data_typeObject (readonly)

Returns the value of attribute data_type.



60
61
62
# File 'lib/rdf_objects/parsers.rb', line 60

def data_type
  @data_type
end

#languageObject (readonly)

Returns the value of attribute language.



60
61
62
# File 'lib/rdf_objects/parsers.rb', line 60

def language
  @language
end

#literalObject (readonly)

Returns the value of attribute literal.



60
61
62
# File 'lib/rdf_objects/parsers.rb', line 60

def literal
  @literal
end

#ntripleObject (readonly)

Returns the value of attribute ntriple.



60
61
62
# File 'lib/rdf_objects/parsers.rb', line 60

def ntriple
  @ntriple
end

#objectObject

Returns the value of attribute object.



61
62
63
# File 'lib/rdf_objects/parsers.rb', line 61

def object
  @object
end

#predicateObject (readonly)

Returns the value of attribute predicate.



60
61
62
# File 'lib/rdf_objects/parsers.rb', line 60

def predicate
  @predicate
end

#subjectObject (readonly)

Returns the value of attribute subject.



60
61
62
# File 'lib/rdf_objects/parsers.rb', line 60

def subject
  @subject
end

Class Method Details

.parse(resources) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rdf_objects/parsers.rb', line 101

def self.parse(resources)
  collection = []
  if resources.is_a?(String)
    assertions = resources.split("\n")
  elsif resources.is_a?(Array)
    assertions = resources
  elsif resources.respond_to?(:read)
    assertions = resources.readlines
  end
  assertions.each do | assertion |
    triple = self.new(assertion)
    resource = Resource.new(triple.subject)
    resource.assert(triple.predicate, triple.object)
    collection << resource
  end
  collection.uniq!
end

Instance Method Details

#parse_ntripleObject



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
# File 'lib/rdf_objects/parsers.rb', line 67

def parse_ntriple
  scanner = StringScanner.new(@ntriple)
  @subject = scanner.scan_until(/> /)
  @subject.sub!(/^</,'')
  @subject.sub!(/> $/,'')
  @predicate = scanner.scan_until(/> /)
  @predicate.sub!(/^</,'')
  @predicate.sub!(/> $/,'')
  if scanner.match?(/</)
    object = scanner.scan_until(/>\s?\.\s*\n?$/)
    object.sub!(/^</,'')
    object.sub!(/>\s?\.\s*\n?$/,'')
    @object = Resource.new(object)
  else
    @literal = true
    scanner.getch
    object = scanner.scan_until(/("\s?\.\n?$)|("@[A-z])|("\^\^)/)
    scanner.pos=(scanner.pos-2)
    object.sub!(/"..$/,'')
    uscan = UTF8Parser.new(object)
    object = uscan.parse_string
    if scanner.match?(/@/)
      scanner.getch
      @language = scanner.scan_until(/\s?\.\n?$/)
      @language.sub!(/\s?\.\n?$/,'')
    elsif scanner.match?(/\^\^/)
      scanner.skip_until(/</)
      @data_type = scanner.scan_until(/>/)
      @data_type.sub!(/>$/,'')
    end
    @object = Literal.new(object,{:data_type=>@data_type,:language=>@language})      
  end
end