lib/rdf/json/reader.rb in rdf-json-1.99.0 vs lib/rdf/json/reader.rb in rdf-json-2.0.0.beta1

- old
+ new

@@ -27,10 +27,11 @@ # end # end # # @see http://n2.talis.com/wiki/RDF_JSON_Specification class Reader < RDF::Reader + include RDF::Util::Logger format RDF::JSON::Format ## # The graph constructed when parsing. # @@ -46,24 +47,10 @@ # @yield [reader] `self` # @yieldparam [RDF::Reader] reader # @yieldreturn [void] ignored def initialize(input = $stdin, options = {}, &block) super do - @graph = RDF::Graph.new - - @input.rewind - ::JSON.parse(@input.read).each do |subject, predicates| - subject = parse_subject(subject) - predicates.each do |predicate, objects| - predicate = parse_predicate(predicate) - objects.each do |object| - object = parse_object(object) - @graph << [subject, predicate, object] - end - end - end - if block_given? case block.arity when 0 then instance_eval(&block) else block.call(self) end @@ -97,12 +84,12 @@ # Parses an RDF/JSON object string into an RDF value. # # @param [Hash{String => Object}] object # @return [RDF::Value] def parse_object(object) - raise RDF::ReaderError, "missing 'type' key in #{object.inspect}" unless object.has_key?('type') - raise RDF::ReaderError, "missing 'value' key in #{object.inspect}" unless object.has_key?('value') + log_error("missing 'type' key in #{object.inspect}", exception: RDF::ReaderError) unless object.has_key?('type') + log_error("missing 'value' key in #{object.inspect}", exception: RDF::ReaderError) unless object.has_key?('value') case type = object['type'] when 'bnode' parse_node(object['value']) when 'uri' @@ -114,22 +101,26 @@ }) literal.validate! if validate? literal.canonicalize! if canonicalize? literal else - raise RDF::ReaderError, "expected 'type' to be 'bnode', 'uri', or 'literal', but got #{type.inspect}" + log_error("expected 'type' to be 'bnode', 'uri', or 'literal', but got #{type.inspect}", exception: RDF::ReaderError) end + rescue RDF::ReaderError + nil end ## # Parses an RDF/JSON blank node string into an `RDF::Node` instance. # # @param [String] string # @return [RDF::Node] # @since 0.3.0 def parse_node(string) - RDF::Node.new(string[2..-1]) # strips off the initial '_:' + @nodes ||= {} + id = string[2..-1] # strips off the initial '_:' + @nodes[id.to_sym] ||= RDF::Node.new(id) end alias_method :parse_bnode, :parse_node ## # Parses an RDF/JSON URI string into an `RDF::URI` instance. @@ -146,35 +137,43 @@ uri end ## # @private - # @see RDF::Reader#each_graph - # @since 0.2.0 - def each_graph(&block) - if block_given? - block.call(@graph) - end - enum_graph - end - - ## - # @private # @see RDF::Reader#each_statement def each_statement(&block) if block_given? - @graph.each_statement(&block) + @input.rewind rescue nil + begin + ::JSON.parse(@input.read).each do |subject, predicates| + subject = parse_subject(subject) + predicates.each do |predicate, objects| + predicate = parse_predicate(predicate) + objects.each do |object| + object = parse_object(object) + yield RDF::Statement(subject, predicate, object) if object + end + end + end + rescue ::JSON::ParserError => e + log_error(e.message) + end + if validate? && log_statistics[:error] + raise RDF::ReaderError, "Errors found during processing" + end end - enum_statement + enum_for(:each_statement) end ## # @private # @see RDF::Reader#each_triple def each_triple(&block) if block_given? - @graph.each_triple(&block) + each_statement do |statement| + block.call(*statement.to_triple) + end end - enum_triple + enum_for(:each_triple) end end # Reader end # RDF::JSON