Class: Reddy::RdfXmlParser

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

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

Attributes inherited from Parser

#debug, #doc, #graph

Instance Method Summary collapse

Methods inherited from Parser

#initialize, n3_parser, parse, rdfa_parser, rdfxml_parser

Constructor Details

This class inherits a constructor from Reddy::Parser

Instance Method Details

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

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

Optionally, the stream may be a string or Nokogiri::XML::Document With a block, yeilds each statement with URIRef, BNode or Literal elements

options[:debug]

Array to place debug messages

options[:strict]

Raise Error if true, continue with lax parsing, otherwise

Parameters:

  • stream:: (IO)

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

  • uri:: (String)

    the URI of the document

  • options:: (Hash)

    Parser options, one of

Returns:

  • (Graph)

    Returns the graph containing parsed triples

Raises:

  • (Error)

    Raises RdfError if strict

Author:

  • Gregg Kellogg



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/reddy/rdfxmlparser.rb', line 113

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)
  end
  
  @id_mapping = Hash.new

  raise ParserException, "Empty document" if @doc.nil? && @strict
  @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