Sha256: 87040e74b85fa6f462ab168fd90858ee4069eda816faaa73805a4061f32c055c

Contents?: true

Size: 1.71 KB

Versions: 6

Compression:

Stored size: 1.71 KB

Contents

# Author::    Jerome Pouiller  <pouill_j@epita.fr>.
# Copyright:: Copyright (c) 2004, 2005 Uttk team. All rights reserved.
# License::   LGPL
# $Id: /w/fey/uttk/trunk/lib/uttk/dumpers/Xml.rb 21970 2006-02-19T22:15:36.271512Z pouillar  $


require 'rexml/document'

module Uttk

  module Dumpers

    class Xml < Dumper
      include Concrete


      def initialize ( *a, &b )
        super
        puts '<?xml version="1.0" encoding="iso-8859-1"?>'
        reset
      end


      def reset
        @doc = @root = REXML::Document.new
        @doc_stack = []
      end


      def add_elt ( elt, hsh={} )
        return @doc if @doc.eql? @root and @root.has_elements?
        @doc = @doc.add_element(REXML::Element.new(clean_for_xml(elt)))
        hsh.each do |k, v|
          @doc.add_attribute(clean_for_xml(k), v)
        end
        @doc
      end
      private :add_elt


      def new_node ( path, node )
        super
        doc_ = @doc
        opts = { :name => node.segment }
        if type = node.options[:type]
          opts[:type] = type
        end
        add_elt(:node, opts)
        @doc_stack << doc_
      end
      protected :new_node


      def new_leaf ( path, leaf )
        super
        @doc.add_text(leaf.to_s)
      end
      protected :new_leaf


      def up ( path )
        super
        @doc = @doc_stack.pop unless @doc_stack.empty?
      end
      protected :up


      def close
        @root.write(self, 0, false)
        puts
        reset
        super
      end
      protected :close


      def clean_for_xml ( anObject )
        str = anObject.to_s
        str.gsub!(/[^a-zA-Z:-_]/, '_')
        str
      end
      protected :clean_for_xml

    end # class Xml

  end # module Dumpers

end # module Uttk

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
uttk-0.3.5.0 lib/uttk/dumpers/Xml.rb
uttk-0.3.6.1 lib/uttk/dumpers/Xml.rb
uttk-0.3.1.2 lib/uttk/dumpers/Xml.rb
uttk-0.4.6.2 lib/uttk/dumpers/Xml.rb
uttk-0.4.5.0 lib/uttk/dumpers/Xml.rb
uttk-0.4.6.1 lib/uttk/dumpers/Xml.rb