Class: Reddy::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/reddy/parser.rb,
lib/reddy/n3parser.rb

Overview

Generic Reddy Parser class

Direct Known Subclasses

N3Parser, RdfXmlParser, RdfaParser

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Parser

Creates a new parser for N3 (or Turtle).

options[:graph]

Graph to parse into, otherwise a new Reddy::Graph instance is created

options[:debug]

Array to place debug messages

options[:type]

One of rdfxml, html, or n3

options[:strict]

Raise Error if true, continue with lax parsing, otherwise

Parameters:

  • options:: (Hash)

    Options from

Author:

  • Gregg Kellogg



17
18
19
20
21
22
23
# File 'lib/reddy/parser.rb', line 17

def initialize(options = {})
  # initialize the triplestore
  @graph = options[:graph]
  @debug = options[:debug]
  @strict = options[:strict]
  @named_bnodes = {}
end

Instance Attribute Details

#debugObject (readonly)

Returns the value of attribute debug.



4
5
6
# File 'lib/reddy/parser.rb', line 4

def debug
  @debug
end

#docObject

Returns the value of attribute doc.



5
6
7
# File 'lib/reddy/parser.rb', line 5

def doc
  @doc
end

#graphObject

Returns the value of attribute graph.



5
6
7
# File 'lib/reddy/parser.rb', line 5

def graph
  @graph
end

Class Method Details

.n3_parser(options = {}) ⇒ Object

Return N3 Parser instance



78
# File 'lib/reddy/parser.rb', line 78

def self.n3_parser(options = {}); N3Parser.new(options); end

.parse(stream, uri = nil, options = {}, &block) ⇒ Graph

Instantiate Parser and parse document

options[:debug]

Array to place debug messages

options[:type]

One of rdfxml, html, or n3

options[:strict]

Raise Error if true, continue with lax parsing, otherwise

Parameters:

  • stream:: (IO, String)

    the RDF IO stream, string, Nokogiri::HTML::Document or Nokogiri::XML::Document

  • uri:: (String)

    the URI of the document

  • options:: (Hash)

    Options from

Returns:

  • (Graph)

    Returns the graph containing parsed triples

Raises:

  • (Error)

    Raises RdfError if strict

Author:

  • Gregg Kellogg



37
38
39
40
# File 'lib/reddy/parser.rb', line 37

def self.parse(stream, uri = nil, options = {}, &block) # :yields: triple
  parser = self.new(options)
  parser.parse(stream, uri, options, &block)
end

.rdfa_parser(options = {}) ⇒ Object

Return Rdfa Parser instance



82
# File 'lib/reddy/parser.rb', line 82

def self.rdfa_parser(options = {}); RdfaParser.new(options); end

.rdfxml_parser(options = {}) ⇒ Object

Return RDF/XML Parser instance



80
# File 'lib/reddy/parser.rb', line 80

def self.rdfxml_parser(options = {}); RdfXmlParser.new(options); end

Instance Method Details

#parse(stream, uri = nil, options = {}, &block) ⇒ Graph

Parse RDF document from a string or input stream to closure or graph.

Virtual Class, prototype for Parser subclass.

options[:debug]

Array to place debug messages

options[:strict]

Raise Error if true, continue with lax parsing, otherwise

Parameters:

  • stream:: (IO, String)

    the RDF IO stream, string, Nokogiri::HTML::Document or Nokogiri::XML::Document

  • uri:: (String)

    the URI of the document

  • options:: (Hash)

    Options from

Returns:

  • (Graph)

    Returns the graph containing parsed triples

Raises:

  • (Error)

    Raises RdfError if strict

Author:

  • Gregg Kellogg



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/reddy/parser.rb', line 55

def parse(stream, uri = nil, options = {}, &block) # :yields: triple
  if self.class == Parser
    # Create a delegate of a specific parser class
    @delegate ||= case options[:type].to_s
    when "n3", "ntriples", "turtle" then N3Parser.new(options)
    when "rdfa", "html", "xhtml"    then RdfaParser.new(options)
    when "xml", "rdf", "rdfxml"     then RdfXmlParser.new(options)
    else
      RdfXmlParser.new(options)
      # raise ParserException.new("type option must be one of :rdfxml, :html, or :n3")
    end
    @delegate.parse(stream, uri, options, &block)
  else
    # Common parser operations
    @uri = Addressable::URI.parse(uri.to_s).to_s unless uri.nil?
    @strict = options[:strict] if options.has_key?(:strict)
    @debug = options[:debug] if options.has_key?(:debug)
    
    @graph ||= Graph.new(:identifier => @uri)
  end
end