class RDF::RDFa::Context

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

This file is automatically generated by ./script/intern_context RDFa context for www.w3.org/2011/rdfa-context/html-rdfa-1.1

This file is automatically generated by ./script/intern_context RDFa context for www.w3.org/2011/rdfa-context/xhtml-rdfa-1.1

This file is automatically generated by ./script/intern_context RDFa context for www.w3.org/2011/rdfa-context/rdfa-1.1

Attributes

prefixes[R]

Prefix mappings defined in this context @!attribute [r] prefixes @return [Hash{Symbol => RDF::URI}]

terms[R]

Term mappings defined in this context @!attribute [r] terms @return [Hash{Symbol => RDF::URI}]

uri[R]

URI defining this context @!attribute [r] uri @return [RDF::URI]

vocabulary[R]

Default URI defined for this vocabulary @!attribute [r] vocabulary @return [RDF::URI]

Public Class Methods

cache() click to toggle source

@return [RDF::Util::Cache] @private

# File lib/rdf/rdfa/context.rb, line 57
def self.cache
  require 'rdf/util/cache' unless defined?(::RDF::Util::Cache)
  @cache ||= begin
    RDF::Util::Cache.new(-1)
  end
end
find(uri) click to toggle source

Return a context faulting through the cache @return [RDF::RDFa::Context]

# File lib/rdf/rdfa/context.rb, line 88
def self.find(uri)
  uri = RDF::URI.intern(uri)

  return cache[uri] unless cache[uri].nil?

  # Two part creation to prevent re-entrancy problems if p1 => p2 and p2 => p1
  # Return something to make the caller happy if we're re-entered
  cache[uri] = Struct.new(:prefixes, :terms, :vocabulary).new({}, {}, nil)
  # Now do the actual load
  cache[uri] = new(uri) do |context|
    log_debug("process_context: retrieve context <#{uri}>") if respond_to?(:log_debug)
    Context.load(uri)
    context.parse(repository.query({graph_name: uri}))
  end
rescue Exception => e
  if respond_to?(:log_fatal)
    log_error("Context #{uri}: #{e.message}")
  else
    abort("Context #{uri}: #{e.message}")
  end
end
find_with_html_rdfa_1_1(uri) click to toggle source
# File 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
Also aliased as: find
find_with_rdfa_1_1(uri) click to toggle source
# File 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: {
        as:      "https://www.w3.org/ns/activitystreams#",
        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/",
        dqv:     "http://www.w3.org/ns/dqv#",
        duv:     "https://www.w3.org/ns/duv#",
        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#",
        jsonld:  "http://www.w3.org/ns/json-ld#",
        ldp:     "http://www.w3.org/ns/ldp#",
        ma:      "http://www.w3.org/ns/ma-ont#",
        oa:      "http://www.w3.org/ns/oa#",
        odrl:    "http://www.w3.org/ns/odrl/2/",
        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#",
        sosa:    "http://www.w3.org/ns/sosa/",
        ssn:     "http://www.w3.org/ns/ssn/",
        time:    "http://www.w3.org/2006/time#",
        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
Also aliased as: find
find_with_xhtml_rdfa_1_1(uri) click to toggle source
# File 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
Also aliased as: find
find_without_html_rdfa_1_1(uri)

Override find to return this context, if matched

Alias for: find
find_without_rdfa_1_1(uri)

Override find to return this context, if matched

Alias for: find
find_without_xhtml_rdfa_1_1(uri)

Override find to return this context, if matched

Alias for: find
load(uri) click to toggle source

Load context into repository

# File 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
new(uri, **options) { |self| ... } click to toggle source

Initialize a new context from the given URI.

Parses the context and places it in the repository and cache

@param [RDF::URI, to_s] uri URI of context to be represented @yield [context] @yieldparam [RDF::RDFa::Context] context @yieldreturn [void] ignored @return [RDF::RDFa::Context]

# File 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
repository() click to toggle source

Repository used for saving contexts @return [RDF::Repository]

# File lib/rdf/rdfa/context.rb, line 67
def self.repository
  @repository ||= RDF::Repository.new(title: "RDFa Contexts")
end
repository=(repo) click to toggle source

Set repository used for saving contexts @param [RDF::Repository] repo @return [RDF::Repository]

# File 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

Public Instance Methods

parse(enumerable) click to toggle source

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

@param [RDF::Enumerable, Enumerator] enumerable @return [void] ignored

# File 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) click to toggle source

Defines the given named URI prefix for this context.

@example Defining a URI prefix

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

@example Returning a URI prefix

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

@param [Symbol, to_s] name @param [RDF::URI, to_s] uri @return [RDF::URI]

# File 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
repository() click to toggle source

@return [RDF::Repository]

# File lib/rdf/rdfa/context.rb, line 117
def repository
  Context.repository
end
term(name, uri = nil) click to toggle source

Defines the given named URI term for this context.

@example Defining a URI term

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

@example Returning a URI context

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

@param [Symbol, to_s] name @param [RDF::URI, to_s] uri @return [RDF::URI]

# File 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