Class: Namespace

Inherits:
Object
  • Object
show all
Defined in:
lib/active_rdf/objectmanager/namespace.rb

Overview

Manages namespace abbreviations and expansions

Constant Summary collapse

@@namespaces =
Hash.new
@@inverted_namespaces =
Hash.new

Class Method Summary collapse

Class Method Details

.abbreviationsObject

returns currently registered namespace abbreviations (e.g. :foaf, :rdf)



99
100
101
# File 'lib/active_rdf/objectmanager/namespace.rb', line 99

def self.abbreviations
	@@namespaces.keys
end

.expand(prefix, localname) ⇒ Object

returns URI (string) formed by concatenation of prefix and localname



53
54
55
# File 'lib/active_rdf/objectmanager/namespace.rb', line 53

def self.expand(prefix, localname)
  @@namespaces[prefix.to_sym].to_s + localname.to_s
end

.localname(resource) ⇒ Object

returns local-part of URI



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/active_rdf/objectmanager/namespace.rb', line 82

def self.localname(resource)
  # get string representation of resource uri
  uri = if(resource.respond_to?(:uri))
    resource.uri.to_s
  else 
    resource.to_s
  end

  delimiter = uri.rindex(/#|\//)
  if delimiter.nil? or delimiter == uri.size-1
    uri
  else
    uri[delimiter+1..-1]
  end
end

.lookup(prefix, localname, resource_type = RDFS::Resource) ⇒ Object

returns a resource whose URI is formed by concatenation of prefix and localname



48
49
50
# File 'lib/active_rdf/objectmanager/namespace.rb', line 48

def self.lookup(prefix, localname, resource_type = RDFS::Resource)
  resource_type.new(expand(prefix, localname))
end

.prefix(resource) ⇒ Object

returns prefix (if known) for the non-local part of the URI, or nil if prefix not registered



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/active_rdf/objectmanager/namespace.rb', line 59

def self.prefix(resource)
  # get string representation of resource uri
  uri = if(resource.respond_to?(:uri))
    resource.uri.to_s
  else 
    resource.to_s
  end

  # uri.to_s gives us the uri of the resource (if resource given)
  # then we find the last occurrence of # or / (heuristical namespace
  # delimitor)
  delimiter = uri.rindex(/#|\//)

  # if delimiter not found, URI cannot be split into (non)local-part
  return uri if delimiter.nil?

  # extract non-local part (including delimiter)
  nonlocal = uri[0..delimiter]

  @@inverted_namespaces[nonlocal]
end

.register(prefix, fullURI) ⇒ Object

registers a namespace prefix and its associated expansion (full URI) e.g. :rdf and ‘www.w3.org/1999/02/22-rdf-syntax-ns#’

Raises:



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
# File 'lib/active_rdf/objectmanager/namespace.rb', line 11

def self.register(prefix, fullURI)
raise ActiveRdfError, 'prefix nor uri can be empty' if (prefix.to_s.empty? or fullURI.to_s.empty?)
  # raise ActiveRdfError, "namespace uri should end with # or /" unless /\/|#/ =~ fullURI.to_s[-1..-1]
ActiveRdfLogger::log_info(self) { "Namespace: registering #{fullURI} to #{prefix}" }
  @@namespaces[prefix.to_sym] = fullURI.to_s
  @@inverted_namespaces[fullURI.to_s] = prefix.to_sym

  # enable namespace lookups through FOAF::name
  # if FOAF defined, add to it
  if Object.const_defined?(prefix.to_s.upcase)
    ns = Object.const_get(prefix.to_s.upcase)
  else
    # otherwise create a new module for it
    ns = Module.new  
    Object.const_set(prefix.to_s.upcase, ns)
  end

  # catch FOAF::name or all other lookups 
  class << ns
    def method_missing(method, *args)
      Namespace.lookup(self.to_s.downcase.to_sym, method)
    end

    def const_missing(klass)
      ObjectManager.construct_class(Namespace.lookup(self.to_s.downcase.to_sym, klass))
    end

    # make some builtin methods private because lookup doesn't work otherwise 
    # on e.g. RDF::type and FOAF::name
    [:type, :name, :id].each {|m| private(m) }
  end

  # return the namespace proxy object
  ns
end