Debian Codegraph @

ruby/lib/rdf/redland/parser.rb

raw symbols SCIP
1 require 'rdf/redland'
2 require 'rdf/redland/uri'
3 require 'rdf/redland/stream'
4
5
6 module Redland
7
8 # The Parser class for rdf. There are currently three types of parsers
9 # *'rdfxml'- a rdf parser
10 # *'ntriples' a An triples parser
11 # *'turtle' - a parser for the turtle syntax as defined in
12 # http://www.dajobe.org/2004/01/turtle/
13 # *'rss-tag-soup' - a parser for all RSS flavours and some atom
14 # *'grddl' - GRDDL
15 #
16 # The last two may or may not be available depending on systems.
17 #
18 # model = Model.new()
19 # parser = Parser.ntriples()
20 # parser.parse_into_model(model,'file:./ical.rdf')
21 class Parser
22 attr_accessor :context
23
24 # create a new Parser. if no name is given, it defaults to parsing RDF/XML syntax
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| # "Finalizer on #{id}"
36 #$log_final.info "closing parser"
37 Redland::librdf_free_parser(parser)
38 }
39 end
40
41 # create a parser for the ntriples syntax
42 def Parser.ntriples(uri=nil)
43 return Parser.new('ntriples',"",uri)
44 end
45
46 # create a parser for the turtle syntax
47 def Parser.turtle(uri=nil)
48 return Parser.new('turtle',"",uri)
49 end
50
51 # create a parser for rdf syntax
52 def Parser.raptor(uri=nil)
53 return Parser.new('rdfxml','application/rdf+xml',uri)
54 end
55
56 # Parse the given uri with optional base_uri and return a
57 # Stream. If block_given, yield the Statement instead
58 # Returns nil on failure.
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 # Parse the syntax at the uri Uri with optional base
80 # Uri base_uri into Model model. If the base_uri is
81 # given then the content is parsed as if it was at the base_uri rather
82 # than the uri. If the optional context is given, the statement is added to the context
83 #
84 # NOTE: The order of the arguments to this method is different from the C API and other language bindings.
85 def parse_into_model(model,uri,base_uri=nil,context=@context)
86 #puts "parse_into_model context is #{context}"
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 #context
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 #self.parse_as_stream(uri,base_uri){|s| model.add_statement(s,context)}
109 end
110
111 end
112
113 # Parse the syntax in string with required base Uri
114 # base_uri
115 #
116 # Returns a Stream or yields Statement
117 # if block given. Returns nil on failure.
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 # Parse the syntax in String string with required Uri
132 # base_uri into Model model.
133 # if context is given then the context information is stored as well
134 #
135 # NOTE: The order of the arguments to this method is different from the C API and other language bindings.
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 #Get a parser feature. The feature is a Uri
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 # Set a parser feature. The feature is named Uri
158 # uri and the value is a Node.
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 #puts to_change
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 #puts to_change
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 # Class for parsing a file
214 class ContextParser
215 require 'uri'
216
217 # Create a new ContextParser for a file
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 # Parse a file into a model (?? confirm - why doesn't it use most of it's args)
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 #module Redland
242
243 #end