Class: RDF::RDFa::Context

Inherits:
Object show all
Includes:
Util::Logger
Defined in:
vendor/bundler/ruby/2.5.0/bundler/gems/rdf-rdfa-2ba4cee3e285/lib/rdf/rdfa/context/xml.rb,
vendor/bundler/ruby/2.5.0/bundler/gems/rdf-rdfa-2ba4cee3e285/lib/rdf/rdfa/context/html.rb,
vendor/bundler/ruby/2.5.0/bundler/gems/rdf-rdfa-2ba4cee3e285/lib/rdf/rdfa/context/xhtml.rb,
vendor/bundler/ruby/2.5.0/bundler/gems/rdf-rdfa-2ba4cee3e285/lib/rdf/rdfa/context.rb

Overview

Context representation existing of a hash of terms, prefixes, a default vocabulary and a URI.

Contexts are used for storing RDFa context representations. A representation is created by serializing a context graph (typically also in RDFa, but may be in other representations).

The class may be backed by an RDF::Repository, which will be used to retrieve a context graph or to load into, if no such graph exists

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::Logger

#log_debug, #log_depth, #log_error, #log_fatal, #log_info, #log_recover, #log_recovering?, #log_statistics, #log_warn, #logger

Constructor Details

#initialize(uri, options = {}) {|context| ... } ⇒ RDF::RDFa::Context

Initialize a new context from the given URI.

Parses the context and places it in the repository and cache

