Class: Kiba::Extend::Sources::XmlDir

Inherits:
Object
  • Object
show all
Extended by:
Sourceable
Includes:
Enumerable
Defined in:
lib/kiba/extend/sources/xml_dir.rb

Overview

XmlDir is a source yielding one Nokogiri::XML::Document per XML file. It will issue warnings (on stdout) for any source file that fails to parse as well-formed XML. This behavior can be suppressed by passing silent_warnings: true to the constructor. Regardless of whether the warnings are suppressed, XmlDir will not yield an XMLDocument for malformed XML.

Other things to be aware of: stripping namespaces might make your life easier for downstream transformations; you can pass remove_namespaces: true to the constructor for this purpose (@see #initialize for details and other options).

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Sourceable

is_source?

Methods included from Registry::Fileable

#default_args, #default_file_options, #labeled_options, #options_key, #path_key, #requires_path?

Constructor Details

#initialize(dirpath:, recursive: false, filesuffixes: [".xml"], silent_warnings: false, parseopts: Kiba::Extend.xmlopts, remove_namespaces: false) ⇒ XmlDir

Returns a new instance of XmlDir.

Parameters:

  • dirpath (String)

    path to a directory containing XML files (one valid XML document per file).

  • recursive (Boolean) (defaults to: false)

    whether to load all subdirectories of the specified dirpath.

  • filesuffixes (Array<String>) (defaults to: [".xml"])

    Load only files with the specified suffix(es).

  • silent_warnings (Boolean) (defaults to: false)

    Whether to suppress warnings for invalid XML. Invalid XML files are skipped (no document yielded) so that callers can depend on valid Nokogiri::XML::Documents from the XmlDir source. Everything in the source directory should be validated before kiba-extend sees it, but we can warn about errors if needed.

  • parseopts (Integer, Nokogiri::XML::ParseOptions) (defaults to: Kiba::Extend.xmlopts)

    passed to Nokogiri::XML::Document.parse as the options argument. Defaults to Kiba::Extend.xmlopts. NOTE that invalid source XML will halt the whole pipeline when encountered unless RECOVER is in xmlopts! (It is by default, but it should always be included when callers override @parseopts.)

  • remove_namespaces (Boolean) (defaults to: false)

    whether to call remove_namespaces! on each parsed Document before yielding it. This strips all namespace prefixes/declarations, which simplifies queries but may remove distinguishing features from different elements with identical names.



64
65
66
67
68
69
70
71
72
73
# File 'lib/kiba/extend/sources/xml_dir.rb', line 64

def initialize(dirpath:, recursive: false, filesuffixes: [".xml"],
  silent_warnings: false, parseopts: Kiba::Extend.xmlopts,
  remove_namespaces: false)
  @path = File.expand_path(dirpath)
  @recursive = recursive
  @filesuffixes = filesuffixes
  @silent_warnings = silent_warnings
  @parseopts = parseopts
  @remove_namespaces = remove_namespaces
end

Class Method Details

.default_file_optionsObject



24
25
26
# File 'lib/kiba/extend/sources/xml_dir.rb', line 24

def default_file_options
  Kiba::Extend.xmlopts
end

.options_keyObject



28
29
30
# File 'lib/kiba/extend/sources/xml_dir.rb', line 28

def options_key
  nil
end

.path_keyObject



32
33
34
# File 'lib/kiba/extend/sources/xml_dir.rb', line 32

def path_key
  :dirpath
end

.requires_path?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/kiba/extend/sources/xml_dir.rb', line 36

def requires_path?
  true
end

Instance Method Details

#eachObject

Yield Returns:

  • (Nokogiri::XML::Document)

    per successfully parsed file



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/kiba/extend/sources/xml_dir.rb', line 76

def each
  file_list.each do |filepath|
    doc = parse(filepath)
    next unless doc

    if doc.errors.any?
      warn_invalid(filepath, doc.errors)
      next
    end
    doc.remove_namespaces! if remove_namespaces
    yield doc
  end
end