rdfobjects 0.9.1 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -40,7 +40,13 @@ module RDFObject
40
40
  if uri.could_be_a_safe_curie?
41
41
  uri = Curie.parse uri
42
42
  end
43
- self[uri] = Resource.new(uri) unless self[uri]
43
+ if BlankNode.is_bnode_id?(uri)
44
+ bnode = BlankNode.new(uri)
45
+ self[bnode.uri] = bnode unless self[bnode.uri]
46
+ return self[bnode.uri]
47
+ else
48
+ self[uri] = Resource.new(uri) unless self[uri]
49
+ end
44
50
  self[uri]
45
51
  end
46
52
 
@@ -55,7 +55,11 @@ class RDFObject::Literal
55
55
  value
56
56
  end
57
57
  if obj.is_a?(Float)
58
- raise ArgumentError if obj.to_s.sub(/\.0/,'') != value
58
+ if obj.to_s == value
59
+ elsif obj.to_s.sub(/\.0*/,'') == value
60
+ else
61
+ raise ArgumentError
62
+ end
59
63
  elsif obj.is_a?(DateTime)
60
64
  raise ArgumentError if obj.to_s !~ /^#{Regexp.escape(value)}/
61
65
  else
@@ -64,7 +64,7 @@ module RDFObject
64
64
  # Choose the best format parser from an admittedly small group of choices.
65
65
  def self.parse(rdf, options={})
66
66
  parser = init_parser(rdf, options)
67
- parser.parse
67
+ parser.parse if parser
68
68
  end
69
69
 
70
70
  def self.init_parser(rdf, options={})
@@ -76,7 +76,16 @@ module RDFObject
76
76
  when 'json' then JSONParser.new(rdf)
77
77
  end
78
78
  else
79
-
79
+ # Check to see if it is a URI being passed
80
+ begin
81
+ uri = URI.parse(rdf)
82
+ if uri.is_a?(URI::HTTP)
83
+ response = HTTPClient.fetch(rdf)
84
+ rdf = response[:content]
85
+ options[:base_uri] = response[:uri]
86
+ end
87
+ rescue URI::InvalidURIError
88
+ end
80
89
  # Check if the format is XML or RDFa
81
90
  doc = XMLTestDocument.new
82
91
  p = Nokogiri::XML::SAX::Parser.new(doc)
@@ -90,8 +99,9 @@ module RDFObject
90
99
  rdf.rewind
91
100
  end
92
101
  if doc.is_html?
102
+
93
103
  parser = RDFAParser.new(rdf)
94
- else
104
+ elsif doc.is_rdf?
95
105
  parser = XMLParser.new(rdf)
96
106
  end
97
107
  else
@@ -111,7 +121,7 @@ module RDFObject
111
121
  end
112
122
  end
113
123
  end
114
- if options[:base_uri]
124
+ if options[:base_uri] && parser
115
125
  parser.base_uri = options[:base_uri]
116
126
  end
117
127
  parser
@@ -155,9 +165,14 @@ module RDFObject
155
165
  ntriple.force_encoding("ASCII-8BIT")
156
166
  end
157
167
  scanner = StringScanner.new(ntriple)
158
- subject = scanner.scan_until(/> /)
159
- subject.sub!(/^</,'')
160
- subject.sub!(/> $/,'')
168
+ if ntriple[0,1] == "<"
169
+ subject = scanner.scan_until(/> /)
170
+ subject.sub!(/^</,'')
171
+ subject.sub!(/> $/,'')
172
+ else
173
+ subject = scanner.scan_until(/\w /)
174
+ subject.strip!
175
+ end
161
176
  predicate = scanner.scan_until(/> /)
162
177
  predicate.sub!(/^</,'')
163
178
  predicate.sub!(/> $/,'')
@@ -167,6 +182,10 @@ module RDFObject
167
182
  tmp_object.sub!(/>\s?\.\s*\n?$/,'')
168
183
  object = self.sanitize_uri(tmp_object)
169
184
  type = "uri"
185
+ elsif scanner.match?(/_:/)
186
+ object = scanner.scan_until(/\w\s?\.\s*\n?$/)
187
+ object.sub!(/\s?\.\s*\n?$/,'')
188
+ type = "bnode"
170
189
  else
171
190
  language = nil
172
191
  data_type = nil
@@ -213,6 +232,7 @@ module RDFObject
213
232
  object = case triple[:type]
214
233
  when "literal" then triple[:object]
215
234
  when "uri" then @collection.find_or_create(triple[:object])
235
+ when "bnode" then @collection.find_or_create(triple[:object])
216
236
  end
217
237
  resource.assert(triple[:predicate],object)
218
238
  end
@@ -248,8 +268,14 @@ module RDFObject
248
268
  return false
249
269
  end
250
270
 
