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

Instance Method Details

#from(content, options = {}) {|| ... } ⇒ Model

Note:

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

Parameters:

  • content (String, URI)
    • Can be a String, causing the statements to be extracted directly from it, or

    • URI causing the content to be first pulled from the specified URI (or a local file, if URI schema == “file:”)

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :format (String)

    name of the parser to use,

  • :mime_type (String)

    MIME type of the syntax, if applicable,

  • :type_uri (String, URI)

    URI of syntax, if applicable,

  • :base_uri (String, URI)

    base URI, to be applied to the nodes with relative URIs.

Yield Parameters:

Returns:

Raises:

  • (RedlandError)

    if it fails to create a parser or stream



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, options = {})
  format = options[:format].to_s
  mime_type = options[:mime_type] && options[:mime_type].to_s
  type_uri = options[:type_uri] && options[:type_uri].to_s
  base_uri = options[:base_uri] && options[: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”).

Parameters:

  • content (String, URI)
    • Can be a String, causing the statements to be extracted directly from it, or

    • URI causing the content to be first pulled from the specified URI (or a local file, if URI schema == “file:”)

  • options (Hash) (defaults to: {})

Yield Parameters:



94
95
96
# File 'lib/redlander/parsing.rb', line 94

def from_ntriples(content, options = {}, &block)
  from(content, options.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”).

Parameters:

  • content (String, URI)
    • Can be a String, causing the statements to be extracted directly from it, or

    • URI causing the content to be first pulled from the specified URI (or a local file, if URI schema == “file:”)

  • options (Hash) (defaults to: {})

Yield Parameters:



84
85
86
# File 'lib/redlander/parsing.rb', line 84

def from_rdfxml(content, options = {}, &block)
  from(content, options.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”).

Parameters:

  • content (String, URI)
    • Can be a String, causing the statements to be extracted directly from it, or

    • URI causing the content to be first pulled from the specified URI (or a local file, if URI schema == “file:”)

  • options (Hash) (defaults to: {})

Yield Parameters:



104
105
106
# File 'lib/redlander/parsing.rb', line 104

def from_turtle(content, options = {}, &block)
  from(content, options.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)

Parameters:

  • uri (URI, String)

    URI of the endpoint or file path

  • options (Hash) (defaults to: {})

    (see #from)

Yield Parameters:



114
115
116
117
118
119
120
# File 'lib/redlander/parsing.rb', line 114

def from_uri(uri, options = {}, &block)
  if uri.is_a?(String)
    uri = URI.parse(uri)
    uri = URI.parse("file://#{File.expand_path(uri.to_s)}") if uri.scheme.nil?
  end
  from(uri, options, &block)
end