rdfobjects 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,29 @@
1
+ module RDFObject
2
+ class Collection < Hash
3
+ def uris
4
+ return self.keys
5
+ end
6
+ def find_by_type(type)
7
+ self.find_all {|r| r}
8
+ end
9
+
10
+ def find_or_create(uri)
11
+ if uri.could_be_a_safe_curie?
12
+ uri = Curie.parse uri
13
+ end
14
+ self[uri] = Resource.new(uri) unless self[uri]
15
+ self[uri]
16
+ end
17
+
18
+ def remove(resource)
19
+ self.delete(resource.uri)
20
+ end
21
+
22
+ def parse(data, format=nil)
23
+ parser = Parser.init_parser(data, format)
24
+ parser.collection = self
25
+ parser.parse
26
+ nil
27
+ end
28
+ end
29
+ end
@@ -58,26 +58,15 @@ class UTF8Parser < StringScanner
58
58
  raise StandardError, "Caught #{e.class}: #{e}"
59
59
  end
60
60
  end
61
- module RDFObject
62
- class Collection < Hash
63
- attr_accessor :objects
64
- def initialize(subjects=true)
65
- @objects = Collection.new(false) if subjects
66
- end
67
- def uris
68
- return self.keys
69
- end
70
- def find_by_type(type)
71
- self.find_all {|r| r}
72
- end
73
- def resources
74
- self.merge(@objects)
75
- end
76
- end
77
-
61
+ module RDFObject
78
62
  class Parser
79
63
  # Choose the best format parser from an admittedly small group of choices.
80
64
  def self.parse(rdf, format=nil)
65
+ parser = init_parser(rdf, format)
66
+ parser.parse
67
+ end
68
+
69
+ def self.init_parser(rdf, format=nil)
81
70
  if format
82
71
  parser = case format
83
72
  when 'rdfxml' then XMLParser.new(rdf)
@@ -112,17 +101,17 @@ module RDFObject
112
101
  end
113
102
  end
114
103
  end
115
- parser.parse
116
- end
104
+ parser
105
+ end
106
+
117
107
  attr_reader :collection
118
108
  def initialize(data=nil)
119
109
  @collection = Collection.new
120
110
  self.data=(data) if data
121
111
  end
122
-
123
- def find_or_create(uri)
124
- return @collection.resources[uri] if @collection.resources[uri]
125
- Resource.new(uri)
112
+ def collection=(collection)
113
+ raise ArgumentError unless collection.is_a?(RDFObject::Collection)
114
+ @collection = collection
126
115
  end
127
116
  end
128
117
  class NTriplesParser < RDFObject::Parser
@@ -142,8 +131,7 @@ module RDFObject
142
131
  tmp_object = scanner.scan_until(/>\s?\.\s*\n?$/)
143
132
  tmp_object.sub!(/^</,'')
144
133
  tmp_object.sub!(/>\s?\.\s*\n?$/,'')
145
- object = find_or_create(tmp_object)
146
- @collection[object.uri] = object
134
+ object = @collection.find_or_create(tmp_object)
147
135
  else
148
136
  language = nil
149
137
  data_type = nil
@@ -185,9 +173,8 @@ module RDFObject
185
173
  @ntriples.each do | assertion |
186
174
  next if assertion[0, 1] == "#" # Ignore comments
187
175
  triple = parse_ntriple(assertion)
188
- resource = find_or_create(triple[0])
176
+ resource = @collection.find_or_create(triple[0])
189
177
  resource.assert(triple[1], triple[2])
190
- @collection[resource.uri] = resource
191
178
  end
192
179
  @collection
193
180
  end
@@ -202,11 +189,10 @@ module RDFObject
202
189
  #
203
190
 
204
191
  def parse
205
- namespaces = @rdfxml.namespaces
206
- if namespaces.index("http://purl.org/rss/1.0/")
192
+ if @rdfxml.namespaces.values.index("http://purl.org/rss/1.0/")
207
193
  fix_rss10
208
194
  end
