Sha256: 0bfa61d7bde93ad1c258d6abc05d1fd07ed3e0629cae1c0803bde2eb2c83de1c

Contents?: true

Size: 1.71 KB

Versions: 37

Compression:

Stored size: 1.71 KB

Contents

require 'rexml/document'

# Custom REXML formatter that removes blank lines, extra whitespace,
# and ensures lines are indented properly.
#
# This class is used for pre-4.4 Kinetic Task servers to format tree xml 
# content when exporting trees and routines.
class Prettier < REXML::Formatters::Default

  def initialize( indentation = 4 )
    @indentation = indentation
    @level = 0
  end

  protected
  def write_element( node, output )
    output << "<#{node.expanded_name}"
    
    att = node.attributes
    class <<att
      def each_attribute_sorted(&b)
          to_enum(:each_attribute).sort_by {|x| x.name}.each(&b)
      end
    end

    node.attributes.each_attribute_sorted do |attr|
      output << " "
      attr.write( output )
    end unless node.attributes.empty?

    if node.children.empty?
      output << "></#{node.expanded_name}>"
    else
      element_nodes = node.children.select { |child| child.kind_of?(REXML::Element) }.size
      processed_element_nodes = 0

      output << ">"
      @level += @indentation
      node.children.each_index { |index|
        child = node.children[index]
        next if child.kind_of?(REXML::Text) && node.expanded_name != "parameter" && child.to_s.strip.length == 0
        if child.kind_of?(REXML::Element) then
          processed_element_nodes += 1
          write_indent_level(output, true)
        end
        write( child, output )
      }
      @level -= @indentation if @level >= @indentation
      write_indent_level(output, true) if element_nodes > 0 && processed_element_nodes == element_nodes
      output << "</#{node.expanded_name}>"
    end
  end

  private
  def write_indent_level(output, newline = false)
    output << "\n" if newline
    output << ' ' * @level
  end

end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
kinetic_sdk-5.0.29 lib/kinetic_sdk/utils/prettier-xml-formatter.rb
kinetic_sdk-5.0.28 lib/kinetic_sdk/utils/prettier-xml-formatter.rb
kinetic_sdk-5.0.27 lib/kinetic_sdk/utils/prettier-xml-formatter.rb
kinetic_sdk-5.0.26 lib/kinetic_sdk/utils/prettier-xml-formatter.rb
kinetic_sdk-5.0.25 lib/kinetic_sdk/utils/prettier-xml-formatter.rb
kinetic_sdk-5.0.24 lib/kinetic_sdk/utils/prettier-xml-formatter.rb
kinetic_sdk-5.0.23 lib/kinetic_sdk/utils/prettier-xml-formatter.rb
kinetic_sdk-5.0.22 lib/kinetic_sdk/utils/prettier-xml-formatter.rb
kinetic_sdk-5.0.21 lib/kinetic_sdk/utils/prettier-xml-formatter.rb
kinetic_sdk-5.0.20 lib/kinetic_sdk/utils/prettier-xml-formatter.rb
kinetic_sdk-5.0.19 lib/kinetic_sdk/utils/prettier-xml-formatter.rb
kinetic_sdk-5.0.18 lib/kinetic_sdk/utils/prettier-xml-formatter.rb
kinetic_sdk-5.0.17 lib/kinetic_sdk/utils/prettier-xml-formatter.rb
kinetic_sdk-5.0.16 lib/kinetic_sdk/utils/prettier-xml-formatter.rb
kinetic_sdk-5.0.15 lib/kinetic_sdk/utils/prettier-xml-formatter.rb
kinetic_sdk-5.0.14 lib/kinetic_sdk/utils/prettier-xml-formatter.rb
kinetic_sdk-5.0.13 lib/kinetic_sdk/utils/prettier-xml-formatter.rb
kinetic_sdk-5.0.12 lib/kinetic_sdk/utils/prettier-xml-formatter.rb
kinetic_sdk-5.0.11 lib/kinetic_sdk/utils/prettier-xml-formatter.rb
kinetic_sdk-5.0.10 lib/kinetic_sdk/utils/prettier-xml-formatter.rb