Class: Xampl::PersistXML

Inherits:
Visitor show all
Defined in:
lib/xamplr/persist-to-xml.rb

Instance Attribute Summary collapse

Attributes inherited from Visitor

#no_children, #no_siblings

Instance Method Summary collapse

Methods inherited from Visitor

#around_visit, #method_missing, #reset, #short_circuit, #start, #substitute_in_visit

Constructor Details

#initialize(out = "", mentions = nil, substitutions = {}) ⇒ PersistXML

Returns a new instance of PersistXML.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/xamplr/persist-to-xml.rb', line 8

def initialize(out="", mentions=nil, substitutions={})
  super()

  @out = out
  @was_attr = false

  @mentions = mentions
  @pid_substitutions = substitutions

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Xampl::Visitor

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



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

def body
  @body
end

#mentionsObject

Returns the value of attribute mentions.



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

def mentions
  @mentions
end

#ns_to_prefixObject

Returns the value of attribute ns_to_prefix.



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

def ns_to_prefix
  @ns_to_prefix
end

#outObject

Returns the value of attribute out.



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

def out
  @out
end

#start_bodyObject

Returns the value of attribute start_body.



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

def start_body
  @start_body
end

Instance Method Details

#after_visit(xampl) ⇒ Object



261
262
263
# File 'lib/xamplr/persist-to-xml.rb', line 261

def after_visit(xampl)
  xampl.after_visit_by_element_kind(self) if xampl.respond_to? "after_visit_by_element_kind"
end

#after_visit_data_content(xampl) ⇒ Object



236
237
238
# File 'lib/xamplr/persist-to-xml.rb', line 236

def after_visit_data_content(xampl)
  end_element(xampl) unless @no_children
end

#after_visit_mixed_content(xampl) ⇒ Object



249
250
251
# File 'lib/xamplr/persist-to-xml.rb', line 249

def after_visit_mixed_content(xampl)
  end_element(xampl) unless @no_children
end

#attr_esc(s) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/xamplr/persist-to-xml.rb', line 68

def attr_esc(s)
  # This depends on ruby 1.9
  return attr_esc(s.to_xml) if (s.kind_of? XamplObject)

  begin
    options = {
      :invalid => :replace,
      :undef=>:replace,
      :xml => :attr
    }
    result = s.to_s.dup.encode('UTF-8', 'UTF-8', options)

    return result
  rescue => e
    puts "Naughty Programmer! No! Bad!: #{ e } encoding in: #{ s.encoding }, out: #{ result.encoding }"
    puts e.backtrace
  end

  return ''
end

#attr_esc_slow(s) ⇒ Object

def attr_esc_fast(s)

end


54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/xamplr/persist-to-xml.rb', line 54

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

  result = s.to_s.dup

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

  return "\"result\""
end

#attribute(xampl) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/xamplr/persist-to-xml.rb', line 109

def attribute(xampl)
  @attr_list = []
  pattr = xampl.indexed_by.to_s

  if (nil != xampl.attributes) then
    xampl.attributes.each do |attr_spec|
      prefix = (2 < attr_spec.length) ? register_ns(attr_spec[2]) : ""
      value = nil
      if pattr == attr_spec[1] then
        value = @pid_substitutions[xampl]
#            puts "#{ File.basename __FILE__ }:#{ __LINE__ } [#{__method__}] xampl: #{ xampl }, substitute: #{ value }" if value
        value = xampl.instance_variable_get(attr_spec[0]) unless value
      else
        value = xampl.instance_variable_get(attr_spec[0])
      end
      @attr_list << (" " << prefix << attr_spec[1] << '=' << attr_esc(value)) unless nil == value
    end
  end
end

#before_visit(xampl) ⇒ Object



253
254
255
256
257
258
259
# File 'lib/xamplr/persist-to-xml.rb', line 253

def before_visit(xampl)
  if xampl.respond_to? "before_visit_by_element_kind" then
    xampl.before_visit_by_element_kind(self)
  else
    @body << xampl.to_s
  end
end

#before_visit_data_content(xampl) ⇒ Object



226
227
228
229
230
231
232
233
234
# File 'lib/xamplr/persist-to-xml.rb', line 226

def before_visit_data_content(xampl)
  start_element(xampl)
  if @no_children then
    @body << "/>"
  else
    @body << ">"
    @body << content_esc(xampl._content) if xampl._content
  end
end

#before_visit_mixed_content(xampl) ⇒ Object



240
241
242
243
244
245
246
247
# File 'lib/xamplr/persist-to-xml.rb', line 240

def before_visit_mixed_content(xampl)
  if @no_children then
    @body << "/>"
  else
    start_element(xampl)
    @body << ">"
  end
end

#before_visit_simple_content(xampl) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/xamplr/persist-to-xml.rb', line 200

def before_visit_simple_content(xampl)
  start_element(xampl)
  if @no_children then
    @body << "/>"
  else
    @body << ">"
    begin
      @body.concat(content_esc(xampl._content)) if xampl._content
    rescue => e
      begin
        s = xampl._content.force_encoding(@body.encoding)
        @body.concat(content_esc(s)) if xampl._content
      rescue => e
        puts "EXCEPTION: #{ e }"
        puts "body encoding: #{ @body.encoding }"
        puts "xampl._content encoding: #{ xampl._content.encoding }"
        puts "content_esc(xampl._content) encoding: #{ content_esc(xampl._content).encoding }"
        puts "xampl._content: [[[#{ xampl._content }]]]"
#          puts "body so far: [[[#{ @body }]]]"
        raise e
      end
    end
    end_element(xampl)
  end
end

#before_visit_without_content(xampl) ⇒ Object



195
196
197
198
# File 'lib/xamplr/persist-to-xml.rb', line 195

def before_visit_without_content(xampl)
  start_element(xampl)
  @body << "/>"
end

#content_esc(s) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/xamplr/persist-to-xml.rb', line 89

def content_esc(s)
  return content_esc(s.to_s.dup) if (s.kind_of? XamplObject)

  begin
    options = {
      :invalid => :replace,
      :undef=>:replace,
      :xml => :text
    }
    result = s.to_s.dup.encode('UTF-8', 'UTF-8', options)

    return result
  rescue => e
    puts "Naughty Programmer! No! Bad!: #{ e } encoding in: #{ s.encoding }, out: #{ result.encoding }"
    puts e.backtrace
  end

  return ''
end

#cycle(xampl) ⇒ Object

Raises:



23
24
25
26
# File 'lib/xamplr/persist-to-xml.rb', line 23

def cycle(xampl)
  raise XamplException.new(:cycle_detected_in_xampl_cluster) unless xampl.kind_of?(XamplPersistedObject)
  return true
end

#define_nsObject



182
183
184
185
186
187
188
189
# File 'lib/xamplr/persist-to-xml.rb', line 182

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

#doneObject



191
192
193
# File 'lib/xamplr/persist-to-xml.rb', line 191

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

#end_element(xampl) ⇒ Object



176
177
178
179
180
# File 'lib/xamplr/persist-to-xml.rb', line 176

def end_element(xampl)
  tag = xampl.tag
  ns = xampl.ns
  @body << "</" << register_ns(ns) << tag << ">"
end

#persist_attribute(xampl) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/xamplr/persist-to-xml.rb', line 129

def persist_attribute(xampl)
  @attr_list = []
  pattr = xampl.indexed_by.to_s
  if (nil != xampl.attributes) then
    xampl.attributes.each do |attr_spec|
      if pattr == attr_spec[1] then
        prefix = (2 < attr_spec.length) ? register_ns(attr_spec[2]) : ""
        value = @pid_substitutions[xampl]
#            puts "#{ File.basename __FILE__ }:#{ __LINE__ } [#{__method__}] xampl: #{ xampl }, substitute: #{ value }" if value
        value = xampl.instance_variable_get(attr_spec[0]) unless value
        @attr_list << (" " << prefix << attr_spec[1] << '=' << attr_esc(value)) unless nil == value
        break
      end
    end
  end
end

#register_ns(ns) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/xamplr/persist-to-xml.rb', line 32

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

#revisit(xampl) ⇒ Object



28
29
30
# File 'lib/xamplr/persist-to-xml.rb', line 28

def revisit(xampl)
  return true
end

#show_attributesObject



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

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

#start_element(xampl) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/xamplr/persist-to-xml.rb', line 155

def start_element(xampl)
  tag = xampl.tag
  ns = xampl.ns
  tag_info = "" << "<" << register_ns(ns) << tag
  unless @start_body then
    attribute(xampl)
    attr_defn = show_attributes
    @start_body = "" << tag_info << attr_defn
    @was_attr = true if 0 < attr_defn.size
  else
    if xampl.persist_required then
      @mentions << xampl if @mentions
      @no_children = true
      persist_attribute(xampl)
    else
      attribute(xampl)
    end
    @body << tag_info << show_attributes
  end
end

#visit_string(string) ⇒ Object



265
266
267
# File 'lib/xamplr/persist-to-xml.rb', line 265

def visit_string(string)
  @body << string
end