Class: RNV::Validator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeValidator

Returns a new instance of Validator.



114
115
116
# File 'lib/rnv/validator.rb', line 114

def initialize
  @document = RNV::Document.new
end

Instance Attribute Details

#documentRNV::Document

Returns:



112
113
114
# File 'lib/rnv/validator.rb', line 112

def document
  @document
end

Instance Method Details

#errorsArray<RNV::Error>

errors from document

Returns:



120
121
122
# File 'lib/rnv/validator.rb', line 120

def errors
  @document.errors
end

#load_schema_from_file(file) ⇒ Boolean

Returns true if schema loaded successfuly.

Parameters:

  • file (String)

    RNC schema filename

Returns:

  • (Boolean)

    true if schema loaded successfuly



132
133
134
# File 'lib/rnv/validator.rb', line 132

def load_schema_from_file(file)
  @document.load_file(file)
end

#load_schema_from_string(str) ⇒ Boolean

Returns true if schema loaded successfuly.

Parameters:

  • str (String)

    RNC schema buffer

Returns:

  • (Boolean)

    true if schema loaded successfuly



126
127
128
# File 'lib/rnv/validator.rb', line 126

def load_schema_from_string(str)
  @document.load_string(str)
end

#parse_file(xml, pre_processor = PreProcessor.new) ⇒ Boolean

parse and validate file

Parameters:

  • xml (String, File)

    XML file to parse

  • pre_processor (RNV::PreProcessor) (defaults to: PreProcessor.new)

    an optional pre-processor for tag and attributes data

Returns:

  • (Boolean)

    true if valid

Raises:



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/rnv/validator.rb', line 160

def parse_file(xml, pre_processor = PreProcessor.new)
  @document.start_document
  rnv_doc = NokogiriSaxDocument.new(@document)
  rnv_doc.pre_processor = pre_processor

  file = xml.is_a?(File) ? xml : File.open(xml)

  raise RNV::DocumentEmpty if file.size == 0

  parser = Nokogiri::XML::SAX::Parser.new(rnv_doc)
  parser.parse(file) do |ctx|
    ctx.replace_entities = true
    rnv_doc.ctx = ctx
  end

  @document.valid?
end

#parse_string(str, pre_processor = PreProcessor.new) ⇒ Boolean

parse and validate buffer

Parameters:

  • str (String)

    XML buffer to parse

  • pre_processor (RNV::PreProcessor) (defaults to: PreProcessor.new)

    an optional pre-processor for tag and attributes data

Returns:

  • (Boolean)

    true if valid

Raises:



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/rnv/validator.rb', line 140

def parse_string(str, pre_processor = PreProcessor.new)
  @document.start_document
  rnv_doc = NokogiriSaxDocument.new(@document)
  rnv_doc.pre_processor = pre_processor

  raise RNV::DocumentEmpty if str.nil? or str.empty?

  parser = Nokogiri::XML::SAX::Parser.new(rnv_doc)
  parser.parse_memory(str) do |ctx|
    ctx.replace_entities = true
    rnv_doc.ctx = ctx
  end

  @document.valid?
end