rdfobjects 0.10.7 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -90,6 +90,14 @@ module RDFObject
90
90
  rdf << rdf_data
91
91
  rdf << "</rdf:RDF>"
92
92
  rdf
93
- end
93
+ end
94
+
95
+ def to_json
96
+ j_hash = {}
97
+ self.values.each do |resource|
98
+ j_hash.merge!(resource.to_json_hash)
99
+ end
100
+ JSON.generate(j_hash)
101
+ end
94
102
  end
95
103
  end
@@ -207,12 +207,13 @@ module RDFObject
207
207
  scanner.getch
208
208
  language = scanner.scan_until(/\s?\.\n?$/)
209
209
  language.sub!(/\s?\.\n?$/,'')
210
+ language = language.to_sym
210
211
  elsif scanner.match?(/\^\^/)
211
212
  scanner.skip_until(/</)
212
213
  data_type = scanner.scan_until(/>/)
213
214
  data_type.sub!(/>$/,'')
214
215
  end
215
- object = Literal.new(tmp_object,{:data_type=>data_type,:language=>language})
216
+ object = RDF::Literal.new(tmp_object,{:datatype=>data_type,:language=>language})
216
217
  type = "literal"
217
218
  end
218
219
  {:subject=>subject, :predicate=>predicate, :object=>object, :type=>type}
@@ -335,8 +336,10 @@ module RDFObject
335
336
  "http://www.w3.org/1999/02/22-rdf-syntax-ns#nodeID", "http://www.w3.org/1999/02/22-rdf-syntax-ns#datatype"]
336
337
  attributes.each_pair do | uri, value |
337
338
  next if skip.index(uri)
338
- lit = Literal.new(value, {:data_type=>attributes["http://www.w3.org/1999/02/22-rdf-syntax-ns#datatype"],
339
- :language=>attributes["http://www.w3.org/XML/1998/namespace/lang"]})
339
+ lit = RDF::Literal.new(value, {:datatype=>attributes["http://www.w3.org/1999/02/22-rdf-syntax-ns#datatype"]})
340
+ if attributes["http://www.w3.org/XML/1998/namespace/lang"]
341
+ lit.language = attributes["http://www.w3.org/XML/1998/namespace/lang"].to_sym
342
+ end
340
343
  self.current_resource.assert(uri, lit)
341
344
  end
342
345
  end
@@ -365,7 +368,7 @@ module RDFObject
365
368
  end
366
369
  if attributes["http://www.w3.org/1999/02/22-rdf-syntax-ns#datatype"] || attributes["http://www.w3.org/XML/1998/namespace/lang"]
367
370
  layer[:datatype] = attributes["http://www.w3.org/1999/02/22-rdf-syntax-ns#datatype"] if attributes["http://www.w3.org/1999/02/22-rdf-syntax-ns#datatype"]
368
- layer[:language] = attributes["http://www.w3.org/XML/1998/namespace/lang"] if attributes["http://www.w3.org/XML/1998/namespace/lang"]
371
+ layer[:language] = attributes["http://www.w3.org/XML/1998/namespace/lang"].to_sym if attributes["http://www.w3.org/XML/1998/namespace/lang"]
369
372
  end
370
373
  layer[:base_uri] = Addressable::URI.parse(attributes["http://www.w3.org/XML/1998/namespace/base"]).normalize if attributes["http://www.w3.org/XML/1998/namespace/base"]
371
374
  @hierarchy << layer
@@ -381,7 +384,7 @@ module RDFObject
381
384
  end
382
385
 
383
386
  def assert_literal(layer)
384
- lit = RDFObject::Literal.new(layer[:literal], {:data_type=>layer[:datatype], :language=>layer[:language]})
387
+ lit = RDF::Literal.new(layer[:literal], {:datatype=>layer[:datatype], :language=>layer[:language]})
385
388
  self.current_resource.assert(layer[:predicate], lit) if layer[:predicate]
386
389
  end
387
390
 
@@ -440,16 +443,20 @@ module RDFObject
440
443
  end
441
444
  end
442
445
 
443
- class RDFAParser < XMLParser
446
+ class RDFAParser < Parser
444
447
  def data=(xhtml)
445
- if xhtml.is_a?(Nokogiri::XML::Document)
446
- doc = xhtml
447
- else
448
- doc = Nokogiri::XML.parse(xhtml)
449
- end
450
- xslt = Nokogiri::XSLT(open(File.dirname(__FILE__) + '/../xsl/RDFa2RDFXML.xsl'))
451
- @rdfxml = xslt.apply_to(doc)
448
+ @rdfa = xhtml
452
449
  end
