File: xmlformatter.rb

package info (click to toggle)
libdbi-ruby 0.4.3-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 472 kB
  • ctags: 619
  • sloc: ruby: 4,583; makefile: 62; perl: 12
file content (73 lines) | stat: -rw-r--r-- 2,998 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
module DBI
    module Utils
        # Formats results in XML.
        module XMLFormatter
            # Generate XML for a row. The column names will surround the the values as tags.
            #
            # * +dbrow+: the array of the result row.
            # * +rowtag+: the name of the tag that encapsulates a row.
            # * +output+: Object that responds to `<<`.
            #
            def self.row(dbrow, rowtag="row", output=STDOUT)
                #XMLFormatter.extended_row(dbrow, "row", [],  
                output << "<#{rowtag}>\n"
                dbrow.each_with_name do |val, name|
                    output << "  <#{name}>" + textconv(val) + "</#{name}>\n" 
                end
                output << "</#{rowtag}>\n"
            end

            # good lord, what a mess.
            #
            # nil in cols_as_tag, means "all columns expect those listed in cols_in_row_tag"
            # add_row_tag_attrs are additional attributes which are inserted into the row-tag
            def self.extended_row(dbrow, rowtag="row", cols_in_row_tag=[], cols_as_tag=nil, add_row_tag_attrs={}, output=STDOUT)
                if cols_as_tag.nil?
                    cols_as_tag = dbrow.column_names - cols_in_row_tag
                end

                output << "<#{rowtag}"
                add_row_tag_attrs.each do |key, val|  
                    # TODO: use textconv ? " substitution?
                    output << %{ #{key}="#{textconv(val)}"}
                end
                cols_in_row_tag.each do |key|
                    # TODO: use textconv ? " substitution?
                    output << %{ #{key}="#{dbrow[key]}"}
                end
                output << ">\n"

                cols_as_tag.each do |key|
                    output << "  <#{key}>" + textconv(dbrow[key]) + "</#{key}>\n" 
                end
                output << "</#{rowtag}>\n"
            end

            # generate a full XML representation of the table.
            # 
            # Arguments and output are similar to #row, with the exception of
            # +roottag+, which is a container for the individual row tags.
            #
            def self.table(rows, roottag = "rows", rowtag = "row", output=STDOUT)
                output << '<?xml version="1.0" encoding="UTF-8" ?>'
                output << "\n<#{roottag}>\n"
                rows.each do |row|
                    row(row, rowtag, output)
                end
                output << "</#{roottag}>\n"
            end

            class << self
                private
                # Your standard XML entity conversions.
                def textconv(str)
                    str = str.to_s.gsub('&', "&#38;")
                    str = str.gsub('\'', "&#39;")
                    str = str.gsub('"', "&#34;")
                    str = str.gsub('<', "&#60;")
                    str.gsub('>', "&#62;")
                end
            end # class self
        end # module XMLFormatter
    end
end