Class: Goods::XML::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/goods/xml/validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeValidator

Returns a new instance of Validator.



8
9
10
# File 'lib/goods/xml/validator.rb', line 8

def initialize
  @error = nil
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



6
7
8
# File 'lib/goods/xml/validator.rb', line 6

def error
  @error
end

Instance Method Details

#valid?(xml_io) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
# File 'lib/goods/xml/validator.rb', line 12

def valid?(xml_io)
  validate(xml_io)
  error.nil?
end

#validate(xml_io) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/goods/xml/validator.rb', line 17

def validate(xml_io)
  @error = nil
  document = LibXML::XML::Document.io(xml_io)

  # Should silence STDERR, because libxml2 spews validation error
  # to standard error stream
  silence_stream(STDERR) do
    # Catch first exception due to bug in libxml - it throws
    # 'the model is not determenistic' error. The second validation runs
    # just fine and gives the real error.
    begin
      document.validate(dtd)
    rescue LibXML::XML::Error => e
      #nothing
    end

    begin
      document.validate(dtd)
    rescue LibXML::XML::Error => e
      @error = e.to_s
    end
  end
end