450
+
451
+ def parse
452
+ rdfa_parser = RdfaParser::RdfaParser.new()
453
+ html = open(uri)
454
+ ntriples = ""
455
+ rdfa_parser.parse(@rdfa, base_uri).each do | triple |
456
+ ntriples << triple.to_ntriples + "\n"
457
+ end
458
+ RDFObject::Parser.parse(ntriples)
459
+ end
453
460
  end
454
461
 
455
462
  class JSONParser < RDFObject::Parser
@@ -472,12 +479,12 @@ module RDFObject
472
479
  if object['type'] == 'literal'
473
480
  opts = {}
474
481
  if object['lang']
475
- opts[:language] = object['lang']
482
+ opts[:language] = object['lang'].to_sym
476
483
  end
477
484
  if object['datatype']
478
- opts[:data_type] = object['datatype']
485
+ opts[:datatype] = object['datatype']
479
486
  end
480
- literal = Literal.new(object['value'],opts)
487
+ literal = RDF::Literal.new(object['value'],opts)
481
488
  resource.assert(predicate, literal)
482
489
  elsif object['type'] == 'uri'
483
490
  o = @collection.find_or_create(object['value'])
@@ -14,6 +14,8 @@ module RDFObject
14
14
  pred_attr = self.send(curied_predicate.prefix.to_sym)
15
15
  if object.is_a?(Resource)
16
16
  object = ResourceReference.new(object)
17
+ elsif !object.is_a?(ResourceReference) && !object.is_a?(BlankNode)
18
+ object = RDF::Literal.new(object) unless object.is_a?(RDF::Literal)
17
19
  end
18
20
  return if assertion_exists?(predicate, object)
19
21
  if pred_attr[curied_predicate.reference]
@@ -24,7 +26,8 @@ module RDFObject
24
26
  else
25
27
  pred_attr[curied_predicate.reference] = object
26
28
  end
27
- end
29
+ end
30
+
28
31
 
29
32
  def assertion_exists?(predicate, object)
30
33
  return false unless self[predicate]
@@ -115,15 +118,10 @@ module RDFObject
115
118
  if object.is_a?(ResourceReference) || object.is_a?(BlankNode)
116
119
  line << object.ntriples_format
117
120
  else
118
- line << "#{object.to_json}"
119
- if (object.respond_to?(:data_type) || object.respond_to?(:language))
120
- if object.data_type
121
- line << "^^<#{object.data_type}>"
122
- end
123
- if object.language
124
- line << "@#{object.language}"
125
- end
126
- end
121
+ object = RDF::Literal.new(object) unless object.is_a?(RDF::Literal)
122
+ line << "#{object.value.to_json}"
123
+ line << "^^<#{object.datatype}>" if object.has_datatype?
124
+ line << "@#{object.language}" if object.has_language?
127
125
  end
128
126
  line << " .\n"
129
127
  ntriples << line
@@ -151,6 +149,40 @@ module RDFObject
151
149
  rdf
152
150
  end
153
151
 
152
+ def to_json_hash
153
+ j_hash = {self.uri=>{}}
154
+ self.assertions.each_pair do |pred,objects|
155
+ j_hash[self.uri][pred] = []
156
+ if objects.is_a?(Array)
157
+ objects.each do |object|
158
+ j_hash[self.uri][pred] << object_to_json_hash(object)
159
+ end
160
+ else
161
+ j_hash[self.uri][pred] << object_to_json_hash(objects)
162
+ end
163
+ end
164
+ j_hash
165
+ end
166
+
167
+ def object_to_json_hash(object)
168
+ if object.is_a?(Resource) or object.is_a?(ResourceReference)
169
+ o = {:type=>"uri", :value=>object.uri}
170
+ elsif object.is_a?(BlankNode)
171
+ o = {:type=>"bnode", :value=>object.uri}
172
+ else
173
+ object = RDF::Literal.new(object) unless object.is_a?(RDF::Literal)
174
+ o = {:type=>"literal", :value=>object.value}
175
+ o[:lang] = object.language.to_s if object.has_language?
176
+ o[:datatype] = object.datatype.to_s if object.has_datatype?
177
+ end
178
+ o
179
+ end
180
+
181
+
182
+ def to_json
183
+ JSON.generate(to_json_hash)
184
+ end
185
+
154
186
  def rdf_description_block(depth=0)
155
187
  rdf = "<rdf:Description #{xml_subject_attribute}>"
156
188
  namespaces = {}
@@ -171,13 +203,14 @@ module RDFObject
171
203
  rdf << "</#{key}:#{predicate}>"
172
204
  end
173
205
  else
