Class: RDF::TriX::Reader

Inherits:
Reader
  • Object
show all
Defined in:
lib/rdf/trix/reader.rb,
lib/rdf/trix/reader/rexml.rb,
lib/rdf/trix/reader/libxml.rb,
lib/rdf/trix/reader/nokogiri.rb

Overview

TriX parser.

This class supports REXML, LibXML and Nokogiri for XML processing, and will automatically select the most performant implementation (Nokogiri or LibXML) that is available. If need be, you can explicitly override the used implementation by passing in a :library option to Reader.new or Reader.open.

Examples:

Loading TriX parsing support

require 'rdf/trix'

Obtaining a TriX reader class

RDF::Reader.for(:trix)         #=> RDF::TriX::Reader
RDF::Reader.for("etc/doap.xml")
RDF::Reader.for(:file_name      => "etc/doap.xml")
RDF::Reader.for(:file_extension => "xml")
RDF::Reader.for(:content_type   => "application/trix")

Instantiating a Nokogiri-based reader

RDF::TriX::Reader.new(input, :library => :nokogiri)

Instantiating a LibXML-based reader

RDF::TriX::Reader.new(input, :library => :libxml)

Instantiating a REXML-based reader

RDF::TriX::Reader.new(input, :library => :rexml)

Parsing RDF statements from a TriX file

RDF::TriX::Reader.open("etc/doap.xml") do |reader|
  reader.each_statement do |statement|
    puts statement.inspect
  end
end

Parsing RDF statements from a TriX string

data = StringIO.new(File.read("etc/doap.xml"))
RDF::TriX::Reader.new(data) do |reader|
  reader.each_statement do |statement|
    puts statement.inspect
  end
end

See Also:

Defined Under Namespace

Modules: LibXML, Nokogiri, REXML

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input = $stdin, **options) {|reader| ... } ⇒ Reader

Initializes the TriX reader instance.

Parameters:

  • input (IO, File, String) (defaults to: $stdin)
  • options (Hash{Symbol => Object})

    any additional options (see RDF::Reader#initialize)

Options Hash (**options):

  • :library (Symbol) — default: :nokogiri, :libxml, or :rexml
  • :base_uri (#to_s) — default: nil

    the base URI to use when resolving relative URIs

Yields:

  • (reader)

    self

Yield Parameters:

  • reader (RDF::Reader)

Yield Returns:

  • (void)

    ignored



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rdf/trix/reader.rb', line 79

def initialize(input = $stdin, **options, &block)
  super do
    @library = case options[:library]
      when nil
        # Use Nokogiri or LibXML when available, and REXML otherwise:
        begin
          require 'nokogiri'
          :nokogiri
        rescue LoadError => e
          begin
            require 'libxml'
            :libxml
          rescue LoadError => e
            :rexml
          end
        end
      when :nokogiri, :libxml, :rexml
        options[:library]
      else
        raise ArgumentError.new("expected :rexml, :libxml or :nokogiri, but got #{options[:library].inspect}")
    end

    require "rdf/trix/reader/#{@library}"
    @implementation = case @library
      when :nokogiri then Nokogiri
      when :libxml   then LibXML
      when :rexml    then REXML
    end
    self.extend(@implementation)

    begin
      initialize_xml(input, **options)
    rescue
      log_error("Malformed document: #{$!.message}")
    end

    if block_given?
      case block.arity
        when 0 then instance_eval(&block)
        else block.call(self)
      end
    end
  end
end

Instance Attribute Details

#base_uriRDF::URI (readonly)

Returns the Base URI as provided, or found from xml:base

Returns:

  • (RDF::URI)


65
66
67
# File 'lib/rdf/trix/reader.rb', line 65

def base_uri
  @base_uri
end

#implementationModule (readonly)

Returns the XML implementation module for this reader instance.

Returns:

  • (Module)


59
60
61
# File 'lib/rdf/trix/reader.rb', line 59

def implementation
  @implementation
end

Instance Method Details

#parse_element(name, element, content) ⇒ RDF::Value

Returns the RDF value of the given TriX element.

Parameters:

  • name (String)
  • element (Hash{String => Object})
  • content (String)

Returns:

  • (RDF::Value)


222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/rdf/trix/reader.rb', line 222

def parse_element(name, element, content)
  case name.to_sym
    when :id
      RDF::Node.intern(content.strip)
    when :uri
      uri = RDF::URI.new(content.strip) # TODO: interned URIs
      uri = base_uri.join(uri) if base_uri && uri.relative?
      uri.validate!     if validate?
      uri.canonicalize! if canonicalize?
      uri
    when :triple # RDF-star
      log_error "expected 'triple' element" unless @options[:rdfstar]
      read_triple(element)
    when :typedLiteral
      content = element.children.c14nxl(library: @library) if
        element['datatype'] == RDF.XMLLiteral
      literal = RDF::Literal.new(content, :datatype => RDF::URI(element['datatype']))
      literal.validate!     if validate?
      literal.canonicalize! if canonicalize?
      literal
    when :plainLiteral
      literal = case
        when lang = element['xml:lang'] || element['lang']
          RDF::Literal.new(content, :language => lang)
        else
          RDF::Literal.new(content)
      end
      literal.validate!     if validate?
      literal.canonicalize! if canonicalize?
      literal
    else
      log_error "expected element name to be 'id', 'uri', 'triple', 'typedLiteral', or 'plainLiteral', but got #{name.inspect}"
  end
end

#read_statements(graph_element) {|statement| ... } ⇒ Object

Yield each statement from a graph

Parameters:

  • graph_element (Object)

Yields:

  • statement

Yield Parameters:

  • statement (RDF::Statement)


195
196
197
198
199
200
201
202
# File 'lib/rdf/trix/reader.rb', line 195

def read_statements(graph_element, &block)
  graph_name = read_graph(graph_element)
  graph_name = base_uri.join(graph_name) if
    base_uri && graph_name && graph_name.relative?
  triple_elements(graph_element).each do |triple_element|
    block.call(read_triple(triple_element, graph_name: graph_name))
  end
end

#read_triple(element, graph_name: nil) ⇒ RDF::Statement

Read a <triple>

Parameters:

  • element (Hash{String => Object})

Returns:

  • (RDF::Statement)

    statement



208
209
210
211
212
213
# File 'lib/rdf/trix/reader.rb', line 208

def read_triple(element, graph_name: nil)
  terms = element_elements(element)[0..2].map do |element|
    parse_element(element.name, element, element_content(element))
  end
  RDF::Statement(*terms, graph_name: graph_name)
end