Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 127 lines (121 sloc) 4.069 kb
19b9f40 Ross Singer Initial import into git
authored
1 require 'uri'
2 require 'builder'
3 require 'date'
4 require 'curies'
5 class RDFResource
6 attr_reader :uri, :namespaces, :modifiers
7 def initialize(uri)
8 Curie.add_prefixes! :frbr=>"http://vocab.org/frbr/core#", :dct=>"http://purl.org/dc/terms/", :bibo=>"http://purl.org/ontology/bibo/",
9 :skos=>"http://www.w3.org/2004/02/skos/core#", :rda=>"http://RDVocab.info/Elements/", :cat=>"http://schema.talis.com/2009/catalontology/",
f8f9563 Ross Singer Agents should be modeled a little better
authored
10 :rdfs=>"http://www.w3.org/2000/01/rdf-schema#", :ov=>"http://open.vocab.org/terms/", :event=>"http://purl.org/NET/c4dm/event.owl#",
11 :role=>"http://RDVocab.info/roles/", :mo=>"http://purl.org/ontology/mo/", :rdf=>'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
12
13 if uri.could_be_a_safe_curie?
14 @uri = Curie.parse uri
15 else
16 @uri = uri
17 end
19b9f40 Ross Singer Initial import into git
authored
18 @namespaces = ['http://www.w3.org/1999/02/22-rdf-syntax-ns#']
19
20 @modifiers = {}
21 end
22
23 def assert(predicate, object, type=nil, lang=nil)
f8f9563 Ross Singer Agents should be modeled a little better
authored
24 if predicate.could_be_a_safe_curie?
25 uri = URI.parse(Curie.parse predicate)
26 else
27 uri = URI.parse(predicate)
28 end
19b9f40 Ross Singer Initial import into git
authored
29 ns = nil
30 elem = nil
31 if uri.fragment
32 ns, elem = uri.to_s.split('#')
33 ns << '#'
34 else
35 elem = uri.path.split('/').last
36 ns = uri.to_s.sub(/#{elem}$/, '')
37 end
38 attr_name = ''
39 if i = @namespaces.index(ns)
40 attr_name = "n#{i}_#{elem}"
41 else
42 @namespaces << ns
43 attr_name = "n#{@namespaces.index(ns)}_#{elem}"
44 end
45 unless type
46 val = object
47 else
48 @modifiers[object.object_id] ||={}
49 @modifiers[object.object_id][:type] = type
50 val = case type
51 when 'http://www.w3.org/2001/XMLSchema#dateTime' then DateTime.parse(object)
52 when 'http://www.w3.org/2001/XMLSchema#date' then Date.parse(object)
53 when 'http://www.w3.org/2001/XMLSchema#int' then object.to_i
54 when 'http://www.w3.org/2001/XMLSchema#string' then object.to_s
55 when 'http://www.w3.org/2001/XMLSchema#boolean'
56 if object.downcase == 'true' || object == '1'
57 true
58 else
59 false
60 end
61 else
62 object
63 end
64 end
65 if lang
66 @modifiers[object.object_id] ||={}
67 @modifiers[val.object_id][:language] = lang
68 end
69 if self.instance_variable_defined?("@#{attr_name}")
70 unless self.instance_variable_get("@#{attr_name}").is_a?(Array)
71 att = self.instance_variable_get("@#{attr_name}")
72 self.instance_variable_set("@#{attr_name}", [att])
73 end
74 self.instance_variable_get("@#{attr_name}") << val
75 else
76 self.instance_variable_set("@#{attr_name}", val)
77 end
78 end
79
80 def relate(predicate, resource)
81 self.assert(predicate, self.class.new(resource))
82 end
83
84 def to_rdfxml
85 doc = Builder::XmlMarkup.new
86 xmlns = {}
87 i = 1
88 @namespaces.each do | ns |
89 next if ns == 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
90 xmlns["xmlns:n#{i}"] = ns
91 i += 1
92 end
93 doc.rdf :Description,xmlns.merge({:about=>uri}) do | rdf |
94 self.instance_variables.each do | ivar |
95 next unless ivar =~ /^@n[0-9]*_/
96 prefix, tag = ivar.split('_',2)
97 attrs = {}
98 curr_attr = self.instance_variable_get("#{ivar}")
99 prefix.sub!(/^@/,'')
100 prefix = 'rdf' if prefix == 'n0'
101 unless curr_attr.is_a?(Array)
102 curr_attr = [curr_attr]
103 end
104 curr_attr.each do | val |
105 if val.is_a?(RDFResource)
106 attrs['rdf:resource'] = val.uri
107 end
108 if @modifiers[val.object_id]
109 if @modifiers[val.object_id][:language]
110 attrs['xml:lang'] = @modifiers[val.object_id][:language]
111 end
112 if @modifiers[val.object_id][:type]
113 attrs['rdf:datatype'] = @modifiers[val.object_id][:type]
114 end
115 end
116 unless attrs['rdf:resource']
117 rdf.tag!("#{prefix}:#{tag}", attrs, val)
118 else
119 rdf.tag!("#{prefix}:#{tag}", attrs)
120 end
121 end
122 end
123 end
124 doc.target!
125 end
126
127 end
Something went wrong with that request. Please try again.