1require 'rexml/formatters/pretty' 2 3module REXML 4 module Formatters 5 # The Transitive formatter writes an XML document that parses to an 6 # identical document as the source document. This means that no extra 7 # whitespace nodes are inserted, and whitespace within text nodes is 8 # preserved. Within these constraints, the document is pretty-printed, 9 # with whitespace inserted into the metadata to introduce formatting. 10 # 11 # Note that this is only useful if the original XML is not already 12 # formatted. Since this formatter does not alter whitespace nodes, the 13 # results of formatting already formatted XML will be odd. 14 class Transitive < Default 15 def initialize( indentation=2, ie_hack=false ) 16 @indentation = indentation 17 @level = 0 18 @ie_hack = ie_hack 19 end 20 21 protected 22 def write_element( node, output ) 23 output << "<#{node.expanded_name}" 24 25 node.attributes.each_attribute do |attr| 26 output << " " 27 attr.write( output ) 28 end unless node.attributes.empty? 29 30 output << "\n" 31 output << ' '*@level 32 if node.children.empty? 33 output << " " if @ie_hack 34 output << "/" 35 else 36 output << ">" 37 # If compact and all children are text, and if the formatted output 38 # is less than the specified width, then try to print everything on 39 # one line 40 @level += @indentation 41 node.children.each { |child| 42 write( child, output ) 43 } 44 @level -= @indentation 45 output << "</#{node.expanded_name}" 46 output << "\n" 47 output << ' '*@level 48 end 49 output << ">" 50 end 51 52 def write_text( node, output ) 53 output << node.to_s() 54 end 55 end 56 end 57end 58