1 module REXML
2 module Formatters
3 class Default
4 # Prints out the XML document with no formatting -- except if id_hack is
5 # set.
6 #
7 # ie_hack::
8 # If set to true, then inserts whitespace before the close of an empty
9 # tag, so that IE's bad XML parser doesn't choke.
10 def initialize( ie_hack=false )
11 @ie_hack = ie_hack
12 end
13
14 # Writes the node to some output.
15 #
16 # node::
17 # The node to write
18 # output::
19 # A class implementing <TT><<</TT>. Pass in an Output object to
20 # change the output encoding.
21 def write( node, output )
22 case node
23
24 when Document
25 if node.xml_decl.encoding != "UTF-8" && !output.kind_of?(Output)
26 output = Output.new( output, node.xml_decl.encoding )
27 end
28 write_document( node, output )
29
30 when Element
31 write_element( node, output )
32
33 when Declaration, ElementDecl, NotationDecl, ExternalEntity, Entity,
34 Attribute, AttlistDecl
35 node.write( output,-1 )
36
37 when Instruction
38 write_instruction( node, output )
39
40 when DocType, XMLDecl
41 node.write( output )
42
43 when Comment
44 write_comment( node, output )
45
46 when CData
47 write_cdata( node, output )
48
49 when Text
50 write_text( node, output )
51
52 else
53 raise Exception.new("XML FORMATTING ERROR")
54
55 end
56 end
57
58 protected
59 def write_document( node, output )
60 node.children.each { |child| write( child, output ) }
61 end
62
63 def write_element( node, output )
64 output << "<#{node.expanded_name}"
65
66 node.attributes.each_attribute do |attr|
67 output << " "
68 attr.write( output )
69 end unless node.attributes.empty?
70
71 if node.children.empty?
72 output << " " if @ie_hack
73 output << "/"
74 else
75 output << ">"
76 node.children.each { |child|
77 write( child, output )
78 }
79 output << "</#{node.expanded_name}"
80 end
81 output << ">"
82 end
83
84 def write_text( node, output )
85 output << node.to_s()
86 end
87
88 def write_comment( node, output )
89 output << Comment::START
90 output << node.to_s
91 output << Comment::STOP
92 end
93
94 def write_cdata( node, output )
95 output << CData::START
96 output << node.to_s
97 output << CData::STOP
98 end
99
100 def write_instruction( node, output )
101 output << Instruction::START.sub(/\\/u, '')
102 output << node.target
103 output << ' '
104 output << node.content
105 output << Instruction::STOP.sub(/\\/u, '')
106 end
107 end
108 end
109 end