Class: Xampl::PersistXML
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) ⇒ PersistXML
Returns a new instance of PersistXML.
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/xamplr/persist-to-xml.rb', line 8
def initialize(out="", mentions=nil)
super()
@out = out
@was_attr = false
@mentions = mentions
@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
#body ⇒ Object
Returns the value of attribute body.
6
7
8
|
# File 'lib/xamplr/persist-to-xml.rb', line 6
def body
@body
end
|
#mentions ⇒ Object
Returns the value of attribute mentions.
6
7
8
|
# File 'lib/xamplr/persist-to-xml.rb', line 6
def mentions
@mentions
end
|
#ns_to_prefix ⇒ Object
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
|
#out ⇒ Object
Returns the value of attribute out.
6
7
8
|
# File 'lib/xamplr/persist-to-xml.rb', line 6
def out
@out
end
|
#start_body ⇒ Object
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
233
234
235
|
# File 'lib/xamplr/persist-to-xml.rb', line 233
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
208
209
210
|
# File 'lib/xamplr/persist-to-xml.rb', line 208
def after_visit_data_content(xampl)
end_element(xampl) unless @no_children
end
|
#after_visit_mixed_content(xampl) ⇒ Object
221
222
223
|
# File 'lib/xamplr/persist-to-xml.rb', line 221
def after_visit_mixed_content(xampl)
end_element(xampl) unless @no_children
end
|
#attr_esc(s) ⇒ Object
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/xamplr/persist-to-xml.rb', line 46
def attr_esc(s)
unless defined?(@@doc) then
@@doc = LibXML::XML::Document.new()
@@doc.root = LibXML::XML::Node.new('r')
@@attr = LibXML::XML::Attr.new(@@doc.root, 'v', 'v')
end
@@attr.value = s.to_s
(@@doc.root.to_s)[6..-4]
end
|
#attribute(xampl) ⇒ Object
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/xamplr/persist-to-xml.rb', line 105
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
|
#before_visit(xampl) ⇒ Object
225
226
227
228
229
230
231
|
# File 'lib/xamplr/persist-to-xml.rb', line 225
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
198
199
200
201
202
203
204
205
206
|
# File 'lib/xamplr/persist-to-xml.rb', line 198
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
212
213
214
215
216
217
218
219
|
# File 'lib/xamplr/persist-to-xml.rb', line 212
def before_visit_mixed_content(xampl)
if @no_children then
@body << "/>"
else
start_element(xampl)
@body << ">"
end
end
|
#before_visit_simple_content(xampl) ⇒ Object
187
188
189
190
191
192
193
194
195
196
|
# File 'lib/xamplr/persist-to-xml.rb', line 187
def before_visit_simple_content(xampl)
start_element(xampl)
if @no_children then
@body << "/>"
else
@body << ">"
@body << content_esc(xampl._content) if xampl._content
end_element(xampl)
end
end
|
#before_visit_without_content(xampl) ⇒ Object
182
183
184
185
|
# File 'lib/xamplr/persist-to-xml.rb', line 182
def before_visit_without_content(xampl)
start_element(xampl)
@body << "/>"
end
|
#content_esc(s) ⇒ Object
TODO – use libxml for this too
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/xamplr/persist-to-xml.rb', line 94
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
22
23
24
25
|
# File 'lib/xamplr/persist-to-xml.rb', line 22
def cycle(xampl)
raise XamplException.new(:cycle_detected_in_xampl_cluster) unless xampl.kind_of?(XamplPersistedObject)
return true
end
|
#define_ns ⇒ Object
169
170
171
172
173
174
175
176
|
# File 'lib/xamplr/persist-to-xml.rb', line 169
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
|
#done ⇒ Object
178
179
180
|
# File 'lib/xamplr/persist-to-xml.rb', line 178
def done
out << @start_body << define_ns << @body
end
|
#end_element(xampl) ⇒ Object
163
164
165
166
167
|
# File 'lib/xamplr/persist-to-xml.rb', line 163
def end_element(xampl)
tag = xampl.tag
ns = xampl.ns
@body << "</" << register_ns(ns) << tag << ">"
end
|
#persist_attribute(xampl) ⇒ Object
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/xamplr/persist-to-xml.rb', line 117
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 = xampl.instance_variable_get(attr_spec[0])
@attr_list << (" " << prefix << attr_spec[1] << '="' << attr_esc(value) << '"') unless nil == value
break
end
end
end
end
|
#register_ns(ns) ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/xamplr/persist-to-xml.rb', line 31
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
27
28
29
|
# File 'lib/xamplr/persist-to-xml.rb', line 27
def revisit(xampl)
return true
end
|
#show_attributes ⇒ Object
133
134
135
136
137
138
139
140
|
# File 'lib/xamplr/persist-to-xml.rb', line 133
def show_attributes
result = @attr_list.join(" ")
if (0 == result.length) then
return ""
else
return result
end
end
|
#start_element(xampl) ⇒ Object
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/xamplr/persist-to-xml.rb', line 142
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
237
238
239
|
# File 'lib/xamplr/persist-to-xml.rb', line 237
def visit_string(string)
@body << string
end
|