Module: Redlander::Parsing
- Included in:
- Model
- Defined in:
- lib/redlander/parsing.rb
Overview
Syntax parsing methods. “self” is assumed to be an instance of Redlander::Model
Instance Method Summary collapse
-
#from(content, options = {}) {|| ... } ⇒ Model
Core parsing method for non-streams.
-
#from_ntriples(content, options = {}) {|| ... } ⇒ void
Parse input in NTriples format.
-
#from_rdfxml(content, options = {}) {|| ... } ⇒ void
Parse input in RDF/XML format.
-
#from_turtle(content, options = {}) {|| ... } ⇒ void
Parse input in Turtls format.
-
#from_uri(uri, options = {}) {|| ... } ⇒ void
(also: #from_file)
Parse input as stream from URI (or File).
Instance Method Details
#from(content, options = {}) {|| ... } ⇒ Model
If a block is given, the extracted statements will be yielded into the block and inserted into the model depending on the output of the block (if true, the statement will be added, if false, the statement will not be added).
Core parsing method for non-streams
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 |
# File 'lib/redlander/parsing.rb', line 30 def from(content, = {}) format = [:format].to_s mime_type = [:mime_type] && [:mime_type].to_s type_uri = [:type_uri] && [:type_uri].to_s base_uri = [:base_uri] && [:base_uri].to_s content = Uri.new(content) if content.is_a?(URI) # FIXME: to be fixed in librdf: # ntriples parser absolutely needs "\n" at the end of the input if format == "ntriples" && !content.is_a?(Uri) && !content.end_with?("\n") content << "\n" end rdf_parser = Redland.librdf_new_parser(Redlander.rdf_world, format, mime_type, type_uri) raise RedlandError, "Failed to create a new '#{format}' parser" if rdf_parser.null? begin if block_given? rdf_stream = if content.is_a?(Uri) Redland.librdf_parser_parse_as_stream(rdf_parser, content.rdf_uri, base_uri) else Redland.librdf_parser_parse_string_as_stream(rdf_parser, content, base_uri) end raise RedlandError, "Failed to create a new stream" if rdf_stream.null? begin while Redland.librdf_stream_end(rdf_stream).zero? statement = Statement.new(Redland.librdf_stream_get_object(rdf_stream)) statements.add(statement) if yield statement Redland.librdf_stream_next(rdf_stream) end ensure Redland.librdf_free_stream(rdf_stream) end else if content.is_a?(Uri) Redland.librdf_parser_parse_into_model(rdf_parser, content.rdf_uri, base_uri, @rdf_model).zero? else Redland.librdf_parser_parse_string_into_model(rdf_parser, content, base_uri, @rdf_model).zero? end end ensure Redland.librdf_free_parser(rdf_parser) end self end |
#from_ntriples(content, options = {}) {|| ... } ⇒ void
This method returns an undefined value.
Parse input in NTriples format. Shortcut for #from(content, :format => “ntriples”).
94 95 96 |
# File 'lib/redlander/parsing.rb', line 94 def from_ntriples(content, = {}, &block) from(content, .merge(:format => "ntriples"), &block) end |
#from_rdfxml(content, options = {}) {|| ... } ⇒ void
This method returns an undefined value.
Parse input in RDF/XML format. Shortcut for #from(content, :format => “rdfxml”).
84 85 86 |
# File 'lib/redlander/parsing.rb', line 84 def from_rdfxml(content, = {}, &block) from(content, .merge(:format => "rdfxml"), &block) end |
#from_turtle(content, options = {}) {|| ... } ⇒ void
This method returns an undefined value.
Parse input in Turtls format. Shortcut for #from(content, :format => “turtle”).
104 105 106 |
# File 'lib/redlander/parsing.rb', line 104 def from_turtle(content, = {}, &block) from(content, .merge(:format => "turtle"), &block) end |
#from_uri(uri, options = {}) {|| ... } ⇒ void Also known as: from_file
This method returns an undefined value.
Parse input as stream from URI (or File)
114 115 116 117 118 119 120 |
# File 'lib/redlander/parsing.rb', line 114 def from_uri(uri, = {}, &block) if uri.is_a?(String) uri = URI.parse(uri) uri = URI.parse("file://#{File.(uri.to_s)}") if uri.scheme.nil? end from(uri, , &block) end |