Class Core::Config::XMLParser
In: core/config.rb
Parent: Object

ParserInterface implementation to read configuration parameters from a XML file.

Methods

get_option   get_type   new   put_option   save  

Included Modules

ParserInterface

Public Class methods

Returns :xml symbol. This method is used by the Factory to choose the appropiate implementation for a given file.

[Source]

# File core/config.rb, line 59
      def XMLParser.get_type() return :xml; end

Read and parse the XML file. Stores the options in an internal Hash to speed up the retrieving process.

[Source]

# File core/config.rb, line 63
      def initialize(file)
        @file = file
        fd = File.new(@file)
        @src = REXML::Document.new(fd)
        fd.close()
        @options = {}
        @modified = false
        @src.elements.each('dradis/option') do |element|
          @options[element.attributes['name'].to_sym] = element.attributes['value']
        end
      end

Public Instance methods

See ParserInterface#get_option

[Source]

# File core/config.rb, line 76
      def get_option(key)
        if @options.key?(key)
          @options[key]
        else
          raise ArgumentError.new('option not found in config file!')
        end
      end

See ParserInterface#put_option

[Source]

# File core/config.rb, line 85
      def put_option(key, value)
        if @options.key?(key)
          return if @options[key] == value
        end
        @options[key] = value
        @modified = true
      end

See ParserInterface#save

[Source]

# File core/config.rb, line 94
      def save
        return unless @modified
        @options.each do |name, value|
          elements = @src.get_elements("dradis/option[@name='#{name}']")
          if (elements.size.zero?)
            # a new element needs to be created
            @src.root.add_element( 'option', { 'name' => name, 'value' => value})
          else
            # update the existing element
            elements.first.attributes['value'] = value
          end
        end
        fd = File.new(@file, 'w')
        fmt = REXML::Formatters::Pretty.new(2)
        fmt.write( @src, fd )
        fd.close()
      end

[Validate]