| 1 |
require 'rdf/redland' |
| 2 |
require 'rdf/redland/uri' |
| 3 |
require 'rdf/redland/stream' |
| 4 |
|
| 5 |
|
| 6 |
module Redland |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class Parser |
| 22 |
attr_accessor :context |
| 23 |
|
| 24 |
|
| 25 |
def initialize(name='rdfxml',mime_type="application/rdf+xml",uri=nil,context=nil) |
| 26 |
@context = nil |
| 27 |
@idents = [] |
| 28 |
uri=uri.uri if uri |
| 29 |
@parser = Redland.librdf_new_parser($world.world,name,mime_type,uri) |
| 30 |
raise RedlandError.new("Parser construction failed") if !@parser |
| 31 |
ObjectSpace.define_finalizer(self,Parser.create_finalizer(@parser)) |
| 32 |
end |
| 33 |
|
| 34 |
def Parser.create_finalizer(parser) |
| 35 |
proc {|id| |
| 36 |
|
| 37 |
Redland::librdf_free_parser(parser) |
| 38 |
} |
| 39 |
end |
| 40 |
|
| 41 |
|
| 42 |
def Parser.ntriples(uri=nil) |
| 43 |
return Parser.new('ntriples',"",uri) |
| 44 |
end |
| 45 |
|
| 46 |
|
| 47 |
def Parser.turtle(uri=nil) |
| 48 |
return Parser.new('turtle',"",uri) |
| 49 |
end |
| 50 |
|
| 51 |
|
| 52 |
def Parser.raptor(uri=nil) |
| 53 |
return Parser.new('rdfxml','application/rdf+xml',uri) |
| 54 |
end |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
def parse_as_stream(uri,base_uri=nil) |
| 60 |
if uri.class == String then uri = Uri.new(uri) end |
| 61 |
if base_uri.class == String then base_uri = Uri.new(base_uri) end |
| 62 |
unless base_uri |
| 63 |
my_stream = Redland::librdf_parser_parse_as_stream @parser, uri.uri, nil |
| 64 |
else |
| 65 |
my_stream = Redland::librdf_parser_parse_as_stream @parser, uri.uri ,base_uri.uri |
| 66 |
end |
| 67 |
return nil if !my_stream |
| 68 |
stream = Stream.new(my_stream,self) |
| 69 |
if !block_given? |
| 70 |
return stream |
| 71 |
else |
| 72 |
while not stream.end? |
| 73 |
yield stream.current |
| 74 |
stream.next |
| 75 |
end |
| 76 |
end |
| 77 |
end |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
def parse_into_model(model,uri,base_uri=nil,context=@context) |
| 86 |
|
| 87 |
if uri.class == String |
| 88 |
uri = Uri.new(uri) |
| 89 |
end |
| 90 |
if base_uri |
| 91 |
if base_uri.class == String |
| 92 |
base_uri = Uri.new(base_uri) |
| 93 |
end |
| 94 |
else |
| 95 |
base_uri=uri |
| 96 |
end |
| 97 |
if not context |
| 98 |
begin |
| 99 |
r = Redland.librdf_parser_parse_into_model(@parser,uri.uri,base_uri.uri,model.model) |
| 100 |
rescue RedlandError => err |
| 101 |
print "caught error"+ err |
| 102 |
end |
| 103 |
else |
| 104 |
raise RedlandError.new("Cannot make a Node from an object of #{context.class}") if not context |
| 105 |
context = Node.ensure(context) |
| 106 |
my_stream = Redland::librdf_parser_parse_as_stream(@parser,uri.uri,base_uri.uri) |
| 107 |
Redland.librdf_model_context_add_statements(model.model,context,my_stream) |
| 108 |
|
| 109 |
end |
| 110 |
|
| 111 |
end |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
def parse_string_as_stream(string,base_uri) |
| 119 |
if base_uri.class == String then base_uri = Uri.new(base_uri) end |
| 120 |
if not base_uri then raise RedlandError("A base URI is required when parsing a string") end |
| 121 |
my_stream = Redland::librdf_parser_parse_string_as_stream(@parser,string,base_uri.uri) |
| 122 |
return nil if !my_stream |
| 123 |
stream = Stream.new(my_stream,self) |
| 124 |
return stream if !block_given? |
| 125 |
while not stream.end? |
| 126 |
yield stream.current |
| 127 |
stream.next |
| 128 |
end |
| 129 |
end |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
def parse_string_into_model(model,string,base_uri=nil,context=@context) |
| 137 |
if base_uri.class == String then base_uri = Uri.new(base_uri)end |
| 138 |
if not base_uri then raise RedlandError.new("A base URI is required when parsing a string") end |
| 139 |
if not context |
| 140 |
return (Redland::librdf_parser_parse_string_into_model(@parser,string,base_uri.uri,model.model)== 0) |
| 141 |
else |
| 142 |
self.parse_string_as_stream(string,base_uri.uri){|s| model.add_statement(s,context)} |
| 143 |
end |
| 144 |
end |
| 145 |
|
| 146 |
|
| 147 |
def feature(uri) |
| 148 |
if uri.class == String then uri = Uri.new(uri) end |
| 149 |
value = Redland::librdf_parser_get_feature(@parser,uri.uri) |
| 150 |
if value == "NULL" or value == nil |
| 151 |
return nil |
| 152 |
else |
| 153 |
return Node.new(:from_object=>value) |
| 154 |
end |
| 155 |
end |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
def setFeature(uri,value) |
| 160 |
if uri.class == String then uri = Uri.new(uri) end |
| 161 |
if value.class == String then value = Node.new(:literal=>value) end |
| 162 |
return (Redland::librdf_parser_set_feature(@parser,uri.uri,value.node) == 0) |
| 163 |
end |
| 164 |
|
| 165 |
def add_ident(node) |
| 166 |
@idents << node |
| 167 |
end |
| 168 |
|
| 169 |
def smush_string(string,model,idents=@idents) |
| 170 |
to_change = {} |
| 171 |
temp_model = Model.new() |
| 172 |
self.parse_string_into_model(temp_model,string,'http://xml.com') |
| 173 |
idents.each do |ident| |
| 174 |
temp_model.find(nil,ident,nil){|s,p,o| |
| 175 |
old_id = s |
| 176 |
new_id = model.subject(p,o) |
| 177 |
if new_id |
| 178 |
to_change[old_id.to_s]= new_id if !to_change.key?(old_id.to_s) |
| 179 |
end |
| 180 |
} |
| 181 |
end |
| 182 |
|
| 183 |
temp_model.triples do |s,p,o| |
| 184 |
s = to_change[s.to_s] if to_change.key?(s.to_s) |
| 185 |
model.add(s,p,o) |
| 186 |
end |
| 187 |
|
| 188 |
end |
| 189 |
|
| 190 |
def smush_file(model,uri,base_uri=nil,context=nil,idents=@idents) |
| 191 |
to_change = {} |
| 192 |
temp_model = Model.new() |
| 193 |
self.parse_into_model(temp_model,uri,base_uri,context) |
| 194 |
idents.each do |ident| |
| 195 |
temp_model.find(nil,ident,nil){|s,p,o| |
| 196 |
old_id = s |
| 197 |
new_id = model.subject(p,o) |
| 198 |
if new_id |
| 199 |
to_change[old_id.to_s]= new_id if !to_change.key?(old_id.to_s) |
| 200 |
end |
| 201 |
} |
| 202 |
end |
| 203 |
|
| 204 |
temp_model.triples do |s,p,o| |
| 205 |
s = to_change[s.to_s] if to_change.key?(s.to_s) |
| 206 |
model.add(s,p,o,context) |
| 207 |
end |
| 208 |
|
| 209 |
end |
| 210 |
|
| 211 |
end |
| 212 |
|
| 213 |
|
| 214 |
class ContextParser |
| 215 |
require 'uri' |
| 216 |
|
| 217 |
|
| 218 |
def initialize(file) |
| 219 |
case file |
| 220 |
when String |
| 221 |
file = file.expand_path(file) |
| 222 |
raise RedlandError("File doesn't exist") unless File.file?(file) |
| 223 |
@uri == Uri.new(file) |
| 224 |
when File |
| 225 |
end |
| 226 |
@parser = Redland.librdf_new_parser($world.world,name,mime_type,@uri.uri,@uri.uri) |
| 227 |
raise RedlandError.new("Parser construction failed") if !@parser |
| 228 |
ObjectSpace.define_finalizer(self,Parser.create_finalizer(@parser)) |
| 229 |
|
| 230 |
end |
| 231 |
|
| 232 |
|
| 233 |
def parse_into_model(model,string,base_uri) |
| 234 |
super(model,@uri,nil,@uri) |
| 235 |
end |
| 236 |
|
| 237 |
|
| 238 |
end |
| 239 |
|
| 240 |
|
| 241 |
end |
| 242 |
|
| 243 |
|