Class EimXML::Formatter
In: lib/eim_xml/formatter.rb
lib/eim_xml/formatter/element_wrapper.rb
Parent: Object

Methods

Classes and Modules

Class EimXML::Formatter::ElementWrapper

Attributes

out  [R] 

Public Class methods

[Source]

    # File lib/eim_xml/formatter.rb, line 13
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

[Source]

    # File lib/eim_xml/formatter.rb, line 7
 7:                 def self.write(element, opt={})
 8:                         opt = {:out=>""}.merge(opt)
 9:                         new(opt).write(element)
10:                         opt[:out]
11:                 end

Public Instance methods

[Source]

    # File lib/eim_xml/formatter.rb, line 37
37:                 def indent(&proc)
38:                         @indent_depth += 1
39:                         proc.call
40:                 ensure
41:                         @indent_depth -= 1
42:                 end

[Source]

    # File lib/eim_xml/formatter.rb, line 44
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

[Source]

    # File lib/eim_xml/formatter.rb, line 22
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

[Source]

    # File lib/eim_xml/formatter.rb, line 63
63:                 def write_comment(c)
64:                         write_indent
65:                         c.write_to(out)
66:                         write_newline
67:                 end

[Source]

    # File lib/eim_xml/formatter.rb, line 69
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

[Source]

    # File lib/eim_xml/formatter.rb, line 83
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

[Source]

    # File lib/eim_xml/formatter.rb, line 55
55:                 def write_indent
56:                         out << @indent_string*@indent_depth unless @preserve_space
57:                 end

[Source]

    # File lib/eim_xml/formatter.rb, line 59
59:                 def write_newline
60:                         out << "\n" unless @preserve_space
61:                 end

[Source]

     # File lib/eim_xml/formatter.rb, line 99
 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

[Source]

     # File lib/eim_xml/formatter.rb, line 107
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

[Source]

     # File lib/eim_xml/formatter.rb, line 115
115:                 def write_wrapper(wrapper)
116:                         wrapper.each(@option) do |i|
117:                                 write(i)
118:                         end
119:                 end

[Validate]