rdfobjects 0.3.3 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rdf_objects/parsers.rb +1 -1
- data/lib/rdf_objects/rdf_resource.rb +54 -24
- data/lib/rdf_objects/rdfxml_parser.rb +29 -0
- metadata +2 -1
data/lib/rdf_objects/parsers.rb
CHANGED
@@ -1,39 +1,43 @@
|
|
1
1
|
require 'uri'
|
2
|
-
require 'builder'
|
3
2
|
require 'date'
|
4
3
|
require 'curies'
|
4
|
+
require 'weakref'
|
5
5
|
|
6
6
|
module RDFObject
|
7
7
|
class Resource < OpenStruct
|
8
8
|
class << self
|
9
|
-
attr_reader :instances
|
10
|
-
|
11
9
|
def instances
|
12
|
-
|
13
|
-
|
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
|
14
16
|
end
|
15
17
|
|
16
18
|
def reset!
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
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
|
23
29
|
end
|
24
30
|
|
25
31
|
def remove(resource)
|
26
|
-
instances
|
27
|
-
|
32
|
+
to_del = instances[resource.uri]
|
33
|
+
to_del.uri = nil
|
28
34
|
end
|
29
35
|
|
30
36
|
def exists?(uri)
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
false
|
36
|
-
end
|
37
|
+
ObjectSpace.each_object(self) { | rdf_object |
|
38
|
+
return true if rdf_object.uri == uri
|
39
|
+
}
|
40
|
+
false
|
37
41
|
end
|
38
42
|
end
|
39
43
|
|
@@ -42,7 +46,6 @@ module RDFObject
|
|
42
46
|
uri = Curie.parse uri
|
43
47
|
end
|
44
48
|
super(:uri=>uri)
|
45
|
-
self.class.register(self)
|
46
49
|
end
|
47
50
|
|
48
51
|
def assert(predicate, object)
|
@@ -53,6 +56,9 @@ module RDFObject
|
|
53
56
|
end
|
54
57
|
self.register_vocabulary(curied_predicate.prefix)
|
55
58
|
pred_attr = self.send(curied_predicate.prefix.to_sym)
|
59
|
+
if object.is_a?(Resource)
|
60
|
+
object = ResourceReference.new(object)
|
61
|
+
end
|
56
62
|
return if assertion_exists?(predicate, object)
|
57
63
|
if pred_attr[curied_predicate.reference]
|
58
64
|
unless pred_attr[curied_predicate.reference].is_a?(Array)
|
@@ -117,12 +123,36 @@ module RDFObject
|
|
117
123
|
end
|
118
124
|
|
119
125
|
def self.new(uri)
|
120
|
-
if self.exists?(uri)
|
121
|
-
|
126
|
+
#if self.exists?(uri)
|
127
|
+
# return self.instances[uri]
|
128
|
+
#end
|
129
|
+
if exists = self.instances[uri]
|
130
|
+
return exists
|
122
131
|
end
|
123
132
|
super(uri)
|
124
|
-
end
|
133
|
+
end
|
134
|
+
end
|
125
135
|
|
126
|
-
|
136
|
+
class ResourceReference
|
137
|
+
def initialize(resource)
|
138
|
+
@resource = resource
|
139
|
+
@inspect = "\"#{@resource.uri}\""
|
140
|
+
end
|
141
|
+
|
142
|
+
def ==(resource)
|
143
|
+
return resource == @resource
|
144
|
+
end
|
145
|
+
|
146
|
+
def inspect
|
147
|
+
@inspect
|
148
|
+
end
|
149
|
+
|
150
|
+
def resource
|
151
|
+
@resource
|
152
|
+
end
|
153
|
+
|
154
|
+
def method_missing(method, *args)
|
155
|
+
@resource.send(method, *args)
|
156
|
+
end
|
127
157
|
end
|
128
158
|
end
|
@@ -0,0 +1,29 @@
|
|
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
|
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.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ross Singer
|
@@ -60,6 +60,7 @@ files:
|
|
60
60
|
- lib/rdf_objects/http_client.rb
|
61
61
|
- lib/rdf_objects/parsers.rb
|
62
62
|
- lib/rdf_objects/rdf_resource.rb
|
63
|
+
- lib/rdf_objects/rdfxml_parser.rb
|
63
64
|
- lib/rdf_objects/serializers.rb
|
64
65
|
- lib/xsl/RDFa2RDFXML.xsl
|
65
66
|
- lib/xsl/rdf2nt.xsl
|