rdfobjects 0.8.2 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -54,10 +54,12 @@ class RDFObject::Literal
54
54
  else
55
55
  value
56
56
  end
57
- unless obj.is_a?(Float)
58
- raise ArgumentError if obj.to_s != value
59
- else
57
+ if obj.is_a?(Float)
60
58
  raise ArgumentError if obj.to_s.sub(/\.0/,'') != value
59
+ elsif obj.is_a?(DateTime)
60
+ raise ArgumentError if obj.to_s !~ /^#{Regexp.escape(value)}/
61
+ else
62
+ raise ArgumentError if obj.to_s != value
61
63
  end
62
64
 
63
65
  obj.set_data_type(options[:data_type])
@@ -18,9 +18,13 @@ module RDFObject
18
18
  http.request(request)
19
19
  end
20
20
  if response.code == "200"
21
- return response.body
21
+ return {:uri=>u.to_s, :content=>response.body}
22
22
  elsif response.code =~ /^30[0-9]$/
23
- return fetch(response.header['location'])
23
+ new_uri = URI.parse(response.header['location'])
24
+ unless new_uri.host
25
+ new_uri = u+new_uri
26
+ end
27
+ return fetch(new_uri.to_s)
24
28
  else
25
29
  raise response.message
26
30
  end
@@ -60,15 +60,16 @@ class UTF8Parser < StringScanner
60
60
  end
61
61
  module RDFObject
62
62
  class Parser
63
+ attr_reader :base_uri
63
64
  # Choose the best format parser from an admittedly small group of choices.
64
- def self.parse(rdf, format=nil)
65
- parser = init_parser(rdf, format)
65
+ def self.parse(rdf, options={})
66
+ parser = init_parser(rdf, options)
66
67
  parser.parse
67
68
  end
68
69
 
69
- def self.init_parser(rdf, format=nil)
70
- if format
71
- parser = case format
70
+ def self.init_parser(rdf, options={})
71
+ if options[:format]
72
+ parser = case options[:format]
72
73
  when 'rdfxml' then XMLParser.new(rdf)
73
74
  when 'rdfa' then RDFAParser.new(rdf)
74
75
  when 'ntriples' then NTriplesParser.new(rdf)
@@ -110,6 +111,9 @@ module RDFObject
110
111
  end
111
112
  end
112
113
  end
114
+ if options[:base_uri]
115
+ parser.base_uri = options[:base_uri]
116
+ end
113
117
  parser
114
118
  end
115
119
 
@@ -122,6 +126,27 @@ module RDFObject
122
126
  raise ArgumentError unless collection.is_a?(RDFObject::Collection)
123
127
  @collection = collection
124
128
  end
129
+
130
+ def base_uri=(uri)
131
+ if uri.is_a?(URI)
132
+ @base_uri = uri
133
+ else
134
+ @base_uri = URI.parse(uri)
135
+ end
136
+ end
137
+
138
+ def sanitize_uri(uri)
139
+ # Return if there's nothing to sanitize with
140
+ return uri unless self.base_uri
141
+ begin
142
+ u = URI.parse(uri)
143
+ return uri if u.host
144
+ fq_uri = self.base_uri+u
145
+ fq_uri.to_s
146
+ rescue URI::InvalidURIError
147
+ uri
148
+ end
149
+ end
125
150
  end
126
151
  class NTriplesParser < RDFObject::Parser
127
152
 
@@ -140,7 +165,7 @@ module RDFObject
140
165
  tmp_object = scanner.scan_until(/>\s?\.\s*\n?$/)
141
166
  tmp_object.sub!(/^</,'')
142
167
  tmp_object.sub!(/>\s?\.\s*\n?$/,'')
143
- object = tmp_object
168
+ object = self.sanitize_uri(tmp_object)
144
169
  type = "uri"
145
170
  else
146
171
  language = nil
@@ -235,6 +260,8 @@ module RDFObject
235
260
  @uris = []
236
261
  @tags = {}
237
262
  @parser = Nokogiri::XML::SAX::Parser.new(self)
263
+ @hierarchy = []
264
+ @xml_base = nil
238
265
  end
239
266
 
240
267
  def data=(xml)
@@ -267,70 +294,113 @@ module RDFObject
267
294
  hash
268
295
  end
269
296
 
270
- def add_layer(element_uri, resource_uri)
271
- if @uris.length > 0 && @current_predicate
272
- @collection[@uris.last].relate(@current_predicate, @collection.find_or_create(resource_uri))
273
- @current_predicate = nil
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
+ def add_layer name, attributes, prefix, uri, ns
323
+ layer = {:name=>"#{uri}#{name}"}
324
+ if attributes['about']
325
+ layer[:resource] = @collection.find_or_create(sanitize_uri(attributes['about']))
326
+ unless "#{uri}#{name}" == "http://www.w3.org/1999/02/22-rdf-syntax-ns#Description"
327
+ layer[:resource].relate("http://www.w3.org/1999/02/22-rdf-syntax-ns#type", @collection.find_or_create("#{uri}#{name}"))
328
+ end
329
+ if !@hierarchy.empty? && @hierarchy.last[:predicate]
330
+ self.current_resource.relate(self.current_predicate, layer[:resource])
331
+ end
332
+ elsif attributes["resource"]
333
+ self.current_resource.assert("#{uri}#{name}", @collection.find_or_create(sanitize_uri(attributes['resource'])))
334
+ layer[:predicate] = layer[:name]
335
+ else
336
+ unless layer[:name] == "http://www.w3.org/1999/02/22-rdf-syntax-ns#RDF"
337
+ layer[:predicate] = layer[:name]
338
+ end
274
339
  end
