Class: Reddy::RdfaParser

Inherits:
Object
  • Object
show all
Defined in:
lib/reddy/rdfaparser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str, uri) ⇒ RdfaParser

Returns a new instance of RdfaParser.



5
6
7
8
9
10
11
# File 'lib/reddy/rdfaparser.rb', line 5

def initialize (str, uri)
  @doc_string = str
  @xml = REXML::Document.new(str)
  @uri = uri
  @graph = Graph.new
  self.iterate(@xml.root.elements[2].elements[1].elements[1])
end

Instance Attribute Details

#graphObject

Returns the value of attribute graph.



3
4
5
# File 'lib/reddy/rdfaparser.rb', line 3

def graph
  @graph
end

#uriObject

Returns the value of attribute uri.



3
4
5
# File 'lib/reddy/rdfaparser.rb', line 3

def uri
  @uri
end

#xmlObject

Returns the value of attribute xml.



3
4
5
# File 'lib/reddy/rdfaparser.rb', line 3

def xml
  @xml
end

Instance Method Details

#iterate(el) ⇒ Object



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
# File 'lib/reddy/rdfaparser.rb', line 24

def iterate (el)

  if el.attributes['about']
    if el.attributes['about'] =~ /^http/
      # deal with as absolute
      subject = el.attributes['about'].to_s
    else
      # time to go xml:base sneakin'
      if xml.base?
        subject = Addressable::URI.parse(xml.base)
        subject = subject + el.attributes['about']
      else
        subject = Addressable::URI.parse(@uri)
        subject = subject + el.attributes['about']
      end
    end
  else
    subject = @uri
  end
  
  if el.attributes['property']
    if el.attributes['property'] =~ /^http/
      property = el.attributes['property']
    else
      # curie hunt!
    property = self.parse_ns_curie(el, "property")
    end
  end
  
  if el.attributes['content']
    value = el.attributes['content']
  else
    value = el.text
  end

  @graph.add_triple subject.to_s, URIRef.new(property), value
end

#parse_ns_curie(el, attname) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/reddy/rdfaparser.rb', line 13

def parse_ns_curie(el, attname)
  attstring = el.attributes[attname]
  prefix = attstring.scan(/(.+):.+/).to_s
  if el.namespaces[prefix]
    namespace = el.namespaces[prefix]
  else
    raise "Namespace used in CURIE but not declared"
  end
  return namespace + attstring.scan(/.+:(.+)/).to_s
end