Class: Xampl::XMLPrinter

Inherits:
Object
  • Object
show all
Defined in:
lib/xamplr/to-xml.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(out, persisting = false) ⇒ XMLPrinter

Returns a new instance of XMLPrinter.



7
8
9
10
11
12
13
14
15
16
# File 'lib/xamplr/to-xml.rb', line 7

def initialize(out, persisting=false)
  @out = out
  @persisting = persisting

  @ns_to_prefix = {}
  @start_body = ""
  @body = ""
  @attr_list = nil
  @mixed = 0
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



5
6
7
# File 'lib/xamplr/to-xml.rb', line 5

def body
  @body
end

#mixedObject

Returns the value of attribute mixed.



5
6
7
# File 'lib/xamplr/to-xml.rb', line 5

def mixed
  @mixed
end

#ns_to_prefixObject

Returns the value of attribute ns_to_prefix.



5
6
7
# File 'lib/xamplr/to-xml.rb', line 5

def ns_to_prefix
  @ns_to_prefix
end

#outObject

Returns the value of attribute out.



5
6
7
# File 'lib/xamplr/to-xml.rb', line 5

def out
  @out
end

#persistingObject

Returns the value of attribute persisting.



5
6
7
# File 'lib/xamplr/to-xml.rb', line 5

def persisting
  @persisting
end

#start_bodyObject

Returns the value of attribute start_body.



5
6
7
# File 'lib/xamplr/to-xml.rb', line 5

def start_body
  @start_body
end

Instance Method Details

#_content(text) ⇒ Object Also known as: content



126
127
128
129
130
131
132
133
134
# File 'lib/xamplr/to-xml.rb', line 126

def _content(text)
  if nil != text then
    if text.kind_of? XMLText then
      @body << text.to_xml(self)
    else
      @body << content_esc(text)
    end
  end
end

#attr_esc(s) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/xamplr/to-xml.rb', line 41

def attr_esc(s)
  if (s.kind_of? XamplObject)
    return attr_esc(s.to_xml)
  end

  result = s.to_s.dup
  #result = s.to_xml

  result.gsub!("&", "&amp;")
  result.gsub!("<", "&lt;")
  result.gsub!(">", "&gt;")
  result.gsub!("'", "&apos;")
  result.gsub!("\"", "&quot;")

  return result
end

#attribute(xampl) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/xamplr/to-xml.rb', line 70

def attribute(xampl)
  @attr_list = []
  if (nil != xampl.attributes) then
    xampl.attributes.each do |attr_spec|
      prefix = (2 < attr_spec.length) ? register_ns(attr_spec[2]) : ""
      value = xampl.instance_variable_get(attr_spec[0])
      @attr_list << " " << prefix << attr_spec[1] << "='" << attr_esc(value) << "'" \
            unless nil == value
    end
  end
end

#content_esc(s) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/xamplr/to-xml.rb', line 58

def content_esc(s)
  result = s.to_s.dup
  #result = s.to_xml

  return result if (s.kind_of? XamplObject)

  result.gsub!("&", "&amp;")
  result.gsub!("<", "&lt;")

  return result
end

#define_nsObject



146
147
148
149
150
151
152
# File 'lib/xamplr/to-xml.rb', line 146

def define_ns
  result = ""
  ns_to_prefix.each do |ns, prefix|
    result = sprintf("%s xmlns:%s='%s'", result, prefix[0..-2], ns)
  end
  return result
end

#doneObject



154
155
156
# File 'lib/xamplr/to-xml.rb', line 154

def done
  out << start_body << define_ns << body
end

#end_element(tag, ns, empty) ⇒ Object



142
143
144
# File 'lib/xamplr/to-xml.rb', line 142

def end_element(tag, ns, empty)
  @body << "</" << register_ns(ns) << tag << ">" if (!empty)
end

#end_root_element(tag, ns, empty) ⇒ Object



138
139
140
# File 'lib/xamplr/to-xml.rb', line 138

def end_root_element(tag, ns, empty)
  @body << "</" << register_ns(ns) << tag << ">" if (!empty)
end

#now_as_beforeObject



22
23
24
# File 'lib/xamplr/to-xml.rb', line 22

def now_as_before
  @mixed = @mixed - 1 if (0 < @mixed)
end

#now_as_mixedObject



18
19
20
# File 'lib/xamplr/to-xml.rb', line 18

def now_as_mixed
  @mixed = @mixed + 1
end

#persist_attribute(xampl) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/xamplr/to-xml.rb', line 82

def persist_attribute(xampl)
  @attr_list = []
  index = xampl.indexed_by[1..-1]
  if (nil != index) then
    value = xampl.get_the_index
    @attr_list << " " << index << "='" << attr_esc(value) << "'" if value
  end
end

#persisted_element(tag, ns) ⇒ Object



122
123
124
# File 'lib/xamplr/to-xml.rb', line 122

def persisted_element(tag, ns)
  @body << "<" << register_ns(ns) << tag << show_attributes << "/>"
end

#register_ns(ns) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/xamplr/to-xml.rb', line 26

def register_ns(ns)
  if (0 == ns.length) then
    return ""
  end

  prefix = ns_to_prefix[ns]
  if (nil == prefix) then
    preferred = XamplObject.lookup_preferred_ns_prefix(ns)
    prefix = "" << preferred << ":" if preferred
    prefix = "ns" << ns_to_prefix.size.to_s << ":" unless prefix
    ns_to_prefix[ns] = prefix
  end
  return prefix
end

#show_attributesObject



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/xamplr/to-xml.rb', line 91

def show_attributes
  if (nil == @attr_list) then
    return ""
  else
    result = @attr_list.join
    if (0 == result.length) then
      return ""
    else
      return result
    end
  end
end

#start_element(tag, ns, empty = false) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/xamplr/to-xml.rb', line 114

def start_element(tag, ns, empty=false)
  if (empty) then
    @body << "<" << register_ns(ns) << tag << show_attributes << "/>"
  else
    @body << "<" << register_ns(ns) << tag << show_attributes << ">"
  end
end

#start_root_element(tag, ns, empty = false) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/xamplr/to-xml.rb', line 104

def start_root_element(tag, ns, empty=false)
  if (empty) then
    @start_body << "<" << register_ns(ns) << tag << show_attributes
    @body = "/>"
  else
    @start_body << "<" << register_ns(ns) << tag << show_attributes
    @body = ">"
  end
end