275
- @uris << resource_uri
276
- @tags[resource_uri] = element_uri
340
+ if attributes["datatype"] || attributes["lang"]
341
+ layer[:datatype] = attributes["datatype"] if attributes["datatype"]
342
+ layer[:language] = attributes["lang"] if attributes["lang"]
343
+ end
344
+ layer[:base_uri] = URI.parse(attributes["base"]) if attributes["base"]
345
+ @hierarchy << layer
277
346
  end
278
347
 
279
- def remove_layer(element_uri)
280
- uris = []
281
- @tags.each do |uri, el|
282
- uris << uri if el == element_uri
348
+ def remove_layer(name)
349
+ unless @hierarchy.last[:name] == name
350
+ throw ArgumentError, "Hierarchy out of sync."
283
351
  end
284
- uris.each do | uri |
285
- if @uris.last == uri
286
- @uris.pop
287
- @tags.delete(uri)
288
- break
352
+ layer = @hierarchy.pop
353
+ assert_literal(layer) if layer[:literal] && !layer[:literal].empty?
354
+ end
355
+
356
+ def assert_literal(layer)
357
+ lit = RDFObject::Literal.new(layer[:literal], {:data_type=>layer[:datatype], :language=>layer[:language]})
358
+ self.current_resource.assert(layer[:predicate], lit) if layer[:predicate]
359
+ end
360
+
361
+ def current_resource
362
+ @hierarchy.reverse.each do | layer |
363
+ return layer[:resource] if layer[:resource]
364
+ end
365
+ end
366
+
367
+ def current_predicate
368
+ @hierarchy.reverse.each do | layer |
369
+ return layer[:predicate] if layer[:predicate]
370
+ end
371
+ end
372
+
373
+ def current_literal
374
+ unless @hierarchy.empty?
375
+ return @hierarchy.last[:literal] if @hierarchy.last[:literal]
376
+ unless @hierarchy.last[:resource]
377
+ @hierarchy.last[:literal] = ""
378
+ return @hierarchy.last[:literal]
289
379
  end
290
380
  end
291
- @current_resource = @collection[@uris.last]
381
+ end
382
+
383
+ def base_uri
384
+ @hierarchy.reverse.each do | layer |
385
+ return layer[:base_uri] if layer[:base_uri]
386
+ end
387
+ @base_uri
292
388
  end
293
389
 
294
390
  def start_element_namespace name, attributes = [], prefix = nil, uri = nil, ns = {}
295
391
  attributes = attributes_to_hash(attributes)
296
- if attributes["about"]
297
- @current_resource = @collection.find_or_create(attributes['about'])
298
- add_layer("#{uri}#{name}", @current_resource.uri)
299
- unless "#{uri}#{name}" == "http://www.w3.org/1999/02/22-rdf-syntax-ns#Description"
300
- @current_resource.relate("http://www.w3.org/1999/02/22-rdf-syntax-ns#type", @collection.find_or_create("#{uri}#{name}"))
301
- end
302
- elsif attributes["resource"]
303
- resource = @collection.find_or_create(attributes['resource'])
304
- @current_resource.assert("#{uri}#{name}", resource)
305
- else
306
- @current_predicate = "#{uri}#{name}"
307
- end
308
- if attributes["datatype"] || attributes["lang"]
309
- @literal = {}
310
- @literal[:datatype] = attributes["datatype"] if attributes["datatype"]
311
- @literal[:language] = attributes["lang"] if attributes["lang"]
312
- @literal[:value] = ""
313
- end
392
+ add_layer(name, attributes, prefix, uri, ns)
314
393
  end
315
394
 
316
395
 
317
396
  def characters text
318
- if @current_predicate && !text.strip.empty?
319
- @literal ||={:value=>""}
320
- @literal[:value] << text.strip
397
+ if self.current_literal && !text.strip.empty?
398
+ self.current_literal << text.strip
321
399
  end
322
400
  end
323
401
 
324
402
  def end_element_namespace name, prefix = nil, uri = nil
325
- if @literal
326
- lit = RDFObject::Literal.new(@literal[:value], {:data_type=>@literal[:datatype], :language=>@literal[:language]})
327
- #puts "#{@current_resource.inspect} :: #{@current_predicate} == #{lit}"
328
- @current_resource.assert(@current_predicate, lit) if @current_predicate
329
- @literal = nil
330
- @current_predicate = nil
331
- else
332
- remove_layer("#{uri}#{name}")
333
- end
403
+ remove_layer("#{uri}#{name}")
334
404
  end
335
405
  end
336
406
 
@@ -75,8 +75,8 @@ module RDFObject
75
75
  end
76
76
 
77
77
  def describe
78
- rdf = HTTPClient.fetch(self.uri)
79
- local_collection = Parser.parse(rdf)
78
+ response = HTTPClient.fetch(self.uri)
79
+ local_collection = Parser.parse(response[:content], {:base_uri=>response[:uri]})
80
80
  local_collection[self.uri].assertions.each do | predicate, object |
81
81
  [*object].each do | obj |
82
82
  self.assert(predicate, obj) unless self.assertion_exists?(predicate, obj)
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.8.2
4
+ version: 0.9.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-01-12 00:00:00 -05:00
12
+ date: 2010-02-01 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency