Class: Checker

Inherits:
Object
  • Object
show all
Defined in:
app/models/checker.rb

Overview

Schematron-based XSLT checker that finds and reports errors in EAD files

Instance Method Summary (collapse)

Constructor Details

- (Checker) initialize(stron = ->() {Schematron.current}, run = nil)

Returns a new instance of Checker



3
4
5
6
7
8
# File 'app/models/checker.rb', line 3

def initialize(stron = ->() {Schematron.current}, run = nil)
  @schematron = stron.kind_of?(Proc) ? stron.call : stron
  @issue_ids = stron.issues.pluck(:identifier, :id).to_h
  @checker = Schematronium.new(@schematron.file)
  @run = run
end

Instance Method Details

- (Array) check(faid)

Returns Issues found, elements of array are suitable for passing to ConcreteIssues constructor

Parameters:

Returns:

  • (Array)

    Issues found, elements of array are suitable for passing to ConcreteIssues constructor



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/checker.rb', line 12

def check(faid)
  # Resolve down to concrete FindingAidFile for passing to Schematronium
  faid = faid.current if faid.is_a? FindingAid

  s_xml = Saxon.XML(faid.file)
  xml = @checker.check(faid.file)
  xml.remove_namespaces!
  errs = xml.xpath('//failed-assert | //successful-report')


  errs.map do |el|
    diag = el.at_xpath('./diagnostic-reference')
    {
      run_id: @run.try(:id),
      finding_aid_version_id: faid.id,
      issue_id: @issue_ids[diag['diagnostic']],
      location: el['location'],
      line_number: s_xml.xpath(el['location']).get_line_number,
      diagnostic_info: diag.inner_html
    }
  end
end

- (Array<Hash>) check_str(xmlstr)

Note: Separate str version exists because saxon XML can't provide line numbers when run on a str not backed by a file

Parameters:

  • xmlstr (String)

    An input string containing EAD content to be checked via Schematron

Returns:

  • (Array<Hash>)

    Issues found, elements of array are suitable for passing to ConcreteIssues constructor



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/checker.rb', line 38

def check_str(xmlstr)
  xml = @checker.check(xmlstr)
  xml.remove_namespaces!
  errs = xml.xpath('//failed-assert | //successful-report')

  errs.map do |el|
    diag = el.at_xpath('./diagnostic-reference')
    {
      run_id: @run.try(:id),
      issue_id: @issue_ids[diag['diagnostic']],
      location: el['location'],
      line_number: -1,
      diagnostic_info: diag.inner_html
    }
  end
end