Class: Xampl::PrettyXML

Inherits:
Visitor show all
Defined in:
lib/xamplr/visitors.rb

Constant Summary collapse

@@compact =
true

Instance Attribute Summary collapse

Attributes inherited from Visitor

#no_children, #no_siblings

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Visitor

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

Constructor Details

#initialize(out = "", skip = []) ⇒ PrettyXML

Returns a new instance of PrettyXML.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/xamplr/visitors.rb', line 91

def initialize(out="", skip=[])
  super()

  @out               = out
  @indent            = ""
  @indent_step       = "  "
  @start_attr_indent = ""
  @was_attr          = false

  @depth             = 0

  @skip              = {}
  skip.each { |ns|
    @skip[ns] = ns
  }

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

  @insert_comment    = 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.



78
79
80
# File 'lib/xamplr/visitors.rb', line 78

def body
  @body
end

#indentObject

Returns the value of attribute indent.



79
80
81
# File 'lib/xamplr/visitors.rb', line 79

def indent
  @indent
end

#indent_stepObject

Returns the value of attribute indent_step.



79
80
81
# File 'lib/xamplr/visitors.rb', line 79

def indent_step
  @indent_step
end

#ns_to_prefixObject

Returns the value of attribute ns_to_prefix.



78
79
80
# File 'lib/xamplr/visitors.rb', line 78

def ns_to_prefix
  @ns_to_prefix
end

#outObject

Returns the value of attribute out.



78
79
80
# File 'lib/xamplr/visitors.rb', line 78

def out
  @out
end

#start_bodyObject

Returns the value of attribute start_body.



78
79
80
# File 'lib/xamplr/visitors.rb', line 78

def start_body
  @start_body
end

Class Method Details

.compactObject



83
84
85
# File 'lib/xamplr/visitors.rb', line 83

def PrettyXML.compact
  @@compact
end

.compact=(v) ⇒ Object



87
88
89
# File 'lib/xamplr/visitors.rb', line 87

def PrettyXML.compact=(v)
  @@compact = v
end

Instance Method Details

#after_visit(xampl) ⇒ Object



319
320
321
# File 'lib/xamplr/visitors.rb', line 319

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



291
292
293
294
# File 'lib/xamplr/visitors.rb', line 291

def after_visit_data_content(xampl)
  @depth += -1
  end_element(xampl)
end

#after_visit_mixed_content(xampl) ⇒ Object



302
303
304
305
# File 'lib/xamplr/visitors.rb', line 302

def after_visit_mixed_content(xampl)
  @depth -= 1
  end_element(xampl)
end

#attr_esc(s) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/xamplr/visitors.rb', line 147

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

  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



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/xamplr/visitors.rb', line 174

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

#before_visit(xampl) ⇒ Object



307
308
309
310
311
312
313
314
315
316
317
# File 'lib/xamplr/visitors.rb', line 307

def before_visit(xampl)
  unless xampl.kind_of?(XamplObject) and @skip[xampl.ns] then
    if xampl.respond_to? "before_visit_by_element_kind" then
      xampl.before_visit_by_element_kind(self)
    else
      @body << xampl.to_s
    end
  else
    @no_children = true
  end
end

#before_visit_data_content(xampl) ⇒ Object



284
285
286
287
288
289
# File 'lib/xamplr/visitors.rb', line 284

def before_visit_data_content(xampl)
  start_element(xampl)
  @body << ">"
  @body << content_esc(xampl._content) if xampl._content
  @depth += 1
end

#before_visit_mixed_content(xampl) ⇒ Object



296
297
298
299
300
# File 'lib/xamplr/visitors.rb', line 296

def before_visit_mixed_content(xampl)
  start_element(xampl)
  @body << ">"
  @depth += 1
end

#before_visit_simple_content(xampl) ⇒ Object



277
278
279
280
281
282
# File 'lib/xamplr/visitors.rb', line 277

def before_visit_simple_content(xampl)
  start_element(xampl)
  @body << ">"
  @body << content_esc(xampl._content) if xampl._content
  end_element(xampl)
end

#before_visit_without_content(xampl) ⇒ Object



272
273
274
275
# File 'lib/xamplr/visitors.rb', line 272

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

#content_esc(s) ⇒ Object



163
164
165
166
167
168
169
170
171
172
# File 'lib/xamplr/visitors.rb', line 163

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

  return result if (s.kind_of? XamplObject)

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

  return result
end

#cycle(xampl) ⇒ Object



120
121
122
123
124
# File 'lib/xamplr/visitors.rb', line 120

def cycle(xampl)
  @short_circuit  = true
  @insert_comment = "<!-- CYCLE -->"
  return true
end

#define_nsObject



258
259
260
261
262
263
264
265
266
# File 'lib/xamplr/visitors.rb', line 258

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

#do_indentObject



219
220
221
# File 'lib/xamplr/visitors.rb', line 219

def do_indent
  return "\n" << @indent << (@indent_step * @depth)
end

#doneObject



268
269
270
# File 'lib/xamplr/visitors.rb', line 268

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

#end_element(xampl) ⇒ Object



248
249
250
251
252
253
254
255
256
# File 'lib/xamplr/visitors.rb', line 248

def end_element(xampl)
  tag = xampl.tag
  ns  = xampl.ns
  if @@compact then
    @body << "</" << register_ns(ns) << tag << ">"
  else
    @body << do_indent << "</" << register_ns(ns) << tag << ">"
  end
end

#persist_attribute(xampl) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/xamplr/visitors.rb', line 192

def persist_attribute(xampl)
  @attr_list = []
  if xampl.persist_required then
    index = xampl.indexed_by.to_s
    if index then
      value = xampl.get_the_index
      @attr_list << (" " << index << "='" << attr_esc(value) << "'") if value
    end
  else
    attribute(xampl)
#@attr_list << " xampl:wtf='WTF??'"
  end
end

#register_ns(ns) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/xamplr/visitors.rb', line 132

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



126
127
128
129
130
# File 'lib/xamplr/visitors.rb', line 126

def revisit(xampl)
  @insert_comment = "<!-- You've seen this before -->"
  #body << "<!-- You've seen this before -->"
  return true
end

#short_circuitObject



115
116
117
118
# File 'lib/xamplr/visitors.rb', line 115

def short_circuit
  body << @insert_comment if @insert_comment
  @insert_comment = nil
end

#show_attributes(attr_indent) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/xamplr/visitors.rb', line 206

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

#start_element(xampl) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/xamplr/visitors.rb', line 223

def start_element(xampl)
  xampl.accessed

  if @revisiting or @cycling then
    @short_circuit = true
    persist_attribute(xampl)
  else
    attribute(xampl)
  end

  tag         = xampl.tag
  ns          = xampl.ns
  indent      = do_indent
  tag_info    = "" << "<" << register_ns(ns) << tag
  attr_indent = "" << indent << (" " * tag_info.size)
  unless @start_body then
    @start_attr_indent = attr_indent
    attr_defn          = show_attributes(attr_indent)
    @start_body        = "" << indent << tag_info << attr_defn
    @was_attr = true if 0 < attr_defn.size
  else
    @body << indent << tag_info << show_attributes(attr_indent)
  end
end

#visit_string(string) ⇒ Object



323
324
325
# File 'lib/xamplr/visitors.rb', line 323

def visit_string(string)
  @body << string
end