Class: ApprovalTests::Writers::XmlWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/approval_tests/writers/xml_writer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ XmlWriter

Returns a new instance of XmlWriter.



7
8
9
# File 'lib/approval_tests/writers/xml_writer.rb', line 7

def initialize(data)
  @data = data
end

Class Method Details

.get_att_list(node) ⇒ Object



57
58
59
60
61
# File 'lib/approval_tests/writers/xml_writer.rb', line 57

def get_att_list(node)
  att_list = ''
  node.attributes.each { |attribute, val| att_list += " #{attribute}='#{val}'" }
  att_list
end

.pretty_print(parent_node, itab) ⇒ Object



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
# File 'lib/approval_tests/writers/xml_writer.rb', line 26

def pretty_print(parent_node, itab)
  buffer = ''

  parent_node.elements.each do |node|

    buffer += ' ' * itab + "<#{node.name}#{get_att_list(node)}"

    if node.to_a.length > 0
      buffer += ">"
      if node.text.nil?
        buffer += "\n"
        buffer += pretty_print(node,itab+2) 
        buffer += ' ' * itab + "</#{node.name}>\n"
      else
        node_text = node.text.strip
        if node_text != ''
          buffer += node_text 
          buffer += "</#{node.name}>\n"        
        else
          buffer += "\n" + pretty_print(node,itab+2) 
          buffer += ' ' * itab + "</#{node.name}>\n"        
        end
      end
    else
      buffer += "/>\n"
    end

  end
  buffer
end

.pretty_xml(xml_string) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/approval_tests/writers/xml_writer.rb', line 63

def pretty_xml(xml_string)
  doc = Document.new(xml_string)

  buffer = ''
  xml_declaration = doc.to_s.match('<\?.*\?>').to_s
  buffer += "#{xml_declaration}\n" if not xml_declaration.nil?
  xml_doctype = doc.to_s.match('<\!.*\">').to_s
  buffer += "#{xml_doctype}\n" if not xml_doctype.nil?
  buffer += "<#{doc.root.name}#{get_att_list(doc.root)}"
  if doc.root.to_a.length > 0
    buffer +=">\n#{pretty_print(doc.root,2)}</#{doc.root.name}>"
  else
    buffer += "/>\n"
  end
end

Instance Method Details

#get_approval_filename(basename) ⇒ Object



11
12
13
# File 'lib/approval_tests/writers/xml_writer.rb', line 11

def get_approval_filename(basename)
  basename + ".approved.xml"
end

#get_received_filename(basename) ⇒ Object



15
16
17
# File 'lib/approval_tests/writers/xml_writer.rb', line 15

def get_received_filename(basename)
  basename + ".received.xml"
end

#write_received_file(received) ⇒ Object



19
20
21
22
23
24
# File 'lib/approval_tests/writers/xml_writer.rb', line 19

def write_received_file(received)
  f = File.new("#{received}", "w+")
  f.write(@data)
  f.close()
  f.path
end