class EimXML::Formatter

Attributes

out[R]

Public Class Methods

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

Public Instance Methods

indent(&proc) click to toggle source
# File lib/eim_xml/formatter.rb, line 37
def indent(&proc)
        @indent_depth += 1
        proc.call
ensure
        @indent_depth -= 1
end
preserve_space_element?(elm) click to toggle source
# File lib/eim_xml/formatter.rb, line 44
def preserve_space_element?(elm)
        @preservers && @preservers.any? do |e|
                case e
                when Symbol
                        e==elm.name
                when Class
                        e===elm
                end
        end
end
write(src) click to toggle source
# File lib/eim_xml/formatter.rb, line 22
def write(src)
        case src
        when ElementWrapper
                write_wrapper(src)
        when Comment
                write_comment(src)
        when Element
                write_element(src)
        when PCString
                write_pcstring(src)
        else
                write_string(src.to_s)
        end
end
write_comment(c) click to toggle source
# File lib/eim_xml/formatter.rb, line 63
def write_comment(c)
        write_indent
        c.write_to(out)
        write_newline
end
write_contents_of(elm) click to toggle source
# File lib/eim_xml/formatter.rb, line 69
def write_contents_of(elm)
        flag = @preserve_space
        @preserve_space = true if preserve_space_element?(elm)
        write_newline
        indent do
                elm.contents.each do |c|
                        write(c)
                end
        end
        write_indent
ensure
        @preserve_space = flag
end
write_element(elm) click to toggle source
# File lib/eim_xml/formatter.rb, line 83
def write_element(elm)
        write_indent
        out << "<"
        elm.name_and_attributes(out)
        case elm.contents.size
        when 0
                out << " />"
                write_newline
        else
                out << ">"
                write_contents_of(elm)
                out << "</#{elm.name}>"
                write_newline
        end
end
write_indent() click to toggle source
# File lib/eim_xml/formatter.rb, line 55
def write_indent
        out << @indent_string*@indent_depth unless @preserve_space
end
write_newline() click to toggle source
# File lib/eim_xml/formatter.rb, line 59
def write_newline
        out << "\n" unless @preserve_space
end
write_pcstring(pcs) click to toggle source
# File lib/eim_xml/formatter.rb, line 99
def write_pcstring(pcs)
        pcs.encoded_string.each_line do |l|
                write_indent
                out << l
        end
        write_newline
end
write_string(str) click to toggle source
# File lib/eim_xml/formatter.rb, line 107
def write_string(str)
        PCString.encode(str).each_line do |l|
                write_indent
                out << l
        end
        write_newline
end
write_wrapper(wrapper) click to toggle source
# File lib/eim_xml/formatter.rb, line 115
def write_wrapper(wrapper)
        wrapper.each(@option) do |i|
                write(i)
        end
end