Class: RDF::Graph::Sesame::XMLParser

Inherits:
WWW::Mechanize::File
  • Object
show all
Defined in:
lib/rdf/graph/sesame/xml_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri = nil, response = nil, body = nil, code = nil) ⇒ XMLParser

Returns a new instance of XMLParser.



8
9
10
11
# File 'lib/rdf/graph/sesame/xml_parser.rb', line 8

def initialize(uri=nil, response=nil, body=nil, code=nil)
  super(uri, response, body, code)
  @parser = Hpricot.XML(body)
end

Instance Attribute Details

#parserObject (readonly) Also known as: root

Returns the value of attribute parser.



5
6
7
# File 'lib/rdf/graph/sesame/xml_parser.rb', line 5

def parser
  @parser
end

Instance Method Details

#parse_node(node, graph) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rdf/graph/sesame/xml_parser.rb', line 33

def parse_node(node, graph)
  if node.name == 'uri'
    RDF::UriNode.new(node.inner_html)
  elsif node.name == 'bNode'
    RDF::BlankNode.new(node.inner_html, graph)
  elsif node.name == 'literal'
    if node['datatype']
      RDF::TypedLiteralNode.new(node.inner_html, node['datatype'])
    elsif node['xml:lang']
      RDF::PlainLiteralNode.new(node.inner_html, node['xml:lang'])
    end
  end
end

#result(graph) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rdf/graph/sesame/xml_parser.rb', line 17

def result(graph)
  header_map = []
  @parser.search('tableQueryResult/header/columnName').each do |c|
    header_map << c.inner_html
  end
  
  @parser.search('tableQueryResult/tuple').inject(RDF::Query::Result.success!) do |r, row|
    b = RDF::Query::Binding.new
    header_map.each_with_index do |name, i|
      b[name.to_sym] = parse_node(row.containers[i], graph)
    end
    r.bindings << b
    r
  end
end

#solution?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/rdf/graph/sesame/xml_parser.rb', line 13

def solution?
  !@parser.search('tableQueryResult/tuple').empty?
end