Class: Reddy::RdfXmlParser::EvaluationContext

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

Overview

The Recursive Baggage

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, element, graph) ⇒ EvaluationContext

Returns a new instance of EvaluationContext.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/reddy/rdfxmlparser.rb', line 18

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

  extract_from_element(element) if element
end

Instance Attribute Details

#baseObject

:nodoc:



11
12
13
# File 'lib/reddy/rdfxmlparser.rb', line 11

def base
  @base
end

#graphObject

Returns the value of attribute graph.



15
16
17
# File 'lib/reddy/rdfxmlparser.rb', line 15

def graph
  @graph
end

#languageObject

Returns the value of attribute language.



14
15
16
# File 'lib/reddy/rdfxmlparser.rb', line 14

def language
  @language
end

#li_counterObject

Returns the value of attribute li_counter.



16
17
18
# File 'lib/reddy/rdfxmlparser.rb', line 16

def li_counter
  @li_counter
end

#subjectObject

Returns the value of attribute subject.



12
13
14
# File 'lib/reddy/rdfxmlparser.rb', line 12

def subject
  @subject
end

#uri_mappingsObject

Returns the value of attribute uri_mappings.



13
14
15
# File 'lib/reddy/rdfxmlparser.rb', line 13

def uri_mappings
  @uri_mappings
end

Instance Method Details

#clone(element, options = {}) ⇒ Object

Clone existing evaluation context adding information from element



31
32
33
34
35
36
37
38
39
40
# File 'lib/reddy/rdfxmlparser.rb', line 31

def clone(element, options = {})
  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) if element
  
  options.each_pair {|k, v| new_ec.send("#{k}=", v)}
  new_ec
end

#extract_from_ancestors(el) ⇒ Object

Extract Evaluation Context from an element by looking at ancestors recurively



43
44
45
46
47
48
49
50
51
# File 'lib/reddy/rdfxmlparser.rb', line 43

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

#extract_from_element(el) ⇒ Object

Extract Evaluation Context from an element



54
55
56
57
58
59
60
# File 'lib/reddy/rdfxmlparser.rb', line 54

def extract_from_element(el)
  b = el.attribute_with_ns("base", XML_NS.uri.to_s)
  lang = el.attribute_with_ns("lang", XML_NS.uri.to_s)
  self.base = URIRef.new(b, self.base)
  self.language = lang if lang
  self.uri_mappings.merge!(extract_mappings(el))
end

#extract_mappings(element) ⇒ Object

Extract the XMLNS mappings from an element



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/reddy/rdfxmlparser.rb', line 63

def extract_mappings(element)
  mappings = {}

  # look for xmlns
  element.namespaces.each do |attr_name,attr_value|
    abbr, suffix = attr_name.to_s.split(":")
    if abbr == "xmlns"
      mappings[suffix] = Namespace.new(attr_value, suffix)
      @graph.bind(mappings[suffix])
    end
  end
  mappings
end

#inspectObject



92
93
94
95
96
# File 'lib/reddy/rdfxmlparser.rb', line 92

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_next(predicate) ⇒ Object

Produce the next list entry for this context



78
79
80
81
82
83
# File 'lib/reddy/rdfxmlparser.rb', line 78

def li_next(predicate)
  @li_counter += 1
  predicate = Addressable::URI.parse(predicate.to_s)
  predicate.fragment = "_#{@li_counter}"
  predicate = URIRef.new(predicate)
end