209
- if namespaces.index("http://www.w3.org/2005/sparql-results#")
195
+ if @rdfxml.namespaces.values.index("http://www.w3.org/2005/sparql-results#")
210
196
  raise "Sorry, SPARQL not yet supported"
211
197
  else
212
198
  parse_rdfxml
@@ -223,17 +209,16 @@ module RDFObject
223
209
  end
224
210
 
225
211
  def parse_resource_node(resource_node, collection)
226
- resource = find_or_create(resource_node.attribute_with_ns('about', "http://www.w3.org/1999/02/22-rdf-syntax-ns#").value)
212
+ resource = @collection.find_or_create(resource_node.attribute_with_ns('about', "http://www.w3.org/1999/02/22-rdf-syntax-ns#").value)
227
213
  unless (resource_node.name == "Description" and resource_node.namespace.href == "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
228
- resource.assert("[rdf:type]", find_or_create("#{resource_node.namespace.href}#{resource_node.name}"))
214
+ resource.assert("[rdf:type]", @collection.find_or_create("#{resource_node.namespace.href}#{resource_node.name}"))
229
215
  end
230
216
  resource_node.children.each do | child |
231
217
  next if child.text?
232
218
  predicate = "#{child.namespace.href}#{child.name}"
233
219
  if object_uri = child.attribute_with_ns("resource", "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
234
- obj_resource = find_or_create(object_uri.value)
220
+ obj_resource = @collection.find_or_create(object_uri.value)
235
221
  resource.assert(predicate, obj_resource)
236
- @collection[obj_resource.uri] = obj_resource
237
222
  elsif all_text?(child)
238
223
  opts = {}
239
224
  if lang = child.attribute_with_ns("lang", "http://www.w3.org/XML/1998/namespace")
@@ -245,13 +230,11 @@ module RDFObject
245
230
  resource.assert(predicate, Literal.new(child.content.strip,opts))
246
231
  end
247
232
  child.xpath("./*[@rdf:about]").each do | grandchild |
248
- gc_resource = find_or_create(grandchild.attribute_with_ns('about', "http://www.w3.org/1999/02/22-rdf-syntax-ns#").value)
233
+ gc_resource = @collection.find_or_create(grandchild.attribute_with_ns('about', "http://www.w3.org/1999/02/22-rdf-syntax-ns#").value)
249
234
  resource.assert(predicate, gc_resource)
250
- @collection[gc_resource.uri] = gc_resource
251
235
  parse_resource_node(grandchild, collection)
252
236
  end
253
237
  end
254
- @collection[resource.uri] = resource
255
238
  end
256
239
 
257
240
  def all_text?(node)
@@ -305,8 +288,7 @@ module RDFObject
305
288
 
306
289
  def parse
307
290
  @json.each_pair do |subject, assertions|
308
- resource = find_or_create(subject)
309
- @collection[resource.uri] = resource
291
+ resource = @collection.find_or_create(subject)
310
292
  assertions.each_pair do |predicate, objects|
311
293
  objects.each do | object |
312
294
  if object['type'] == 'literal'
@@ -320,13 +302,11 @@ module RDFObject
320
302
  literal = Literal.new(object['value'],opts)
321
303
  resource.assert(predicate, literal)
322
304
  elsif object['type'] == 'uri'
323
- o = find_or_create(object['value'])
305
+ o = @collection.find_or_create(object['value'])
324
306
  resource.assert(predicate, o)
325
- @collection[o.uri] = o
326
307
  elsif object['type'] == 'bnode' # For now, we're going to treat a blank node like a URI resource.
327
- o = find_or_create(object['value'])
308
+ o = @collection.find_or_create(object['value'])
328
309
  resource.assert(predicate, o)
329
- @collection[o.uri] = o
330
310
  end
331
311
  end
332
312
  end
@@ -5,42 +5,6 @@ require 'weakref'
5
5
 
6
6
  module RDFObject
7
7
  class Resource < OpenStruct
8
- class << self
9
- def instances
10
- instances = {}
11
- ObjectSpace.each_object(self) { | rdf_object |
12
- next unless rdf_object.uri
13
- instances[rdf_object.uri] = rdf_object
14
- }
15
- instances
16
- end
17
-
18
- def reset!
19
- # @instances = {}
20
- ObjectSpace.each_object(self) { | rdf_object |
21
- rdf_object.uri = nil
22
- Curie.get_mappings.each do | prefix, uri |
23
- if rdf_object.respond_to?(prefix.to_sym)
24
- rdf_object.send("#{prefix}=".to_sym, nil)
25
- end
26
- end
27
- }
28
- ObjectSpace.garbage_collect
29
- end
30
-
31
- def remove(resource)
32
- to_del = instances[resource.uri]
33
- to_del.uri = nil
34
- end
35
-
36
- def exists?(uri)
37
- ObjectSpace.each_object(self) { | rdf_object |
38
- return true if rdf_object.uri == uri
39
- }
40
- false
41
- end
42
- end
43
-
44
8
  def initialize(uri)
45
9
  if uri.could_be_a_safe_curie?
46
10
  uri = Curie.parse uri
@@ -121,18 +85,6 @@ module RDFObject
121
85
  end
122
86
  return true
123
87
  end
124
-
125
- def self.new(uri, *opts)
126
- #if self.exists?(uri)
127
- # return self.instances[uri]
128
- #end
129
- if exists = self.instances[uri]
130
- unless opts.index(:force)
131
- return exists
132
- end
133
- end
134
- super(uri)
135
- end
136
88
  end
137
89
 
138
90
  class ResourceReference
data/lib/rdf_objects.rb CHANGED
@@ -6,6 +6,7 @@ module RDFObject
6
6
  require File.dirname(__FILE__) + '/rdf_objects/rdf_resource'
7
7
  require File.dirname(__FILE__) + '/rdf_objects/curies'
8
8
  require File.dirname(__FILE__) + '/rdf_objects/data_types'
9
- require File.dirname(__FILE__) + '/rdf_objects/http_client'
9
+ require File.dirname(__FILE__) + '/rdf_objects/http_client'
10
+ require File.dirname(__FILE__) + '/rdf_objects/collection'
10
11
  Curie.remove_prefixes!(:http)
11
12
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rdfobjects
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ross Singer
@@ -55,12 +55,12 @@ files:
55
55
  - LICENSE
56
56
  - README
57
57
  - lib/rdf_objects.rb
58
+ - lib/rdf_objects/collection.rb
58
59
  - lib/rdf_objects/curies.rb
59
60
  - lib/rdf_objects/data_types.rb
60
61
  - lib/rdf_objects/http_client.rb
61
62
  - lib/rdf_objects/parsers.rb
62
63
  - lib/rdf_objects/rdf_resource.rb
63
- - lib/rdf_objects/rdfxml_parser.rb
64
64
  - lib/rdf_objects/serializers.rb
65
65
  - lib/xsl/RDFa2RDFXML.xsl
66
66
  - lib/xsl/rdf2nt.xsl
@@ -1,29 +0,0 @@
1
- require 'nokogiri'
2
-
3
- module RDFObject
4
-
5
- class XMLParser
6
- def self.parse(doc)
7
- parser = Nokogiri::XML::SAX::Parser.new(RDFXMLDocument.new())
8
- parser.parse(doc.to_s)
9
- return parser.document.collection.uniq
10
- end
11
- end
12
- class RDFXMLDocument < Nokogiri::XML::SAX::Document
13
- attr_reader :collection
14
- def initialize
15
- @collection = []
16
- @resource_path = []
17
- @predicate_path = []
18
- end
19
-
20
- def start_element_namespace name, attributes = [], prefix = nil, uri = nil, ns = {}
21
- attributes.each do | attribute |
22
- if attribute.localname == "about" && attribute.uri == "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
23
- resource = Resource.new(attribute.value)
24
- @collection << resource
25
- end
26
- end
27
- end
28
- end
29
- end