Class: Moxml::C14n::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/moxml/c14n/writer.rb

Overview

Builds canonical octet output incrementally. Output is always UTF-8 with no BOM.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWriter

Returns a new instance of Writer.



10
11
12
13
14
15
16
# File 'lib/moxml/c14n/writer.rb', line 10

def initialize
  @output = (+"")
  # Tracks namespaces already rendered in the output ancestor chain,
  # so descendants don't re-render the same declaration.
  # Key: element (by object identity), value: { prefix => uri }.
  @rendered_stack = []
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



8
9
10
# File 'lib/moxml/c14n/writer.rb', line 8

def output
  @output
end

Instance Method Details

#<<(str) ⇒ Object



18
19
20
21
# File 'lib/moxml/c14n/writer.rb', line 18

def <<(str)
  @output << str
  self
end

#attribute(expanded_name, value) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/moxml/c14n/writer.rb', line 76

def attribute(expanded_name, value)
  @output << " "
  @output << expanded_name
  @output << "=\""
  @output << C14n.escape_attribute(value)
  @output << "\""
  self
end

#close_rendered_frameObject



37
38
39
40
# File 'lib/moxml/c14n/writer.rb', line 37

def close_rendered_frame
  @rendered_stack.pop
  self
end

#close_tag(prefix, local) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/moxml/c14n/writer.rb', line 68

def close_tag(prefix, local)
  @output << "</"
  @output << "#{prefix}:" unless prefix.nil? || prefix.empty?
  @output << local
  @output << ">"
  self
end

#close_tag_openObject



58
59
60
61
# File 'lib/moxml/c14n/writer.rb', line 58

def close_tag_open
  @output << ">"
  self
end

#close_tag_self_closeObject



63
64
65
66
# File 'lib/moxml/c14n/writer.rb', line 63

def close_tag_self_close
  @output << "></"
  self
end

#comment(content) ⇒ Object



104
105
106
107
108
109
# File 'lib/moxml/c14n/writer.rb', line 104

def comment(content)
  @output << "<!--"
  @output << C14n.escape_text(content)
  @output << "-->"
  self
end

#mark_rendered(_element, prefix, uri) ⇒ Object



46
47
48
49
# File 'lib/moxml/c14n/writer.rb', line 46

def mark_rendered(_element, prefix, uri)
  (@rendered_stack.last || {})[prefix] = uri
  self
end

#namespace(prefix, uri) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/moxml/c14n/writer.rb', line 85

def namespace(prefix, uri)
  @output << " "
  if prefix.nil? || prefix.empty?
    @output << "xmlns"
  else
    @output << "xmlns:"
    @output << prefix
  end
  @output << "=\""
  @output << C14n.escape_attribute(uri)
  @output << "\""
  self
end

#open_rendered_frame(element) ⇒ Object

Push a fresh "rendered" frame for the given element. Called by the canonicalizer before rendering the element's namespaces.



30
31
32
33
34
35
# File 'lib/moxml/c14n/writer.rb', line 30

def open_rendered_frame(element)
  parent_rendered = @rendered_stack.last || {}
  # Inherit parent's rendered namespaces as the starting set.
  @rendered_stack.push(parent_rendered.dup)
  element
end

#open_tag(prefix, local) ⇒ Object



51
52
53
54
55
56
# File 'lib/moxml/c14n/writer.rb', line 51

def open_tag(prefix, local)
  @output << "<"
  @output << "#{prefix}:" unless prefix.nil? || prefix.empty?
  @output << local
  self
end

#processing_instruction(target, content) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/moxml/c14n/writer.rb', line 111

def processing_instruction(target, content)
  @output << "<?"
  @output << target
  @output << " "
  @output << content
  @output << "?>"
  self
end

#raw(str) ⇒ Object



23
24
25
26
# File 'lib/moxml/c14n/writer.rb', line 23

def raw(str)
  @output << str.to_s
  self
end

#rendered_namespaces_for(_element) ⇒ Object



42
43
44
# File 'lib/moxml/c14n/writer.rb', line 42

def rendered_namespaces_for(_element)
  @rendered_stack.last || {}
end

#text(content) ⇒ Object



99
100
101
102
# File 'lib/moxml/c14n/writer.rb', line 99

def text(content)
  @output << C14n.escape_text(content)
  self
end