Class: Goods::XML::Validator
- Inherits:
-
Object
- Object
- Goods::XML::Validator
- Defined in:
- lib/goods/xml/validator.rb
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
Instance Method Summary collapse
-
#initialize ⇒ Validator
constructor
A new instance of Validator.
- #valid?(xml) ⇒ Boolean
- #validate(xml) ⇒ Object
Constructor Details
#initialize ⇒ Validator
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
#error ⇒ Object (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) ⇒ Boolean
12 13 14 15 |
# File 'lib/goods/xml/validator.rb', line 12 def valid?(xml) validate(xml) error.nil? end |
#validate(xml) ⇒ 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) @error = nil document = LibXML::XML::Document.string(xml) # 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 |