Class: Idml::Validation::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/idml/validation/validator.rb

Overview

Validates IDML parts against RelaxNG-Compact schemas using Jing.

Jing is bundled in the InDesign Plug-in SDK at reference-docs/plugin_sdk_21/devtools/idmltools/jing/bin/jing.jar. The gem does not vendor Jing — users point Validator at a local jar via the jing_jar: argument.

Schemas are not vendored either. The default schema_root points at the development tree; users override via schema_root:.

Constant Summary collapse

DEFAULT_JING_JAR =
File.expand_path(
  "../../../../reference-docs/plugin_sdk_21/" \
  "devtools/idmltools/jing/bin/jing.jar",
  __dir__,
)
NON_SCHEMATIZED_PARTS =
%w[
  mimetype
  META-INF/container.xml
  META-INF/metadata.xml
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema_root: Idml::Validation::DEFAULT_SCHEMA_ROOT, jing_jar: DEFAULT_JING_JAR) ⇒ Validator



44
45
46
47
48
# File 'lib/idml/validation/validator.rb', line 44

def initialize(schema_root: Idml::Validation::DEFAULT_SCHEMA_ROOT,
               jing_jar: DEFAULT_JING_JAR)
  @schema_root = schema_root
  @jing_jar = jing_jar
end

Instance Attribute Details

#jing_jarObject (readonly)

Returns the value of attribute jing_jar.



50
51
52
# File 'lib/idml/validation/validator.rb', line 50

def jing_jar
  @jing_jar
end

#schema_rootObject (readonly)

Returns the value of attribute schema_root.



50
51
52
# File 'lib/idml/validation/validator.rb', line 50

def schema_root
  @schema_root
end

Instance Method Details

#loose_validate_part(part_name, xml) ⇒ Object

Tolerate DOMVersion mismatches by blanking the attribute before validation. Useful for older IDML files: catches every schema violation except version drift, which is expected.



68
69
70
71
72
73
74
75
76
77
# File 'lib/idml/validation/validator.rb', line 68

def loose_validate_part(part_name, xml)
  loosened = xml.gsub(/DOMVersion="[^"]*"/, 'DOMVersion=""')
  result = validate_part(part_name, loosened)
  return result if result.ok?

  non_version_errors = result.errors.reject do |e|
    e.include?("DOMVersion")
  end
  Result.new(part_name, non_version_errors.empty?, non_version_errors)
end

#validate_package(package) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/idml/validation/validator.rb', line 79

def validate_package(package)
  package.part_names.filter_map do |name|
    next if NON_SCHEMATIZED_PARTS.include?(name)

    validate_part(name, package.read_part(name))
  end
end

#validate_part(part_name, xml) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/idml/validation/validator.rb', line 52

def validate_part(part_name, xml)
  schema = schema_path_for(part_name)
  unless schema
    return Result.new(part_name, false,
                      ["no schema mapping for #{part_name}"])
  end

  output, status = run_jing(schema, xml)
  ok = status.success? && output.empty?
  errors = ok ? [] : output.split("\n").grep(/error:/)
  Result.new(part_name, ok, errors)
end