class EimXML::Formatter

Attributes

out[R]

Public Class Methods

new(opt) click to toggle source
   # File lib/eim_xml/formatter.rb
13 def initialize(opt)
14         @out = opt[:out]
15         @preservers = opt[:preservers]
16         @preserve_space = false
17         @indent_string = "  "
18         @indent_depth = 0
19         @option = opt.dup.tap{|h| [:out, :preservers].each{|k| h.delete(k)}}
20 end
write(element, opt={}) click to toggle source
   # File lib/eim_xml/formatter.rb
 7 def self.write(element, opt={})
 8         opt = {:out=>""}.merge(opt)
 9         new(opt).write(element)
10         opt[:out]
11 end

Public Instance Methods

indent(&proc) click to toggle source
   # File lib/eim_xml/formatter.rb
37 def indent(&proc)
38         @indent_depth += 1
39         proc.call
40 ensure
41         @indent_depth -= 1
42 end
preserve_space_element?(elm) click to toggle source
   # File lib/eim_xml/formatter.rb
44 def preserve_space_element?(elm)
45         @preservers && @preservers.any? do |e|
46                 case e
47                 when Symbol
48                         e==elm.name
49                 when Class
50                         e===elm
51                 end
52         end
53 end
write(src) click to toggle source
   # File lib/eim_xml/formatter.rb
22 def write(src)
23         case src
24         when ElementWrapper
25                 write_wrapper(src)
26         when Comment
27                 write_comment(src)
28         when Element
29                 write_element(src)
30         when PCString
31                 write_pcstring(src)
32         else
33                 write_string(src.to_s)
34         end
35 end
write_comment(c) click to toggle source
   # File lib/eim_xml/formatter.rb
63 def write_comment(c)
64         write_indent
65         c.write_to(out)
66         write_newline
67 end
write_contents_of(elm) click to toggle source
   # File lib/eim_xml/formatter.rb
69 def write_contents_of(elm)
70         flag = @preserve_space
71         @preserve_space = true if preserve_space_element?(elm)
72         write_newline
73         indent do
74                 elm.contents.each do |c|
75                         write(c)
76                 end
77         end
78         write_indent
79 ensure
80         @preserve_space = flag
81 end
write_element(elm) click to toggle source
   # File lib/eim_xml/formatter.rb
83 def write_element(elm)
84         write_indent
85         out << "<"
86         elm.name_and_attributes(out)
87         case elm.contents.size
88         when 0
89                 out << " />"
90                 write_newline
91         else
92                 out << ">"
93                 write_contents_of(elm)
94                 out << "</#{elm.name}>"
95                 write_newline
96         end
97 end
write_indent() click to toggle source
   # File lib/eim_xml/formatter.rb
55 def write_indent
56         out << @indent_string*@indent_depth unless @preserve_space
57 end
write_newline() click to toggle source
   # File lib/eim_xml/formatter.rb
59 def write_newline
60         out << "\n" unless @preserve_space
61 end
write_pcstring(pcs) click to toggle source
    # File lib/eim_xml/formatter.rb
 99 def write_pcstring(pcs)
100         pcs.encoded_string.each_line do |l|
101                 write_indent
102                 out << l
103         end
104         write_newline
105 end
write_string(str) click to toggle source
    # File lib/eim_xml/formatter.rb
107 def write_string(str)
108         PCString.encode(str).each_line do |l|
109                 write_indent
110                 out << l
111         end
112         write_newline
113 end
write_wrapper(wrapper) click to toggle source
    # File lib/eim_xml/formatter.rb
115 def write_wrapper(wrapper)
116         wrapper.each(@option) do |i|
117                 write(i)
118         end
119 end