Class: RDF::RDFXML::Reader
- Inherits:
-
RDF::Reader
- Object
- RDF::Reader
- RDF::RDFXML::Reader
- Includes:
- Util::Logger
- Defined in:
- lib/rdf/rdfxml/reader.rb,
lib/rdf/rdfxml/reader/rexml.rb,
lib/rdf/rdfxml/reader/nokogiri.rb
Overview
An RDF/XML parser in Ruby
Based on RDF/XML Syntax Specification: www.w3.org/TR/REC-rdf-syntax/
Extension: A nodeElement can also use the rdf:resource attribute, if none of the other standard attributes are defined.
Defined Under Namespace
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 collapse
-
#implementation ⇒ Module
readonly
Returns the XML implementation module for this reader instance.
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.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/rdf/rdfxml/reader.rb', line 141 def initialize(input = $stdin, **, &block) super do @library = case [:library] when nil # Use Nokogiri when available, and REXML otherwise: defined?(::Nokogiri) ? :nokogiri : :rexml when :nokogiri, :rexml [:library] else log_fatal("expected :rexml or :nokogiri, but got #{[:library].inspect}", exception: ArgumentError) end require "rdf/rdfxml/reader/#{@library}" @implementation = case @library when :nokogiri then Nokogiri when :rexml then REXML end self.extend(@implementation) input.rewind if input.respond_to?(:rewind) initialize_xml(input, **) rescue log_fatal($!.) if root.nil? log_info("Empty document") elsif !doc_errors.empty? log_error("Synax errors") {doc_errors} end block.call(self) if block_given? end end |
Instance Attribute Details
#implementation ⇒ Module (readonly)
Returns the XML implementation module for this reader instance.
113 114 115 |
# File 'lib/rdf/rdfxml/reader.rb', line 113 def implementation @implementation end |
Instance Method Details
#close ⇒ Object
Document closed when read in initialize
177 |
# File 'lib/rdf/rdfxml/reader.rb', line 177 def close; end |
#each_statement {|statement| ... }
This method returns an undefined value.
Iterates the given block for each RDF statement in the input.
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/rdf/rdfxml/reader.rb', line 185 def each_statement(&block) if block_given? # Block called from add_statement @callback = block return unless root log_fatal "root must be a proxy not a #{root.class}" unless root.is_a?(@implementation::NodeProxy) add_debug(root, "base_uri: #{base_uri.inspect}") rdf_nodes = root.xpath("//rdf:RDF", "rdf" => RDF.to_uri.to_s) if rdf_nodes.size == 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| log_fatal "node must be a proxy not a #{node.class}" unless node.is_a?(@implementation::NodeProxy) # 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) ec.extract_from_ancestors(node) do |prefix, value| prefix(prefix, value) end node.children.each {|el| next unless el.element? log_fatal "el must be a proxy not a #{el.class}" unless el.is_a?(@implementation::NodeProxy) new_ec = ec.clone(el) do |prefix, value| prefix(prefix, value) end nodeElement(el, new_ec) } end end if validate? && log_statistics[:error] raise RDF::ReaderError, "Errors found during processing" end end enum_for(:each_statement) end |
#each_triple {|subject, predicate, object| ... }
This method returns an undefined value.
Iterates the given block for each RDF triple in the input.
240 241 242 243 244 245 246 247 |
# File 'lib/rdf/rdfxml/reader.rb', line 240 def each_triple(&block) if block_given? each_statement do |statement| block.call(*statement.to_triple) end end enum_for(:each_triple) end |
#rewind ⇒ Object
No need to rewind, as parsing is done in initialize
174 |
# File 'lib/rdf/rdfxml/reader.rb', line 174 def rewind; end |