Class: RDF::RDFXML::Reader::EvaluationContext

Inherits:
Object
  • Object
show all
Defined in:
lib/rdf/rdfxml/reader.rb

Overview

The Recursive Baggage

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, element, graph, &cb) ⇒ EvaluationContext

Returns a new instance of EvaluationContext.



34
35
36
37
38
39
40
41
42
43
# File 'lib/rdf/rdfxml/reader.rb', line 34

def initialize(base, element, graph, &cb)
  # Initialize the evaluation context, [5.1]
  self.base = RDF::URI(base)
  @uri_mappings = {}
  @language = nil
  @graph = graph
  @li_counter = 0

  extract_from_element(element, &cb) if element
end

Instance Attribute Details

#baseObject

Returns the value of attribute base.



27
28
29
# File 'lib/rdf/rdfxml/reader.rb', line 27

def base
  @base
end

#graphObject

Returns the value of attribute graph.



31
32
33
# File 'lib/rdf/rdfxml/reader.rb', line 31

def graph
  @graph
end

#languageObject

Returns the value of attribute language.



30
31
32
# File 'lib/rdf/rdfxml/reader.rb', line 30

def language
  @language
end

#li_counterObject

Returns the value of attribute li_counter.



32
33
34
# File 'lib/rdf/rdfxml/reader.rb', line 32

def li_counter
  @li_counter
end

#subjectObject

Returns the value of attribute subject.



28
29
30
# File 'lib/rdf/rdfxml/reader.rb', line 28

def subject
  @subject
end

#uri_mappingsObject

Returns the value of attribute uri_mappings.



29
30
31
# File 'lib/rdf/rdfxml/reader.rb', line 29

def uri_mappings
  @uri_mappings
end

Instance Method Details

#clone(element, **options, &cb) ⇒ Object

Clone existing evaluation context adding information from element



46
47
48
49
50
51
52
53
54
55
# File 'lib/rdf/rdfxml/reader.rb', line 46

def clone(element, **options, &cb)
  new_ec = EvaluationContext.new(@base, nil, @graph)
  new_ec.uri_mappings = self.uri_mappings.clone
  new_ec.language = self.language

  new_ec.extract_from_element(element, &cb) if element
  
  options.each_pair {|k, v| new_ec.send("#{k}=", v)}
  new_ec
end

#extract_from_ancestors(el, &cb) ⇒ Object

Extract Evaluation Context from an element by looking at ancestors recurively



58
59
60
61
62
63
64
65
66
# File 'lib/rdf/rdfxml/reader.rb', line 58

def extract_from_ancestors(el, &cb)
  ancestors = el.ancestors
  while ancestors.length > 0
    a = ancestors.pop
    next unless a.element?
    extract_from_element(a, &cb)
  end
  extract_from_element(el, &cb)
end

#extract_from_element(el, &cb) ⇒ Object

Extract Evaluation Context from an element



69
70
71
72
73
74
75
76
# File 'lib/rdf/rdfxml/reader.rb', line 69

def extract_from_element(el, &cb)
  self.language = el.language if el.language
  if b = el.base
    b = RDF::URI(b)
    self.base = b.absolute? ? b : self.base.join(b)
  end
  self.uri_mappings.merge!(extract_mappings(el, &cb))
end

#extract_mappings(element, &cb) ⇒ Object

Extract the XMLNS mappings from an element



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rdf/rdfxml/reader.rb', line 79

def extract_mappings(element, &cb)
  mappings = {}

  # look for xmlns
  element.namespaces.each do |prefix, value|
    value = base.join(value)
    mappings[prefix] = value
    cb.call(prefix, value) if block_given?
  end
  mappings
end

#inspectObject



102
103
104
105
106
# File 'lib/rdf/rdfxml/reader.rb', line 102

def inspect
  v = %w(base subject language).map {|a| "#{a}='#{self.send(a).nil? ? 'nil' : self.send(a)}'"}
  v << "uri_mappings[#{uri_mappings.keys.length}]"
  v.join(",")
end

#li_nextObject

Produce the next list entry for this context



92
93
94
95
# File 'lib/rdf/rdfxml/reader.rb', line 92

def li_next
  @li_counter += 1
  predicate = RDF["_#{@li_counter}"]
end