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