Sha256: 6bc9ecbabcc50d74abf0b6a959c3f2a2d536c7f0cd01dc2e538ef0b5a8bc7261

Contents?: true

Size: 1.43 KB

Versions: 4

Compression:

Stored size: 1.43 KB

Contents

require File.dirname(__FILE__) + "/node_visitor"

module Glimmer
  class XmlVisitor < NodeVisitor

    attr_reader :document

    def initialize
      @document = ""
    end

    def process_before_children(node)
      if (node.is_a?(String))
        @document << node
        return
      end
      begin_open_tag(node)
      append_attributes(node) if node.attributes
      end_open_tag(node)
    end

    def process_after_children(node)
      return if (node.is_a?(String))
      append_close_tag(node)
    end

    def begin_open_tag(node)
      @document << "<"
      @document << "#{node.name_space.name}:" if node.name_space
      @document << node.name
    end

    def end_open_tag(node)
      if (node.contents)
        @document << ">"
      else
        @document << " " if node.attributes.keys.size > 0
        @document << "/>"
      end
    end

    def append_close_tag(node)
      if (node.contents)
        @document << "</"
        @document << "#{node.name_space.name}:" if node.name_space
        @document << "#{node.name}>"
      end
    end

    def append_attributes(node)
      Glimmer.logger.debug "Take 3"
      Glimmer.logger.debug(node.attributes)
      node.attributes.each_key do |attribute|
        attribute_name = attribute
        attribute_name = "#{attribute.name_space.name}:#{attribute.name}" if attribute.is_a?(Node)
        @document << " #{attribute_name}=\"#{node.attributes[attribute]}\""
      end
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
glimmer-0.4.3 lib/glimmer/xml_command_handlers/models/xml_visitor.rb
glimmer-0.4.2 lib/glimmer/xml_command_handlers/models/xml_visitor.rb
glimmer-0.4.1 lib/glimmer/xml_command_handlers/models/xml_visitor.rb
glimmer-0.4.0 lib/glimmer/xml_command_handlers/models/xml_visitor.rb