lib/rdf/rdfa/reader.rb in rdf-rdfa-1.1.6 vs lib/rdf/rdfa/reader.rb in rdf-rdfa-1.1.6.1

- old
+ new

@@ -97,10 +97,20 @@ # # @!attribute [rw] implementation # @return [Module] attr_reader :implementation + ## + # Warnings found during processing + # @return [Array<String>] + attr_reader :warnings + + ## + # Accumulated errors found during processing + # @return [Array<String>] + attr_reader :errors + # The Recursive Baggage # @private class EvaluationContext # :nodoc: ## # The base. @@ -264,20 +274,26 @@ # @option options [Array<Symbol>] :rdfagraph ([:output]) # Used to indicate if either or both of the :output or :processor graphs are output. # Value is an array containing on or both of :output or :processor. # @option options [Repository] :vocab_repository (nil) # Repository to save loaded vocabularies. + # @option options [Array] :errors + # array for placing errors found when parsing + # @option options [Array] :warnings + # array for placing warnings found when parsing # @option options [Array] :debug # Array to place debug messages # @return [reader] # @yield [reader] `self` # @yieldparam [RDF::Reader] reader # @yieldreturn [void] ignored # @raise [RDF::ReaderError] if _validate_ def initialize(input = $stdin, options = {}, &block) super do - @debug = options[:debug] + @errors = @options[:errors] + @warnings = @options[:warnings] + @debug = @options[:debug] @options = {:reference_folding => true}.merge(@options) @repository = RDF::Repository.new @options[:rdfagraph] = case @options[:rdfagraph] when String, Symbol then @options[:rdfagraph].to_s.split(',').map(&:strip).map(&:to_sym) @@ -311,11 +327,11 @@ initialize_xml(input, options) rescue add_error(nil, "Malformed document: #{$!.message}") end add_error(nil, "Empty document") if root.nil? - add_error(nil, "Syntax errors:\n#{doc_errors}") if !doc_errors.empty? + add_error(nil, doc_errors.map(&:message).uniq.join("\n")) if !doc_errors.empty? # Section 4.2 RDFa Host Language Conformance # # The Host Language may require the automatic inclusion of one or more Initial Contexts @host_defaults = { @@ -367,27 +383,34 @@ def extract_script(el, input, type, options, &block) add_debug(el, "script element of type #{type}") begin # Formats don't exist unless they've been required case type.to_s - when 'application/rdf+xml' then require 'rdf/rdfxml' - when 'text/ntriples' then require 'rdf/ntriples' - when 'text/turtle' then require 'rdf/turtle' - when 'application/ld+json' then require 'json/ld' + when 'application/csvm+json' then require 'rdf/tabular' + when 'application/ld+json' then require 'json/ld' + when 'application/rdf+xml' then require 'rdf/rdfxml' + when 'text/ntriples' then require 'rdf/ntriples' + when 'text/turtle' then require 'rdf/turtle' end rescue LoadError end - if reader = RDF::Reader.for(:content_type => type) + if reader = RDF::Reader.for(:content_type => type.to_s) add_debug(el, "=> reader #{reader.to_sym}") - reader.new(input, options).each(&block) + # Wrap input in a RemoteDocument with appropriate content-type + doc = if input.is_a?(String) + RDF::Util::File::RemoteDocument.new(input, options.merge(content_type: type.to_s)) + else + input + end + reader.new(doc, options).each(&block) else add_debug(el, "=> no reader found") end end - # Look for Embedded Turtle and RDF/XML + # Look for Embedded RDF/XML unless @root.xpath("//rdf:RDF", "rdf" => "http://www.w3.org/1999/02/22-rdf-syntax-ns#").empty? extract_script(@root, @doc, "application/rdf+xml", @options) do |statement| @repository << statement end end @@ -482,14 +505,16 @@ def add_info(node, message, process_class = RDF::RDFA.Info) add_processor_message(node, message, process_class) end def add_warning(node, message, process_class = RDF::RDFA.Warning) + @warnings << "#{node_path(node)}: #{message}" if @warnings add_processor_message(node, message, process_class) end def add_error(node, message, process_class = RDF::RDFA.Error) + @errors << "#{node_path(node)}: #{message}" if @errors add_processor_message(node, message, process_class) raise RDF::ReaderError, message if validate? end def add_processor_message(node, message, process_class) @@ -1456,11 +1481,18 @@ end end end def uri(value, append = nil) - value = RDF::URI.new(value) - value = value.join(append) if append + append = RDF::URI(append) + value = RDF::URI(value) + value = if append.absolute? + value = append + elsif append + value = value.join(append) + else + value + end value.validate! if validate? value.canonicalize! if canonicalize? value = RDF::URI.intern(value) if intern? value rescue ArgumentError => e