Class: CamtParser::Xml

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

Constant Summary collapse

PARSER_MAPPING =
{
  camt052: CamtParser::Format052::Base,
  camt053: CamtParser::Format053::Base,
  camt054: CamtParser::Format054::Base
}.freeze
SUPPORTED_PARSERS =
PARSER_MAPPING.keys.freeze

Class Method Summary collapse

Class Method Details

.parse(doc) ⇒ Object


25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/camt_parser/xml.rb', line 25

def self.parse(doc)
  raise CamtParser::Errors::NotXMLError, doc.class unless doc.is_a? Nokogiri::XML::Document

  namespace = doc.namespaces['xmlns']
  doc.remove_namespaces!

  parser_class = @namespace_parsers[namespace]
  if parser_class == nil
    raise CamtParser::Errors::UnsupportedNamespaceError, namespace
  end

  return parser_class.new(doc.xpath('Document'))
end

.register(namespace, parser) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
# File 'lib/camt_parser/xml.rb', line 13

def self.register(namespace, parser)
  if !SUPPORTED_PARSERS.include?(parser)
    raise CamtParser::Errors::UnsupportedParserClass, parser.to_s
  end

  if @namespace_parsers.key?(namespace) # Prevent overwriting existing registrations
    raise CamtParser::Errors::NamespaceAlreadyRegistered, namespace
  end

  @namespace_parsers[namespace] = PARSER_MAPPING[parser]
end