Debian Codegraph @

ruby/lib/rdf/redland/node.rb

raw symbols SCIP
1 require 'rdf/redland'
2 require 'rdf/redland/uri'
3
4 module Redland
5
6 class Node
7 attr_reader :node
8
9 @@node_types = {0 =>'NODE_TYPE_UNKNOWN',
10 1 =>'NODE_TYPE_RESOURCE',
11 2 =>'NODE_TYPE_LITERAL',
12 4 =>'NODE_TYPE_BLANK'}
13
14 # Create an RDF Node
15 #
16 # Resource or property node creation
17 # n1 = Node.new(Uri.new("http://example.com/foo"))
18 #
19 # String literal node creation
20 # n2 = Node.new("foo")
21 def initialize(arg=nil)
22 if arg.class == String
23 temp = arg
24 arg = {}
25 arg[:literal]= temp
26 @node = self.node_from_hash(arg)
27 elsif arg.class == Hash
28 @node = node_from_hash(arg)
29 else
30 @node = Node.ensure(arg)
31 end
32 raise RedlandError.new("Node construction failed") if not @node
33 ObjectSpace.define_finalizer(self,Node.create_finalizer(@node))
34 end
35
36 # sets this node's value using a blank or URI extracted from a Hash
37 # (?? confirm)
38 def node_from_hash(hash)
39 if hash.key?(:blank)
40 node = Redland.librdf_new_node_from_blank_identifier($world.world,hash[:blank])
41 end
42 if hash.key?(:from_object)
43 if hash.key?(:do_not_copy)
44 node = hash[:from_object]
45 else
46 node = Redland.librdf_new_node_from_node(hash[:from_object])
47 end
48 end
49
50 if hash.key?(:uri_string)
51 node = Redland.librdf_new_node_from_uri_string($world.world,hash[:uri_string])
52 end
53 if hash.key?(:literal)
54 node = node_from_literal(hash)
55 end
56 return node
57 end
58
59 # sets this node's value using a literal value extracted from a Hash
60 # (?? confirm)
61 def node_from_literal(hash)
62 xml_language = hash.key?(:xml_language) ? hash[:xml_language] : ""
63 wf_xml = hash.key?(:wf_xml?) ? 1 : 0
64 if hash.key?(:datatype)
65 datatype = hash[:datatype]
66 node = Redland.librdf_new_node_from_typed_literal($world.world,hash[:literal],xml_language,datatype.uri)
67 else
68 node = Redland.librdf_new_node_from_literal($world.world,hash[:literal],xml_language,wf_xml)
69 end
70 return node
71 end
72
73 # Sets this node's value to a blank node with the specified ID (?? confirm)
74 def bnode(id=nil)
75 @node = Redland.librdf_new_node_from_blank_identifier($world.world,id)
76 end
77
78 # You shouldn't use this. Used internally for cleanup.
79 def Node.create_finalizer(node)
80 proc {|id| Redland::librdf_free_node(node) }
81 end
82
83 # Creates a new blank node with the specified ID (?? confirm)
84 def Node.anon(id)
85 n = Node.new('')
86 n.bnode(id)
87 n
88 end
89
90 # Return true if node is a literal
91 def literal?
92 return (Redland.librdf_node_is_literal(self.node) !=0)
93 end
94
95 # Return true if node is a resource with a URI
96 def resource?
97 return (Redland.librdf_node_is_resource(self.node) !=0)
98 end
99
100 # Return true if node is a blank node
101 def blank?
102 return (Redland.librdf_node_is_blank(self.node) !=0)
103 end
104
105 # Equivalency. Only works for comparing two Nodes
106 def ==(other)
107 return (Redland.librdf_node_equals(self.node,other.node) != 0)
108 end
109
110 # Convert this to a string
111 def to_s
112 if self.literal?
113 Redland.librdf_node_get_literal_value(self.node)
114 elsif self.blank?
115 Redland.librdf_node_get_blank_identifier(self.node)
116 elsif self.resource?
117 uri = Redland.librdf_node_get_uri(self.node)
118 Redland.librdf_uri_to_string(uri) unless uri.nil?
119 else
120 nil
121 end
122 end
123
124 # Get the type of this node as a string (Node.node_types)
125 def node_type()
126 return @@node_types[Redland.librdf_node_get_type(self.node)]
127 end
128
129 # return a copy of the internal uri
130 def uri
131 if self.resource?
132 return Uri.new(Redland.librdf_node_get_uri(self.node))
133 else
134 raise NodeTypeError.new("Can't get URI for node type %s" % self.node_type() )
135 end
136 end
137
138 # returns a Literal if the node is a literal type
139 def literal
140 unless self.literal?
141 raise NodeTypeError.new("Can't get literal value for node type %s" % self.node_type)
142 end
143 return Literal.from_node(self.node)
144 end
145
146 # returns the blank identifier for this node if the internal model is
147 # a blank node
148 def blank_identifier()
149 unless self.blank?
150 raise NodeTypeError.new("Can't get blank identifier for node type %s" % self.node_type)
151 else
152 return Redland.librdf_node_get_blank_identifier(self.node)
153 end
154 end
155
156 # Ensures that the argument is a node by constructing one or returning nil
157 # (?? confirm)
158 def Node.ensure(node)
159 case node
160 when Node
161 my_node = Redland.librdf_new_node_from_node(node.node)
162 when SWIG::TYPE_p_librdf_node_s
163 my_node = Redland.librdf_new_node_from_node(node)
164 when Uri
165 my_node = Redland.librdf_new_node_from_uri($world.world,node.uri)
166 when URI
167 my_node = Redland.librdf_new_node_from_uri_string($world.world,node.to_s)
168 else
169 my_node = nil
170 end
171 return my_node
172 end
173
174 end
175
176 # A class to make Blank Node creation easier
177 # model = Model.new
178 # dom = BNode.new('dom') # create with optional id
179 #
180 # I will probably depreciate this and just use Resource.new()
181 # to generate blank nodes.
182 class BNode < Node
183
184 def initialize(id=nil)
185 @node = Redland.librdf_new_node_from_blank_identifier($world.world,id)
186 if not @node then raise RedlandError.new("Node construction failed")end
187 ObjectSpace.define_finalizer(self,Node.create_finalizer(@node))
188 end
189
190 end
191
192 # A helper class for namespace. Caches the nodes for a given namespaces
193 # foaf = Namespace.new('http://xmlns.com/0.1/foaf/')
194 # model.find(nil,foaf['name'],'Dominic')
195 # puts foaf['name'] => http://xmlns.com/0.1/foaf/name
196 class Namespace < Node
197
198 def initialize(namespace)
199 super(:uri_string=>namespace)
200 @nodecache = {}
201 end
202
203 def [](item)
204 return the_node(item)
205 end
206
207 def the_node(localName)
208 if not @nodecache.member? localName
209 @nodecache[localName] = Node.new(:uri_string=>self.uri.to_s + localName)
210 end
211 return @nodecache[localName]
212 end
213
214 private :the_node
215
216 end
217
218 # A literal node
219 class Literal < Node
220 include Redland
221
222 # the string value of the literal
223 attr_reader :value
224
225 # the language of the literal
226 attr_reader :language
227
228 # create a new Literal node
229 # literal = Literal.new('Dominic')
230 # label = Literal.new('Name','en')
231 def initialize(str,lang=nil,uri=nil,is_xml=false)
232 @value = str
233 @language = lang
234 is_xml = is_xml==true ? 1 : 0
235 if uri
236 @node = Redland.librdf_new_node_from_typed_literal($world.world,value,lang,uri.uri)
237 else
238 @node = Redland.librdf_new_node_from_literal($world.world,value,lang,is_xml)
239 end
240
241 raise RedlandError.new("Node construction failed") if not @node
242 ObjectSpace.define_finalizer(self,Node.create_finalizer(@node))
243 end
244
245 # create a literal from another Node
246 def Literal.from_node(node)
247 lang = Redland.librdf_node_get_literal_value_language(node) if Redland.librdf_node_get_literal_value_language(node)
248 str = Redland.librdf_node_get_literal_value(node)
249 hash_uri = Redland.librdf_node_get_literal_value_datatype_uri(node)
250 hash_uri = Uri.new(Redland.librdf_uri_to_string(hash_uri)) if hash_uri
251 return Literal.new(str,lang,hash_uri)
252 end
253
254 # create a literal from and xml string
255 # literal = Literal.from_xml('<em>This is emphasized</em>')
256 def Literal.from_xml(str,lang=nil)
257 return Literal.new(str,lang,nil,true)
258 end
259 end
260 end #Redland
261