Class: Writexlsx::Package::XMLWriterSimple

Inherits:
Object
  • Object
show all
Defined in:
lib/write_xlsx/package/xml_writer_simple.rb

Constant Summary collapse

XMLNS =
'http://schemas.openxmlformats.org/spreadsheetml/2006/main'

Instance Method Summary collapse

Constructor Details

#initializeXMLWriterSimple

Returns a new instance of XMLWriterSimple.



14
15
16
17
18
19
20
21
# File 'lib/write_xlsx/package/xml_writer_simple.rb', line 14

def initialize
  @io = StringIO.new
  # Will allocate new string once, then use allocated string
  # Key is tag name
  # Only tags without attributes will be cached
  @tag_start_cache = {}
  @tag_end_cache = {}
end

Instance Method Details

#characters(data) ⇒ Object



87
88
89
# File 'lib/write_xlsx/package/xml_writer_simple.rb', line 87

def characters(data)
  io_write(escape_data(data))
end

#closeObject



95
96
97
98
# File 'lib/write_xlsx/package/xml_writer_simple.rb', line 95

def close
  File.open(@filename, "wb:utf-8:utf-8") { |f| f << string } if @filename
  @io.close
end

#crlfObject



91
92
93
# File 'lib/write_xlsx/package/xml_writer_simple.rb', line 91

def crlf
  io_write("\n")
end

#data_element(tag, data, attr = nil) ⇒ Object



69
70
71
# File 'lib/write_xlsx/package/xml_writer_simple.rb', line 69

def data_element(tag, data, attr = nil)
  tag_elements(tag, attr) { io_write(escape_data(data)) }
end

#empty_tag(tag, attr = nil) ⇒ Object



64
65
66
67
# File 'lib/write_xlsx/package/xml_writer_simple.rb', line 64

def empty_tag(tag, attr = nil)
  str = "<#{tag}#{key_vals(attr)}/>"
  io_write(str)
end

#end_tag(tag) ⇒ Object



56
57
58
# File 'lib/write_xlsx/package/xml_writer_simple.rb', line 56

def end_tag(tag)
  io_write(end_tag_str(tag))
end

#end_tag_str(tag) ⇒ Object



60
61
62
# File 'lib/write_xlsx/package/xml_writer_simple.rb', line 60

def end_tag_str(tag)
  @tag_end_cache[tag] ||= "</#{tag}>"
end

#io_write(str) ⇒ Object



104
105
106
107
# File 'lib/write_xlsx/package/xml_writer_simple.rb', line 104

def io_write(str)
  @io << str
  str
end

#set_xml_writer(filename = nil) ⇒ Object



23
24
25
# File 'lib/write_xlsx/package/xml_writer_simple.rb', line 23

def set_xml_writer(filename = nil)
  @filename = filename
end

#si_element(data, attr) ⇒ Object

Optimised tag writer ? for shared strings <si> elements.



76
77
78
# File 'lib/write_xlsx/package/xml_writer_simple.rb', line 76

def si_element(data, attr)
  tag_elements('si') { data_element('t', data, attr) }
end

#si_rich_element(data) ⇒ Object

Optimised tag writer for shared strings <si> rich string elements.



83
84
85
# File 'lib/write_xlsx/package/xml_writer_simple.rb', line 83

def si_rich_element(data)
  io_write("<si>#{data}</si>")
end

#start_tag(tag, attr = nil) ⇒ Object



44
45
46
# File 'lib/write_xlsx/package/xml_writer_simple.rb', line 44

def start_tag(tag, attr = nil)
  io_write(start_tag_str(tag, attr))
end

#start_tag_str(tag, attr = nil) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/write_xlsx/package/xml_writer_simple.rb', line 48

def start_tag_str(tag, attr = nil)
  if attr.nil? || attr.empty?
    @tag_start_cache[tag] ||= "<#{tag}>"
  else
    "<#{tag}#{key_vals(attr)}>"
  end
end

#stringObject



100
101
102
# File 'lib/write_xlsx/package/xml_writer_simple.rb', line 100

def string
  @io.string
end

#tag_elements(tag, attributes = nil) ⇒ Object



32
33
34
35
36
# File 'lib/write_xlsx/package/xml_writer_simple.rb', line 32

def tag_elements(tag, attributes = nil)
  start_tag(tag, attributes)
  yield
  end_tag(tag)
end

#tag_elements_str(tag, attributes = nil) ⇒ Object



38
39
40
41
42
# File 'lib/write_xlsx/package/xml_writer_simple.rb', line 38

def tag_elements_str(tag, attributes = nil)
  start_tag_str(tag, attributes) +
    yield +
    end_tag_str(tag)
end

#xml_decl(encoding = 'UTF-8', standalone = true) ⇒ Object



27
28
29
30
# File 'lib/write_xlsx/package/xml_writer_simple.rb', line 27

def xml_decl(encoding = 'UTF-8', standalone = true)
  str = %(<?xml version="1.0" encoding="#{encoding}" standalone="#{standalone ? "yes" : "no"}"?>\n)
  io_write(str)
end