206
+ object = RDF::Literal.new(object) unless object.is_a?(RDF::Literal)
174
207
  if object.language
175
208
  rdf << " xml:lang=\"#{object.language}\""
176
209
  end
177
- if object.data_type
178
- rdf << " rdf:datatype=\"#{object.data_type}\""
210
+ if object.datatype
211
+ rdf << " rdf:datatype=\"#{object.datatype}\""
179
212
  end
180
- rdf << ">#{CGI.escapeHTML(object.to_s)}</#{key}:#{predicate}>"
213
+ rdf << ">#{CGI.escapeHTML(object.value)}</#{key}:#{predicate}>"
181
214
  end
182
215
  end
183
216
  end
@@ -190,39 +223,29 @@ module RDFObject
190
223
  def ==(other)
191
224
  return false unless other.is_a?(self.class) or other.is_a?(ResourceReference)
192
225
  return false unless self.uri == other.uri
193
- Curie.get_mappings.each do | prefix, uri |
194
- next unless self[uri] or other[uri]
195
- return false if self[uri] && !other[uri]
196
- return false if !self[uri] && other[uri]
197
- return false if self[uri].class != other[uri].class
198
- if self[uri] != other[uri]
199
- if self[uri].is_a?(Hash)
200
- return false unless self[uri].keys.eql?(other[uri].keys)
201
- self[uri].keys.each do | pred |
202
- if self[uri][pred].is_a?(Array)
203
- return false unless self[uri][pred].length == other[uri][pred].length
204
- self[uri][pred].each do | idx |
205
- return false unless other[uri][pred].index(idx)
206
- end
207
- other[uri][pred].each do | idx |
208
- return false unless self[uri][pred].index(idx)
209
- end
210
- else
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)
213
- return false unless self[uri][pred].uri == other[uri][pred].uri
214
- else
215
- return false unless self[uri][pred] == other[uri][pred]
216
- end
217
- end
226
+ return false unless self.assertions.keys.sort == other.assertions.keys.sort
227
+ self.assertions.each_pair do |pred, objects|
228
+ return false if objects.class != other[pred].class
229
+ objects = [objects] unless objects.is_a?(Array)
230
+ objects.each do | o |
231
+ match = false
232
+ [*other[pred]].each do |oo|
233
+ next unless oo
234
+ next unless o.class == oo.class
235
+ if oo.is_a?(RDF::Literal)
236
+ match = o == oo
237
+ else
238
+ match = o.uri == oo.uri
218
239
  end
219
- else
220
- return false
240
+ break if match
221
241
  end
242
+ return false unless match
222
243
  end
244
+
223
245
  end
224
- true
246
+ return true
225
247
  end
248
+
226
249
  end
227
250
 
228
251
  class Resource < OpenStruct
data/lib/rdf_objects.rb CHANGED
@@ -2,11 +2,12 @@ module RDFObject
2
2
  require 'rubygems'
3
3
  require 'ostruct'
4
4
  require 'curies'
5
+ require 'rdf'
6
+ require 'rdfa_parser'
5
7
  require 'addressable/uri'
6
8
  require File.dirname(__FILE__) + '/rdf_objects/parsers'
7
9
  require File.dirname(__FILE__) + '/rdf_objects/rdf_resource'
8
10
  require File.dirname(__FILE__) + '/rdf_objects/curies'
9
- require File.dirname(__FILE__) + '/rdf_objects/data_types'
10
11
  require File.dirname(__FILE__) + '/rdf_objects/http_client'
11
12
  require File.dirname(__FILE__) + '/rdf_objects/collection'
12
13
  Curie.remove_prefixes!(:http)
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 10
8
- - 7
9
- version: 0.10.7
7
+ - 11
8
+ - 0
9
+ version: 0.11.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ross Singer
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-16 00:00:00 -04:00
17
+ date: 2010-06-03 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -53,6 +53,30 @@ dependencies:
53
53
  version: "0"
54
54
  type: :runtime
55
55
  version_requirements: *id003
56
+ - !ruby/object:Gem::Dependency
57
+ name: rdf
58
+ prerelease: false
59
+ requirement: &id004 !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ type: :runtime
67
+ version_requirements: *id004
68
+ - !ruby/object:Gem::Dependency
69
+ name: rdfa_parser
70
+ prerelease: false
71
+ requirement: &id005 !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ type: :runtime
79
+ version_requirements: *id005
56
80
  description: RDFObjects are intended to simplify working with RDF data by providing a (more) Ruby-like interface to resources (thanks to OpenStruct).
57
81
  email: rossfsinger@gmail.com
58
82
  executables: []