Class: RdfContext::RdfXmlParser

Inherits:
Parser
  • Object
show all
Defined in:
lib/rdf_context/rdfxmlparser.rb

Constant Summary

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 Attribute Summary

Attributes inherited from Parser

#debug, #doc, #graph, #processor_graph, #uri

Instance Method Summary collapse

Methods inherited from Parser

#add_debug, #add_error, #add_info, #add_processor_message, #add_triple, #add_warning, #detect_format, #initialize, n3_parser, #node_path, parse, rdfa_parser, rdfxml_parser

Constructor Details

This class inherits a constructor from RdfContext::Parser

Instance Method Details

#parse(stream, uri = nil, options = {}) {|triple| ... } ⇒ Graph

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

If the parser is called with a block, triples are passed to the block rather than added to the graph.

Optionally, the stream may be a string or Nokogiri::XML::Document

Options Hash (options):

  • :graph (Graph) — default: Graph.new

    Graph to parse into, otherwise a new Graph

  • :debug (Array) — default: nil

    Array to place debug messages

  • :strict (Boolean) — default: false

    Raise Error if true, continue with lax parsing, otherwise

Yields:

  • (triple)

Yield Parameters:

Raises:

  • (Error)

    Raises RdfError if strict



150
151
152
153
154
155
156
157
158
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
# File 'lib/rdf_context/rdfxmlparser.rb', line 150

def parse(stream, uri = nil, options = {}, &block) # :yields: triple
  super

  @doc = case stream
  when Nokogiri::XML::Document then stream
  else   Nokogiri::XML.parse(stream, uri.to_s)
  end
  
  raise ParserException, "Synax errors:\n#{@doc.errors}" unless @doc.errors.empty?
  
  @id_mapping = Hash.new

  raise ParserException, "Empty document" if @doc.nil? || @doc.root.nil?
  @callback = block
  
  root = @doc.root
  
  # Look for rdf:RDF elements and process each.
  rdf_nodes = root.xpath("//rdf:RDF", RDF_NS.prefix => RDF_NS.uri.to_s)
  if rdf_nodes.length == 0
    # If none found, root element may be processed as an RDF Node

    ec = EvaluationContext.new(@uri, root, @graph)
    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(@uri, nil, @graph)
      ec.extract_from_ancestors(node)
      node.children.each {|el|
        next unless el.elem?
        new_ec = ec.clone(el)
        nodeElement(el, new_ec)
      }
    end
  end

  @graph
end