lib/rdf/rdfa/reader.rb in rdf-rdfa-1.99.3 vs lib/rdf/rdfa/reader.rb in rdf-rdfa-2.0.0.beta1
- old
+ new
@@ -1,8 +1,8 @@
begin
require 'nokogiri'
-rescue LoadError => e
+rescue LoadError
:rexml
end
require 'rdf/ntriples'
require 'rdf/xsd'
@@ -26,10 +26,11 @@
#
# @author [Gregg Kellogg](http://kellogg-assoc.com/)
class Reader < RDF::Reader
format Format
include Expansion
+ include RDF::Util::Logger
XHTML = "http://www.w3.org/1999/xhtml"
# Content model for @about and @resource. In RDFa 1.0, this was URIorSafeCURIE
SafeCURIEorCURIEorIRI = {
@@ -97,20 +98,10 @@
#
# @!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.
@@ -251,10 +242,35 @@
v.join(", ")
end
end
##
+ # RDFa Reader options
+ # @see http://www.rubydoc.info/github/ruby-rdf/rdf/RDF/Reader#options-class_method
+ def self.options
+ super + [
+ RDF::CLI::Option.new(
+ symbol: :vocab_expansion,
+ datatype: TrueClass,
+ on: ["--vocab-expansion"],
+ description: "Perform OWL2 expansion on the resulting graph.") {true},
+ RDF::CLI::Option.new(
+ symbol: :host_language,
+ datatype: %w(xml xhtml1 xhtml5 html4 svg),
+ on: ["--host-language HOSTLANG", %w(xml xhtml1 xhtml5 html4 svg)],
+ description: "Host Language. One of xml, xhtml1, xhtml5, html4, or svg") do |arg|
+ arg.to_sym
+ end,
+ RDF::CLI::Option.new(
+ symbol: :rdfagraph,
+ datatype: %w(output processor both),
+ on: ["--rdfagraph RDFAGRAPH", %w(output processor both)],
+ description: "Used to indicate if either or both of the :output or :processor graphs are output.") {|arg| arg.to_sym},
+ ]
+ end
+
+ ##
# Initializes the RDFa reader instance.
#
# @param [IO, File, String] input
# the input stream to read
# @param [Hash{Symbol => Object}] options
@@ -274,30 +290,22 @@
# @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
- @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 'all' then [:output, :processor]
when String, Symbol then @options[:rdfagraph].to_s.split(',').map(&:strip).map(&:to_sym)
when Array then @options[:rdfagraph].map {|o| o.to_s.to_sym}
else []
end.select {|o| [:output, :processor].include?(o)}
@options[:rdfagraph] << :output if @options[:rdfagraph].empty?
@@ -394,13 +402,17 @@
rescue LoadError
end
if reader = RDF::Reader.for(content_type: type.to_s)
add_debug(el, "=> reader #{reader.to_sym}")
- # Wrap input in a RemoteDocument with appropriate content-type
+ # Wrap input in a RemoteDocument with appropriate content-type and base
doc = if input.is_a?(String)
- RDF::Util::File::RemoteDocument.new(input, options.merge(content_type: type.to_s))
+ RDF::Util::File::RemoteDocument.new(input,
+ options.merge(
+ content_type: type.to_s,
+ base_uri: base_uri
+ ))
else
input
end
reader.new(doc, options).each(&block)
else
@@ -455,10 +467,14 @@
yield statement if @options[:rdfagraph].include?(:output)
when RDF::RDFA.ProcessorGraph
yield RDF::Statement.new(*statement.to_triple) if @options[:rdfagraph].include?(:processor)
end
end
+
+ if validate? && log_statistics[:error]
+ raise RDF::ReaderError, "Errors found during processing"
+ end
end
enum_for(:each_statement)
end
##
@@ -494,34 +510,34 @@
# Add debug event to debug array, if specified
#
# @param [#display_path, #to_s] node XML Node or string for showing context
# @param [String] message
# @yieldreturn [String] appended to message, to allow for lazy-evaulation of message
- def add_debug(node, message = "")
- return unless ::RDF::RDFa.debug? || @debug
- message = message + yield if block_given?
- add_processor_message(node, message, RDF::RDFA.Info)
+ def add_debug(node, message = "", &block)
+ add_processor_message(node, message, nil, &block)
end
- def add_info(node, message, process_class = RDF::RDFA.Info)
- add_processor_message(node, message, process_class)
+ def add_info(node, message, process_class = RDF::RDFA.Info, &block)
+ add_processor_message(node, message, process_class, &block)
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)
- puts "#{node_path(node)}: #{message}" if ::RDF::RDFa.debug?
- @debug << "#{node_path(node)}: #{message}" if @debug.is_a?(Array)
+ def add_processor_message(node, message, process_class, &block)
+ case process_class
+ when RDF::RDFA.Error then log_error(node_path(node), message, &block)
+ when RDF::RDFA.Warning then log_warn(node_path(node), message, &block)
+ when RDF::RDFA.Info then log_info(node_path(node), message, &block)
+ else log_debug(node_path(node), message, &block)
+ end
+ process_class ||= RDF::RDFA.Info
if @options[:processor_callback] || @options[:rdfagraph].include?(:processor)
n = RDF::Node.new
processor_statements = [
RDF::Statement.new(n, RDF["type"], process_class, graph_name: RDF::RDFA.ProcessorGraph),
RDF::Statement.new(n, RDF::URI("http://purl.org/dc/terms/description"), message, graph_name: RDF::RDFA.ProcessorGraph),
@@ -603,29 +619,29 @@
if base_uri == uri
add_debug(root) {"load_initial_contexts: skip recursive context #{uri.to_base}"}
next
end
- old_debug = RDF::RDFa.debug?
+ old_logger = @options[:logger]
begin
add_info(root, "load_initial_contexts: load #{uri.to_base}")
- RDF::RDFa.debug = false
+ @options[:logger] = false
context = Context.find(uri)
# Add URI Mappings to prefixes
context.prefixes.each_pair do |prefix, value|
prefix(prefix, value)
end
yield :uri_mappings, context.prefixes unless context.prefixes.empty?
yield :term_mappings, context.terms unless context.terms.empty?
yield :default_vocabulary, context.vocabulary if context.vocabulary
rescue Exception => e
- RDF::RDFa.debug = old_debug
+ options[:logger] = old_logger
add_error(root, e.message)
raise # In case we're not in strict mode, we need to be sure processing stops
ensure
- RDF::RDFa.debug = old_debug
+ @options[:logger] = old_logger
end
end
end
# Extract the prefix mappings from an element
@@ -1172,10 +1188,10 @@
# Lexically scan value and assign appropriate type, otherwise, leave untyped
# See https://www.w3.org/2001/sw/wiki/RDFa_1.1._Errata#Using_.3Cdata.3E.2C_.3Cinput.3E_and_.3Cli.3E_along_with_.40value
add_debug(element, "[Step 11] value literal (#{attrs[:value]})")
v = attrs[:value].to_s
# Figure it out by parsing
- dt_lit = %w(Integer Float Double).map {|t| RDF::Literal.const_get(t)}.detect do |dt|
+ dt_lit = %w(Integer Decimal Double).map {|t| RDF::Literal.const_get(t)}.detect do |dt|
v.match(dt::GRAMMAR)
end || RDF::Literal
dt_lit.new(v)
when attrs[:datatype]
# otherwise, as a plain literal if @datatype is present but has an empty value.