Class: Xampl::PrettyXML
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.
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/xamplr/visitors.rb', line 68
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
#body ⇒ Object
Returns the value of attribute body.
55
56
57
|
# File 'lib/xamplr/visitors.rb', line 55
def body
@body
end
|
#indent ⇒ Object
Returns the value of attribute indent.
56
57
58
|
# File 'lib/xamplr/visitors.rb', line 56
def indent
@indent
end
|
#indent_step ⇒ Object
Returns the value of attribute indent_step.
56
57
58
|
# File 'lib/xamplr/visitors.rb', line 56
def indent_step
@indent_step
end
|
#ns_to_prefix ⇒ Object
Returns the value of attribute ns_to_prefix.
55
56
57
|
# File 'lib/xamplr/visitors.rb', line 55
def ns_to_prefix
@ns_to_prefix
end
|
#out ⇒ Object
Returns the value of attribute out.
55
56
57
|
# File 'lib/xamplr/visitors.rb', line 55
def out
@out
end
|
#start_body ⇒ Object
Returns the value of attribute start_body.
55
56
57
|
# File 'lib/xamplr/visitors.rb', line 55
def start_body
@start_body
end
|
Class Method Details
.compact ⇒ Object
60
61
62
|
# File 'lib/xamplr/visitors.rb', line 60
def PrettyXML.compact
@@compact
end
|
.compact=(v) ⇒ Object
64
65
66
|
# File 'lib/xamplr/visitors.rb', line 64
def PrettyXML.compact=(v)
@@compact = v
end
|
Instance Method Details
#after_visit(xampl) ⇒ Object
296
297
298
|
# File 'lib/xamplr/visitors.rb', line 296
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
268
269
270
271
|
# File 'lib/xamplr/visitors.rb', line 268
def after_visit_data_content(xampl)
@depth += -1
end_element(xampl)
end
|
#after_visit_mixed_content(xampl) ⇒ Object
279
280
281
282
|
# File 'lib/xamplr/visitors.rb', line 279
def after_visit_mixed_content(xampl)
@depth -= 1
end_element(xampl)
end
|
#attr_esc(s) ⇒ Object
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/xamplr/visitors.rb', line 124
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!("<", "<")
result.gsub!(">", ">")
result.gsub!("'", "'")
result.gsub!("\"", """)
return result
end
|
#attribute(xampl) ⇒ Object
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
# File 'lib/xamplr/visitors.rb', line 151
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
end
|
#before_visit(xampl) ⇒ Object
284
285
286
287
288
289
290
291
292
293
294
|
# File 'lib/xamplr/visitors.rb', line 284
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
261
262
263
264
265
266
|
# File 'lib/xamplr/visitors.rb', line 261
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
273
274
275
276
277
|
# File 'lib/xamplr/visitors.rb', line 273
def before_visit_mixed_content(xampl)
start_element(xampl)
@body << ">"
@depth += 1
end
|
#before_visit_simple_content(xampl) ⇒ Object
254
255
256
257
258
259
|
# File 'lib/xamplr/visitors.rb', line 254
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
249
250
251
252
|
# File 'lib/xamplr/visitors.rb', line 249
def before_visit_without_content(xampl)
start_element(xampl)
@body << "/>"
end
|
#content_esc(s) ⇒ Object
140
141
142
143
144
145
146
147
148
149
|
# File 'lib/xamplr/visitors.rb', line 140
def content_esc(s)
result = s.to_s.dup
return result if (s.kind_of? XamplObject)
result.gsub!("&", "&")
result.gsub!("<", "<")
return result
end
|
#cycle(xampl) ⇒ Object
97
98
99
100
101
|
# File 'lib/xamplr/visitors.rb', line 97
def cycle(xampl)
@short_circuit = true
@insert_comment = "<!-- CYCLE -->"
return true
end
|
#define_ns ⇒ Object
235
236
237
238
239
240
241
242
243
|
# File 'lib/xamplr/visitors.rb', line 235
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_indent ⇒ Object
196
197
198
|
# File 'lib/xamplr/visitors.rb', line 196
def do_indent
return "\n" << @indent << (@indent_step * @depth)
end
|
#done ⇒ Object
245
246
247
|
# File 'lib/xamplr/visitors.rb', line 245
def done
out << @start_body << define_ns << @body
end
|
#end_element(xampl) ⇒ Object
225
226
227
228
229
230
231
232
233
|
# File 'lib/xamplr/visitors.rb', line 225
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
169
170
171
172
173
174
175
176
177
178
179
180
181
|
# File 'lib/xamplr/visitors.rb', line 169
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)
end
end
|
#register_ns(ns) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/xamplr/visitors.rb', line 109
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
103
104
105
106
107
|
# File 'lib/xamplr/visitors.rb', line 103
def revisit(xampl)
@insert_comment = "<!-- You've seen this before -->"
return true
end
|
#short_circuit ⇒ Object
92
93
94
95
|
# File 'lib/xamplr/visitors.rb', line 92
def short_circuit
body << @insert_comment if @insert_comment
@insert_comment = nil
end
|
#show_attributes(attr_indent) ⇒ Object
183
184
185
186
187
188
189
190
191
192
193
194
|
# File 'lib/xamplr/visitors.rb', line 183
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
# File 'lib/xamplr/visitors.rb', line 200
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
300
301
302
|
# File 'lib/xamplr/visitors.rb', line 300
def visit_string(string)
@body << string
end
|