1 require 'rexml/formatters/pretty'
2
3 module 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 )
16 @indentation = indentation
17 @level = 0
18 end
19
20 protected
21 def write_element( node, output )
22 output << "<#{node.expanded_name}"
23
24 node.attributes.each_attribute do |attr|
25 output << " "
26 attr.write( output )
27 end unless node.attributes.empty?
28
29 output << "\n"
30 output << ' '*@level
31 if node.children.empty?
32 output << "/"
33 else
34 output << ">"
35 # If compact and all children are text, and if the formatted output
36 # is less than the specified width, then try to print everything on
37 # one line
38 skip = false
39 @level += @indentation
40 node.children.each { |child|
41 write( child, output )
42 }
43 @level -= @indentation
44 output << "</#{node.expanded_name}"
45 output << "\n"
46 output << ' '*@level
47 end
48 output << ">"
49 end
50
51 def write_text( node, output )
52 output << node.to_s()
53 end
54 end
55 end
56 end