Class: RDF::RDFXML::Reader
- Inherits:
-
RDF::Reader
- Object
- RDF::Reader
- RDF::RDFXML::Reader
- 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
-
#close ⇒ Object
Document closed when read in initialize.
-
#each_statement {|statement| ... }
Iterates the given block for each RDF statement in the input.
-
#each_triple {|subject, predicate, object| ... }
Iterates the given block for each RDF triple in the input.
-
#initialize(input = $stdin, options = {}) {|reader| ... } ⇒ reader
constructor
Initializes the RDF/XML reader instance.
-
#rewind ⇒ Object
No need to rewind, as parsing is done in initialize.
Constructor Details
#initialize(input = $stdin, options = {}) {|reader| ... } ⇒ reader
Initializes the RDF/XML reader instance.
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, = {}, &block) super do @debug = [:debug] @base_uri = uri([:base_uri]) if [: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
#close ⇒ Object
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.
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.
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 |
#rewind ⇒ Object
No need to rewind, as parsing is done in initialize
148 |
# File 'lib/rdf/rdfxml/reader.rb', line 148 def rewind; end |