Class: OJXV::JatsFile

Inherits:
Object
  • Object
show all
Defined in:
lib/ojxv/jats_file.rb

Overview

VALIDATING JATS against DTD DTD schemas from jats.nlm.nih.gov/archiving/1.3/dtd.html

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ JatsFile

Returns a new instance of JatsFile.



13
14
15
16
17
# File 'lib/ojxv/jats_file.rb', line 13

def initialize(path)
  @errors = nil
  raise ::OJXV::FileNotFound, "Can't find file: #{path}" unless File.exist?(path)
  @filepath = path
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



10
11
12
# File 'lib/ojxv/jats_file.rb', line 10

def errors
  @errors
end

#filepathObject (readonly)

Returns the value of attribute filepath.



11
12
13
# File 'lib/ojxv/jats_file.rb', line 11

def filepath
  @filepath
end

Class Method Details

.supported_schema_versionsObject



43
44
45
# File 'lib/ojxv/jats_file.rb', line 43

def self.supported_schema_versions
  ["1.1", "1.1d1", "1.1d2", "1.1d3", "1.2", "1.2d1", "1.2d2", "1.3d1", "1.3d2", "1.3"]
end

Instance Method Details

#valid_jats?(schema_version = "1.3") ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ojxv/jats_file.rb', line 19

def valid_jats?(schema_version="1.3")
  @errors = nil
  FileUtils.mkdir_p(local_path)

  FileUtils.cp_r schema_files_path(schema_version), local_path
  FileUtils.cp @filepath, local_path

  new_filepath = File.join(local_path, File.basename(@filepath))

  doc = Nokogiri::XML(File.open(new_filepath)) do |config|
    config.dtdload.dtdvalid
  end

  raise(NoDTD, "File has no DTD declaration") if doc.external_subset.nil?

  @errors = doc.external_subset.validate(doc)
  @errors.empty?

rescue Nokogiri::XML::SyntaxError => e
  raise ::OJXV::XMLParsingError, e.message
ensure
  cleanup
end