class MSS::Core::XML::Parser

Attributes

rules[R]

@return [Hash] Returns the rules for this xml parser that define

how it should transform the XMl into Ruby.

Public Class Methods

new(rules = {}) click to toggle source

@param [Hash] rules A has of xml parsing rules. Generally

rules will come from an xml grammar.
# File lib/mss/core/xml/parser.rb, line 20
def initialize rules = {}
  @rules = rules
end
parse(xml, rules = {}) click to toggle source

@param [String] xml An XML document string to parse. @param [Hash] rules A has of xml parsing rules. Generally

rules will come from an xml grammar.

@return [Hash] Returns a hash of parsed xml data.

# File lib/mss/core/xml/parser.rb, line 44
def self.parse xml, rules = {}
  self.new(rules).parse(xml)
end

Public Instance Methods

parse(xml) click to toggle source

@param [String] xml An XML document string to parse. @return [Hash] Returns a hash of parsed xml data.

# File lib/mss/core/xml/parser.rb, line 30
def parse xml
  xml = '<xml/>' if xml.nil? or xml.empty?
  sax_handler.parse(xml)
end
simulate() click to toggle source

@return [Hash] Returns a hash of mostly empty placeholder data.

# File lib/mss/core/xml/parser.rb, line 36
def simulate
  XML::Stub.simulate(rules)
end

Protected Instance Methods

sax_handler() click to toggle source

There are three handlers, nokogiri is the fastest, followed by libxml-ruby. Lastly (by a long shot) is REXML. REXML is the only library that does not rely on a native extension.

Currently you can not choose your xml sax handler, and the only we express a gem dependency on is nokogiri.

# File lib/mss/core/xml/parser.rb, line 58
def sax_handler
  begin
    SaxHandlers::Nokogiri.new(rules)
  rescue NameError, LoadError
    SaxHandlers::REXML.new(rules)
  end
end