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



242
243
244
# File 'lib/xamplr/persist-to-xml.rb', line 242

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



217
218
219
# File 'lib/xamplr/persist-to-xml.rb', line 217

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

#after_visit_mixed_content(xampl) ⇒ Object



230
231
232
# File 'lib/xamplr/persist-to-xml.rb', line 230

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

#attr_esc_fast(s) ⇒ Object Also known as: attr_esc



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/xamplr/persist-to-xml.rb', line 47

def attr_esc_fast(s)
  #NOTE -- there are known issues with using Ruby 1.9.1 and libxml-ruby, which this is using. Seems to mostly
  #        be related to DOM and XPATH but...
  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

#attr_esc_slow(s) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/xamplr/persist-to-xml.rb', line 60

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

#      stupid_test()

  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



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/xamplr/persist-to-xml.rb', line 105

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



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

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



207
208
209
210
211
212
213
214
215
# File 'lib/xamplr/persist-to-xml.rb', line 207

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



221
222
223
224
225
226
227
228
# File 'lib/xamplr/persist-to-xml.rb', line 221

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

#before_visit_simple_content(xampl) ⇒ Object



196
197
198
199
200
201
202
203
204
205
# File 'lib/xamplr/persist-to-xml.rb', line 196

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



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

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

#content_esc(s) ⇒ Object

TODO – use libxml for this too



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

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

  return result if (s.kind_of? XamplObject)

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

  return result
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



178
179
180
181
182
183
184
185
# File 'lib/xamplr/persist-to-xml.rb', line 178

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



187
188
189
# File 'lib/xamplr/persist-to-xml.rb', line 187

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

#end_element(xampl) ⇒ Object



172
173
174
175
176
# File 'lib/xamplr/persist-to-xml.rb', line 172

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

#persist_attribute(xampl) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/xamplr/persist-to-xml.rb', line 125

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



142
143
144
145
146
147
148
149
# File 'lib/xamplr/persist-to-xml.rb', line 142

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

#start_element(xampl) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/xamplr/persist-to-xml.rb', line 151

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



246
247
248
# File 'lib/xamplr/persist-to-xml.rb', line 246

def visit_string(string)
  @body << string
end