Parameters:

  • uri (RDF::URI, #to_s)

    URI of context to be represented

Yields:

  • (context)

Yield Parameters:

Yield Returns:

  • (void)

    ignored



43
44
45
46
47
48
49
50
51
52
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-rdfa-2ba4cee3e285/lib/rdf/rdfa/context.rb', line 43

def initialize(uri, options = {}, &block)
  @uri = RDF::URI.intern(uri)
  @prefixes = options.fetch(:prefixes, {})
  @terms = options.fetch(:terms, {})
  @vocabulary = options[:vocabulary]
  @options = options.dup

  yield(self) if block_given?
  self
end

Instance Attribute Details

#prefixesHash{Symbol => RDF::URI} (readonly)

Prefix mappings defined in this context

Returns:



16
17
18
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-rdfa-2ba4cee3e285/lib/rdf/rdfa/context.rb', line 16

def prefixes
  @prefixes
end

#termsHash{Symbol => RDF::URI} (readonly)

Term mappings defined in this context

Returns:



21
22
23
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-rdfa-2ba4cee3e285/lib/rdf/rdfa/context.rb', line 21

def terms
  @terms
end

#uriRDF::URI (readonly)

URI defining this context

Returns:



31
32
33
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-rdfa-2ba4cee3e285/lib/rdf/rdfa/context.rb', line 31

def uri
  @uri
end

#vocabularyRDF::URI (readonly)

Default URI defined for this vocabulary

Returns:



26
27
28
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-rdfa-2ba4cee3e285/lib/rdf/rdfa/context.rb', line 26

def vocabulary
  @vocabulary
end

Class Method Details

.find_with_html_rdfa_1_1(uri) ⇒ Object



5
6
7
8
9
10
11
12
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-rdfa-2ba4cee3e285/lib/rdf/rdfa/context/html.rb', line 5

def self.find_with_html_rdfa_1_1(uri)
  if uri.to_s == "http://www.w3.org/2011/rdfa-context/html-rdfa-1.1"
    @_html_rdfa_1_1_prof ||= RDF::RDFa::Context.new(RDF::URI("http://www.w3.org/2011/rdfa-context/html-rdfa-1.1"), {
    })
  else
    find_without_html_rdfa_1_1(uri)
  end
end

.find_with_rdfa_1_1(uri) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-rdfa-2ba4cee3e285/lib/rdf/rdfa/context/xml.rb', line 5

def self.find_with_rdfa_1_1(uri)
  if uri.to_s == "http://www.w3.org/2011/rdfa-context/rdfa-1.1"
    @_rdfa_1_1_prof ||= RDF::RDFa::Context.new(RDF::URI("http://www.w3.org/2011/rdfa-context/rdfa-1.1"), {
      prefixes: {
        cc:      "http://creativecommons.org/ns#",
        csvw:    "http://www.w3.org/ns/csvw#",
        ctag:    "http://commontag.org/ns#",
        dc:      "http://purl.org/dc/terms/",
        dc11:    "http://purl.org/dc/elements/1.1/",
        dcat:    "http://www.w3.org/ns/dcat#",
        dcterms: "http://purl.org/dc/terms/",
        foaf:    "http://xmlns.com/foaf/0.1/",
        gr:      "http://purl.org/goodrelations/v1#",
        grddl:   "http://www.w3.org/2003/g/data-view#",
        ical:    "http://www.w3.org/2002/12/cal/icaltzd#",
        ma:      "http://www.w3.org/ns/ma-ont#",
        og:      "http://ogp.me/ns#",
        org:     "http://www.w3.org/ns/org#",
        owl:     "http://www.w3.org/2002/07/owl#",
        prov:    "http://www.w3.org/ns/prov#",
        qb:      "http://purl.org/linked-data/cube#",
        rdf:     "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
        rdfa:    "http://www.w3.org/ns/rdfa#",
        rdfs:    "http://www.w3.org/2000/01/rdf-schema#",
        rev:     "http://purl.org/stuff/rev#",
        rif:     "http://www.w3.org/2007/rif#",
        rr:      "http://www.w3.org/ns/r2rml#",
        schema:  "http://schema.org/",
        sd:      "http://www.w3.org/ns/sparql-service-description#",
        sioc:    "http://rdfs.org/sioc/ns#",
        skos:    "http://www.w3.org/2004/02/skos/core#",
        skosxl:  "http://www.w3.org/2008/05/skos-xl#",
        v:       "http://rdf.data-vocabulary.org/#",
        vcard:   "http://www.w3.org/2006/vcard/ns#",
        void:    "http://rdfs.org/ns/void#",
        wdr:     "http://www.w3.org/2007/05/powder#",
        wdrs:    "http://www.w3.org/2007/05/powder-s#",
        xhv:     "http://www.w3.org/1999/xhtml/vocab#",
        xml:     "http://www.w3.org/XML/1998/namespace",
        xsd:     "http://www.w3.org/2001/XMLSchema#",
      },
      terms: {
        describedby: "http://www.w3.org/2007/05/powder-s#describedby",
        license:     "http://www.w3.org/1999/xhtml/vocab#license",
        role:        "http://www.w3.org/1999/xhtml/vocab#role",
      },
    })
  else
    find_without_rdfa_1_1(uri)
  end
end

.find_with_xhtml_rdfa_1_1(uri) ⇒ Object Also known as: find



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-rdfa-2ba4cee3e285/lib/rdf/rdfa/context/xhtml.rb', line 5

def self.find_with_xhtml_rdfa_1_1(uri)
  if uri.to_s == "http://www.w3.org/2011/rdfa-context/xhtml-rdfa-1.1"
    @_xhtml_rdfa_1_1_prof ||= RDF::RDFa::Context.new(RDF::URI("http://www.w3.org/2011/rdfa-context/xhtml-rdfa-1.1"), {
      terms: {
        alternate:  "http://www.w3.org/1999/xhtml/vocab#alternate",
        appendix:   "http://www.w3.org/1999/xhtml/vocab#appendix",
        bookmark:   "http://www.w3.org/1999/xhtml/vocab#bookmark",
        chapter:    "http://www.w3.org/1999/xhtml/vocab#chapter",
        cite:       "http://www.w3.org/1999/xhtml/vocab#cite",
        contents:   "http://www.w3.org/1999/xhtml/vocab#contents",
        copyright:  "http://www.w3.org/1999/xhtml/vocab#copyright",
        first:      "http://www.w3.org/1999/xhtml/vocab#first",
        glossary:   "http://www.w3.org/1999/xhtml/vocab#glossary",
        help:       "http://www.w3.org/1999/xhtml/vocab#help",
        icon:       "http://www.w3.org/1999/xhtml/vocab#icon",
        index:      "http://www.w3.org/1999/xhtml/vocab#index",
        last:       "http://www.w3.org/1999/xhtml/vocab#last",
        license:    "http://www.w3.org/1999/xhtml/vocab#license",
        meta:       "http://www.w3.org/1999/xhtml/vocab#meta",
        next:       "http://www.w3.org/1999/xhtml/vocab#next",
        p3pv1:      "http://www.w3.org/1999/xhtml/vocab#p3pv1",
        prev:       "http://www.w3.org/1999/xhtml/vocab#prev",
        previous:   "http://www.w3.org/1999/xhtml/vocab#previous",
        section:    "http://www.w3.org/1999/xhtml/vocab#section",
        start:      "http://www.w3.org/1999/xhtml/vocab#start",
        stylesheet: "http://www.w3.org/1999/xhtml/vocab#stylesheet",
        subsection: "http://www.w3.org/1999/xhtml/vocab#subsection",
        top:        "http://www.w3.org/1999/xhtml/vocab#top",
        up:         "http://www.w3.org/1999/xhtml/vocab#up",
      },
    })
  else
    find_without_xhtml_rdfa_1_1(uri)
  end
end

.find_without_html_rdfa_1_1Object

Override find to return this context, if matched



17
18
19
20
21
22
23
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
61
62
63
64
65
66
67
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-rdfa-2ba4cee3e285/lib/rdf/rdfa/context/html.rb', line 17

def self.find_with_rdfa_1_1(uri)
  if uri.to_s == "http://www.w3.org/2011/rdfa-context/rdfa-1.1"
    @_rdfa_1_1_prof ||= RDF::RDFa::Context.new(RDF::URI("http://www.w3.org/2011/rdfa-context/rdfa-1.1"), {
      prefixes: {
        cc:      "http://creativecommons.org/ns#",
        csvw:    "http://www.w3.org/ns/csvw#",
        ctag:    "http://commontag.org/ns#",
        dc:      "http://purl.org/dc/terms/",
        dc11:    "http://purl.org/dc/elements/1.1/",
        dcat:    "http://www.w3.org/ns/dcat#",
        dcterms: "http://purl.org/dc/terms/",
        foaf:    "http://xmlns.com/foaf/0.1/",
        gr:      "http://purl.org/goodrelations/v1#",
        grddl:   "http://www.w3.org/2003/g/data-view#",
        ical:    "http://www.w3.org/2002/12/cal/icaltzd#",
        ma:      "http://www.w3.org/ns/ma-ont#",
        og:      "http://ogp.me/ns#",
        org:     "http://www.w3.org/ns/org#",
        owl:     "http://www.w3.org/2002/07/owl#",
        prov:    "http://www.w3.org/ns/prov#",
        qb:      "http://purl.org/linked-data/cube#",
        rdf:     "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
        rdfa:    "http://www.w3.org/ns/rdfa#",
        rdfs:    "http://www.w3.org/2000/01/rdf-schema#",
        rev:     "http://purl.org/stuff/rev#",
        rif:     "http://www.w3.org/2007/rif#",
        rr:      "http://www.w3.org/ns/r2rml#",
        schema:  "http://schema.org/",
        sd:      "http://www.w3.org/ns/sparql-service-description#",
        sioc:    "http://rdfs.org/sioc/ns#",
        skos:    "http://www.w3.org/2004/02/skos/core#",
        skosxl:  "http://www.w3.org/2008/05/skos-xl#",
        v:       "http://rdf.data-vocabulary.org/#",
        vcard:   "http://www.w3.org/2006/vcard/ns#",
        void:    "http://rdfs.org/ns/void#",
        wdr:     "http://www.w3.org/2007/05/powder#",
        wdrs:    "http://www.w3.org/2007/05/powder-s#",
        xhv:     "http://www.w3.org/1999/xhtml/vocab#",
        xml:     "http://www.w3.org/XML/1998/namespace",
        xsd:     "http://www.w3.org/2001/XMLSchema#",
      },
      terms: {
        describedby: "http://www.w3.org/2007/05/powder-s#describedby",
        license:     "http://www.w3.org/1999/xhtml/vocab#license",
        role:        "http://www.w3.org/1999/xhtml/vocab#role",
      },
    })
  else
    find_without_rdfa_1_1(uri)
  end
end

.find_without_rdfa_1_1Object

Override find to return this context, if matched



60
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-rdfa-2ba4cee3e285/lib/rdf/rdfa/context/xml.rb', line 60

alias_method :find_without_rdfa_1_1, :find

.find_without_xhtml_rdfa_1_1Object

Override find to return this context, if matched



44
45
46
47
48
49
50
51
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-rdfa-2ba4cee3e285/lib/rdf/rdfa/context/xhtml.rb', line 44

def self.find_with_html_rdfa_1_1(uri)
  if uri.to_s == "http://www.w3.org/2011/rdfa-context/html-rdfa-1.1"
    @_html_rdfa_1_1_prof ||= RDF::RDFa::Context.new(RDF::URI("http://www.w3.org/2011/rdfa-context/html-rdfa-1.1"), {
    })
  else
    find_without_html_rdfa_1_1(uri)
  end
end

.load(uri) ⇒ Object

Load context into repository



111
112
113
114
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-rdfa-2ba4cee3e285/lib/rdf/rdfa/context.rb', line 111

def self.load(uri)
  uri = RDF::URI.intern(uri)
  repository.load(uri.to_s, base_uri: uri, graph_name: uri) unless repository.has_graph?(uri)
end

.repositoryRDF::Repository

Repository used for saving contexts

Returns:



67
68
69
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-rdfa-2ba4cee3e285/lib/rdf/rdfa/context.rb', line 67

def self.repository
  @repository ||= RDF::Repository.new(title: "RDFa Contexts")
end

.repository=(repo) ⇒ RDF::Repository

Set repository used for saving contexts

Parameters:

Returns:



75
76
77
78
79
80
81
82
83
84
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-rdfa-2ba4cee3e285/lib/rdf/rdfa/context.rb', line 75

def self.repository=(repo)
  unless repo.supports?(:graph_name)
    if respond_to?(:log_fatal)
      log_fatal("Context Repository must support graph_name", exception: ContextError)
    else
      abort("Context Repository must support graph_name")
    end
  end
  @repository = repo
end

Instance Method Details

#parse(enumerable)

This method returns an undefined value.

Extract vocabulary, prefix mappings and terms from a enumerable object into an instance

Parameters:



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-rdfa-2ba4cee3e285/lib/rdf/rdfa/context.rb', line 160

def parse(enumerable)
  log_debug("process_context: parse context <#{uri}>") if respond_to?(:log_debug)
  resource_info = {}
  enumerable.each do |statement|
    res = resource_info[statement.subject] ||= {}
    next unless statement.object.is_a?(RDF::Literal)
    log_debug("process_context: statement=#{statement.inspect}") if respond_to?(:log_debug)
    %w(uri term prefix vocabulary).each do |term|
      res[term] ||= statement.object.value if statement.predicate == RDF::RDFA[term]
    end
  end

  resource_info.values.each do |res|
    # If one of the objects is not a Literal or if there are additional rdfa:uri or rdfa:term
    # predicates sharing the same subject, no mapping is created.
    uri = res["uri"]
    term = res["term"]
    prefix = res["prefix"]
    vocab = res["vocabulary"]
    log_debug("process_context: uri=#{uri.inspect}, term=#{term.inspect}, prefix=#{prefix.inspect}, vocabulary=#{vocab.inspect}") if respond_to?(:log_debug)

    @vocabulary = vocab if vocab

    # For every extracted triple that is the common subject of an rdfa:prefix and an rdfa:uri
    # predicate, create a mapping from the object literal of the rdfa:prefix predicate to the
    # object literal of the rdfa:uri predicate. Add or update this mapping in the local list of
    # URI mappings after transforming the 'prefix' component to lower-case.
    # For every extracted
    prefix(prefix.downcase, uri) if uri && prefix && prefix != "_"

    # triple that is the common subject of an rdfa:term and an rdfa:uri predicate, create a
    # mapping from the object literal of the rdfa:term predicate to the object literal of the
    # rdfa:uri predicate. Add or update this mapping in the local term mappings.
    term(term, uri) if term && uri
  end
end

#prefix(name, uri = nil) ⇒ RDF::URI

Defines the given named URI prefix for this context.

Examples:

Defining a URI prefix

context.prefix :dc, RDF::URI('http://purl.org/dc/terms/')

Returning a URI prefix

context.prefix(:dc)    #=> RDF::URI('http://purl.org/dc/terms/')

Parameters:

Returns:



133
134
135
136
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-rdfa-2ba4cee3e285/lib/rdf/rdfa/context.rb', line 133

def prefix(name, uri = nil)
  name = name.to_s.empty? ? nil : (name.respond_to?(:to_sym) ? name.to_sym : name.to_s.to_sym)
  uri.nil? ? prefixes[name] : prefixes[name] = uri
end

#repositoryRDF::Repository

Returns:



117
118
119
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-rdfa-2ba4cee3e285/lib/rdf/rdfa/context.rb', line 117

def repository
  Context.repository
end

#term(name, uri = nil) ⇒ RDF::URI

Defines the given named URI term for this context.

Examples:

Defining a URI term

context.term :title, RDF::URI('http://purl.org/dc/terms/title')

Returning a URI context

context.term(:title)    #=> RDF::URI('http://purl.org/dc/terms/TITLE')

Parameters:

Returns:



150
151
152
153
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-rdfa-2ba4cee3e285/lib/rdf/rdfa/context.rb', line 150

def term(name, uri = nil)
  name = name.to_s.empty? ? nil : (name.respond_to?(:to_sym) ? name.to_sym : name.to_s.to_sym)
  uri.nil? ? terms[name] : terms[name] = uri
end