Class: Docbook::Services::Validator
- Inherits:
-
Object
- Object
- Docbook::Services::Validator
- Defined in:
- lib/docbook/services/validator.rb
Overview
Validates DocBook XML documents. Handles well-formedness checks and RELAX NG schema validation.
Defined Under Namespace
Classes: ValidationResult
Constant Summary collapse
- SCHEMAS_DIR =
File.("../schemas", __dir__)
Instance Method Summary collapse
-
#check_schema(doc = nil) ⇒ ValidationResult
Validate against DocBook RELAX NG schema.
-
#check_wellformedness ⇒ ValidationResult
Check well-formedness only.
-
#initialize(input_path:) ⇒ Validator
constructor
A new instance of Validator.
-
#validate ⇒ ValidationResult
Full validation: well-formedness + schema.
Constructor Details
#initialize(input_path:) ⇒ Validator
Returns a new instance of Validator.
12 13 14 |
# File 'lib/docbook/services/validator.rb', line 12 def initialize(input_path:) @input_path = input_path end |
Instance Method Details
#check_schema(doc = nil) ⇒ ValidationResult
Validate against DocBook RELAX NG schema.
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/docbook/services/validator.rb', line 29 def check_schema(doc = nil) doc ||= Nokogiri::XML(File.read(@input_path)) schema_file = if xinclude?(doc) File.join(SCHEMAS_DIR, "docbookxi.rng") else File.join(SCHEMAS_DIR, "docbook.rng") end rng = File.read(schema_file) schema = Nokogiri::XML::RelaxNG(rng) errors = schema.validate(doc).map { |e| "#{@input_path}: #{e}" } ValidationResult.new(valid?: errors.empty?, errors: errors) end |
#check_wellformedness ⇒ ValidationResult
Check well-formedness only.
18 19 20 21 22 23 24 |
# File 'lib/docbook/services/validator.rb', line 18 def check_wellformedness xml_string = File.read(@input_path) doc = Nokogiri::XML(xml_string) errors = doc.errors.map { |e| "#{@input_path}: #{e}" } ValidationResult.new(valid?: errors.empty?, errors: errors) end |
#validate ⇒ ValidationResult
Full validation: well-formedness + schema.
45 46 47 48 49 50 |
# File 'lib/docbook/services/validator.rb', line 45 def validate wf = check_wellformedness return wf unless wf.valid? check_schema end |