271
+ def is_rdf?
272
+ return true if @namespaces.index("http://www.w3.org/1999/02/22-rdf-syntax-ns#")
273
+ return false
274
+ end
275
+
251
276
  def is_html?
252
277
  return true if @namespaces.index("http://www.w3.org/1999/xhtml")
278
+ return true if @xml_start =~ /html/i
253
279
  return false
254
280
  end
255
281
  end
@@ -294,35 +320,12 @@ module RDFObject
294
320
  hash
295
321
  end
296
322
 
297
- #def add_layer(element_uri, resource_uri)
298
- # if @uris.length > 0 && @current_predicate
299
- # @collection[@uris.last].relate(@current_predicate, @collection.find_or_create(resource_uri))
300
- # @current_predicate = nil
301
- # end
302
- # @uris << resource_uri
303
- # @tags[resource_uri] = element_uri
304
- #end
305
-
306
- #def remove_layer(element_uri)
307
- # uris = []
308
- # @tags.each do |uri, el|
309
- # uris << uri if el == element_uri
310
- # end
311
- # uris.each do | uri |
312
- # if @uris.last == uri
313
- # @uris.pop
314
- # @tags.delete(uri)
315
- # break
316
- # end
317
- # end
318
- # @current_resource = @collection[@uris.last]
319
- #end
320
-
321
-
322
323
  def add_layer name, attributes, prefix, uri, ns
323
324
  layer = {:name=>"#{uri}#{name}"}
324
- if attributes['about']
325
- layer[:resource] = @collection.find_or_create(sanitize_uri(attributes['about']))
325
+ if attributes['about'] or attributes['nodeID']
326
+ id = attributes['about'] || attributes['nodeID']
327
+ id = sanitize_uri(id) if attributes['about']
328
+ layer[:resource] = @collection.find_or_create(id)
326
329
  unless "#{uri}#{name}" == "http://www.w3.org/1999/02/22-rdf-syntax-ns#Description"
327
330
  layer[:resource].relate("http://www.w3.org/1999/02/22-rdf-syntax-ns#type", @collection.find_or_create("#{uri}#{name}"))
328
331
  end
@@ -3,15 +3,7 @@ require 'date'
3
3
  require 'curies'
4
4
 
5
5
  module RDFObject
6
- class Resource < OpenStruct
7
- attr_reader :table
8
- def initialize(uri)
9
- if uri.could_be_a_safe_curie?
10
- uri = Curie.parse uri
11
- end
12
- super(:uri=>uri)
13
- end
14
-
6
+ module Node
15
7
  def assert(predicate, object)
16
8
  curied_predicate = case
17
9
  when predicate.could_be_a_safe_curie? then Curie.new_from_curie(predicate)
@@ -68,8 +60,12 @@ module RDFObject
68
60
  end
69
61
 
70
62
  def relate(predicate, resource)
71
- unless resource.is_a?(self.class)
72
- resource = self.class.new(resource)
63
+ unless resource.is_a?(Resource) or resource.is_a?(BlankNode) or resource.is_a?(ResourceReference)
64
+ if BlankNode.is_bnode_id?(resource)
65
+ resource = BlankNode.new(resource)
66
+ else
67
+ resource = Resource.new(resource)
68
+ end
73
69
  end
74
70
  self.assert(predicate, resource)
75
71
  end
@@ -77,7 +73,7 @@ module RDFObject
77
73
  def describe
78
74
  response = HTTPClient.fetch(self.uri)
79
75
  local_collection = Parser.parse(response[:content], {:base_uri=>response[:uri]})
80
- return unless local_collection[self.uri]
76
+ return unless local_collection && local_collection[self.uri]
81
77
  local_collection[self.uri].assertions.each do | predicate, object |
82
78
  [*object].each do | obj |
83
79
  self.assert(predicate, obj) unless self.assertion_exists?(predicate, obj)
@@ -115,9 +111,9 @@ module RDFObject
115
111
  objects = [self[uri][pred]]
116
112
  end
117
113
  objects.each do | object |
118
- line = "<#{self.uri}> <#{uri}#{pred}> "
119
- if (object.is_a?(Resource) or object.is_a?(ResourceReference))
120
- line << "<#{object.uri}>"
114
+ line = "#{ntriples_format} <#{uri}#{pred}> "
115
+ if object.is_a?(ResourceReference)
116
+ line << object.ntriples_format
121
117
  else
122
118
  line << "#{object.to_json}"
123
119
  if (object.respond_to?(:data_type) || object.respond_to?(:language))
@@ -155,8 +151,8 @@ module RDFObject
155
151
  rdf
156
152
  end
157
153
 
158
- def rdf_description_block(depth)
159
- rdf = "<rdf:Description rdf:about=\"#{CGI.escapeHTML(self.uri)}\">"
154
+ def rdf_description_block(depth=0)
155
+ rdf = "<rdf:Description #{xml_subject_attribute}>"
160
156
  namespaces = {}
