Class: RDF::RDFXML::Reader

Inherits:
RDF::Reader
  • Object
show all
Defined in:
lib/rdf/rdfxml/reader.rb

Overview

An RDF/XML parser in Ruby

Based on RDF/XML Syntax Specification: www.w3.org/TR/REC-rdf-syntax/

Defined Under Namespace

Classes: EvaluationContext

Constant Summary collapse

CORE_SYNTAX_TERMS =
%w(RDF ID about parseType resource nodeID datatype).map {|n| "http://www.w3.org/1999/02/22-rdf-syntax-ns##{n}"}
OLD_TERMS =
%w(aboutEach aboutEachPrefix bagID).map {|n| "http://www.w3.org/1999/02/22-rdf-syntax-ns##{n}"}

Instance Method Summary collapse

Constructor Details

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

Initializes the RDF/XML reader instance.

Parameters:

  • input (Nokogiri::XML::Document, IO, File, String) (defaults to: $stdin)

    the input stream to read

  • options (Hash{Symbol => Object}) (defaults to: {})

    any additional options

Options Hash (options):

  • :encoding (Encoding) — default: Encoding::UTF_8

    the encoding of the input stream (Ruby 1.9+)

  • :validate (Boolean) — default: false

    whether to validate the parsed statements and values

  • :canonicalize (Boolean) — default: false

    whether to canonicalize parsed literals

  • :intern (Boolean) — default: true

    whether to intern all parsed URIs

  • :prefixes (Hash) — default: Hash.new

    the prefix mappings to use (not supported by all readers)

  • :base_uri (#to_s) — default: nil

    the base URI to use when resolving relative URIs

  • :debug (Array)

    Array to place debug messages

Yields:

  • (reader)

    ‘self`

Yield Parameters:

  • reader (RDF::Reader)

Yield Returns:

  • (void)

    ignored

Raises:

  • (Error)

    Raises RDF::ReaderError if validate



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/rdf/rdfxml/reader.rb', line 130

def initialize(input = $stdin, options = {}, &block)
  super do
    @debug = options[:debug]
    @base_uri = uri(options[:base_uri]) if options[:base_uri]
        
    @doc = case input
    when Nokogiri::XML::Document then input
    else Nokogiri::XML.parse(input, @base_uri.to_s)
    end
    
    raise RDF::ReaderError, "Synax errors:\n#{@doc.errors}" if !@doc.errors.empty? && validate?
    raise RDF::ReaderError, "Empty document" if (@doc.nil? || @doc.root.nil?) && validate?

    block.call(self) if block_given?
  end
end

Instance Method Details

#closeObject

Document closed when read in initialize



151
# File 'lib/rdf/rdfxml/reader.rb', line 151

def close; end

#each_statement {|statement| ... }

This method returns an undefined value.

Iterates the given block for each RDF statement in the input.

Yields:

  • (statement)

Yield Parameters:

  • statement (RDF::Statement)


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/rdf/rdfxml/reader.rb', line 159

def each_statement(&block)
  # Block called from add_statement
  @callback = block

  root = @doc.root

  add_debug(root, "base_uri: #{@base_uri || 'nil'}")
  
  rdf_nodes = root.xpath("//rdf:RDF", "rdf" => RDF.to_uri.to_s)
  if rdf_nodes.length == 0
    # If none found, root element may be processed as an RDF Node

    ec = EvaluationContext.new(@base_uri, root, @graph) do |prefix, value|
      prefix(prefix, value)
    end
    
    nodeElement(root, ec)
  else
    rdf_nodes.each do |node|
      # XXX Skip this element if it's contained within another rdf:RDF element

      # Extract base, lang and namespaces from parents to create proper evaluation context
      ec = EvaluationContext.new(@base_uri, nil, @graph) do |prefix, value|
        prefix(prefix, value)
      end
      ec.extract_from_ancestors(node)
      node.children.each {|el|
        next unless el.elem?
        new_ec = ec.clone(el) do |prefix, value|
          prefix(prefix, value)
        end
        nodeElement(el, new_ec)
      }
    end
  end
end

#each_triple {|subject, predicate, object| ... }

This method returns an undefined value.

Iterates the given block for each RDF triple in the input.

Yields:

  • (subject, predicate, object)

Yield Parameters:

  • subject (RDF::Resource)
  • predicate (RDF::URI)
  • object (RDF::Value)


204
205
206
207
208
# File 'lib/rdf/rdfxml/reader.rb', line 204

def each_triple(&block)
  each_statement do |statement|
    block.call(*statement.to_triple)
  end
end

#rewindObject

No need to rewind, as parsing is done in initialize



148
# File 'lib/rdf/rdfxml/reader.rb', line 148

def rewind; end