161
157
  Curie.get_mappings.each_pair do |key, value|
162
158
  if self.respond_to?(key.to_sym)
@@ -165,11 +161,11 @@ module RDFObject
165
161
  rdf << "<#{key}:#{predicate}"
166
162
  namespaces["xmlns:#{key}"] = "#{Curie.parse("[#{key}:]")}"
167
163
  if object.is_a?(RDFObject::ResourceReference)
168
- if depth > 0
169
- rdf << " rdf:resource=\"#{CGI.escapeHTML(object.uri)}\" />"
164
+ if depth == 0
165
+ rdf << " #{object.xml_object_attribute} />"
170
166
  else
171
167
  rdf << ">"
172
- ns, rdf_data = object.resource.rdf_description_block(depth+1)
168
+ ns, rdf_data = object.resource.rdf_description_block(depth-1)
173
169
  namespaces.merge!(ns)
174
170
  rdf << rdf_data
175
171
  rdf << "</#{key}:#{predicate}>"
@@ -192,7 +188,7 @@ module RDFObject
192
188
  end
193
189
 
194
190
  def ==(other)
195
- return false unless other.is_a?(Resource) or other.is_a?(ResourceReference)
191
+ return false unless other.is_a?(self.class) or other.is_a?(ResourceReference)
196
192
  return false unless self.uri == other.uri
197
193
  Curie.get_mappings.each do | prefix, uri |
198
194
  next unless self[uri] or other[uri]
@@ -212,8 +208,8 @@ module RDFObject
212
208
  return false unless self[uri][pred].index(idx)
213
209
  end
214
210
  else
215
- if self[uri][pred].is_a?(Resource) or self[uri][pred].is_a?(ResourceReference)
216
- return false unless other[uri][pred].is_a?(Resource) or other[uri][pred].is_a?(ResourceReference)
211
+ if self[uri][pred].is_a?(Resource) or self[uri][pred].is_a?(BlankNode) or self[uri][pred].is_a?(ResourceReference)
212
+ return false unless other[uri][pred].is_a?(Resource) or self[uri][pred].is_a?(BlankNode) or other[uri][pred].is_a?(ResourceReference)
217
213
  return false unless self[uri][pred].uri == other[uri][pred].uri
218
214
  else
219
215
  return false unless self[uri][pred] == other[uri][pred]
@@ -227,6 +223,73 @@ module RDFObject
227
223
  end
228
224
  true
229
225
  end
226
+ end
227
+
228
+ class Resource < OpenStruct
229
+ include RDFObject::Node
230
+ attr_reader :table
231
+ def initialize(uri)
232
+ if uri.could_be_a_safe_curie?
233
+ uri = Curie.parse uri
234
+ end
235
+ super(:uri=>uri)
236
+ end
237
+
238
+ def xml_subject_attribute
239
+ "rdf:about=\"#{CGI.escapeHTML(self.uri)}\""
240
+ end
241
+
242
+ def xml_object_attribute
243
+ "rdf:resource=\"#{CGI.escapeHTML(self.uri)}\""
244
+ end
245
+
246
+ def ntriples_format
247
+ "<#{uri}>"
248
+ end
249
+ end
250
+
251
+ class BlankNode < OpenStruct
252
+ include RDFObject::Node
253
+ require 'md5'
254
+ def initialize(node_id = nil)
255
+ super(:node_id=>sanitize_bnode_id(node_id||MD5.md5(self.object_id.to_s + "/" + DateTime.now.to_s).to_s))
256
+ end
257
+
258
+ def describe; end
259
+
260
+ def uri
261
+ "_:#{self.node_id}"
262
+ end
263
+
264
+ def xml_subject_attribute
265
+ "rdf:nodeID=\"#{self.node_id}\""
266
+ end
267
+
268
+ def xml_object_attribute
269
+ xml_subject_attribute
270
+ end
271
+
272
+ def ntriples_format
273
+ uri
274
+ end
275
+
276
+ def self.is_bnode_id?(str)
277
+ return true if str =~ /^_\:/
278
+ if str.could_be_a_safe_curie?
279
+ str = Curie.parse str
280
+ end
281
+ begin
282
+ uri = URI.parse(str)
283
+ return true unless uri.scheme
284
+ rescue URI::InvalidURIError
285
+ return true
286
+ end
287
+ return false
288
+ end
289
+
290
+ def sanitize_bnode_id(str)
291
+ str.sub(/^_\:/,"")
292
+ end
230
293
  end
231
294
 
232
295
  class ResourceReference
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.9.1
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ross Singer
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-09 00:00:00 -05:00
12
+ date: 2010-02-10 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency