Class: Xampl::FromXML_original

Inherits:
Xampl_PP
  • Object
show all
Defined in:
lib/xamplr/from-xml-orig.rb

Constant Summary collapse

@@by_tag =
{}
@@by_ns_tag =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(recovering = false) ⇒ FromXML_original

Returns a new instance of FromXML_original.



14
15
16
17
# File 'lib/xamplr/from-xml-orig.rb', line 14

def initialize(recovering=false)
  super()
  @recovering = recovering
end

Instance Attribute Details

#checkWellFormedObject (readonly)

Returns the value of attribute checkWellFormed.



7
8
9
# File 'lib/xamplr/from-xml-orig.rb', line 7

def checkWellFormed
  @checkWellFormed
end

#is_realisingObject (readonly)

Returns the value of attribute is_realising.



8
9
10
# File 'lib/xamplr/from-xml-orig.rb', line 8

def is_realising
  @is_realising
end

#tokenise_contentObject (readonly)

Returns the value of attribute tokenise_content.



9
10
11
# File 'lib/xamplr/from-xml-orig.rb', line 9

def tokenise_content
  @tokenise_content
end

Class Method Details

.register(tag, ns_tag, klass) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/xamplr/from-xml-orig.rb', line 24

def FromXML_original.register(tag, ns_tag, klass)
  @@by_ns_tag[ns_tag] = [ klass ]
  a = @@by_tag[tag]
  if (nil == a) then
    @@by_tag[tag] = [ klass ]
  else
    found = false
    a.each { | thing | found = found | (thing == klass) }
    a << klass unless found
  end
end

.registered(name) ⇒ Object



36
37
38
39
40
41
# File 'lib/xamplr/from-xml-orig.rb', line 36

def FromXML_original.registered(name)
  klass = @@by_ns_tag[name]
  klass = @@by_tag[name] unless klass
  klass = [] unless klass
  return klass
end

.reset_registryObject



19
20
21
22
# File 'lib/xamplr/from-xml-orig.rb', line 19

def FromXML_original.reset_registry
  @@by_tag = {}
  @@by_ns_tag = {}
end

.tokenise_string(str, strip = true) ⇒ Object



99
100
101
102
103
104
# File 'lib/xamplr/from-xml-orig.rb', line 99

def FromXML_original.tokenise_string(str, strip=true)
  return nil unless str
  str.strip! if strip
  str.gsub!(/[ \n\r\t][ \n\r\t]*/, " ")
  return str
end

Instance Method Details

#attributeCountObject



314
315
316
# File 'lib/xamplr/from-xml-orig.rb', line 314

def attributeCount
  return @attributeName.length
end

#attributeName(i) ⇒ Object



318
319
320
# File 'lib/xamplr/from-xml-orig.rb', line 318

def attributeName(i)
  return @attributeName[i]
end

#attributeNamespace(i) ⇒ Object



322
323
324
# File 'lib/xamplr/from-xml-orig.rb', line 322

def attributeNamespace(i)
  return @attributeNamespace[i]
end

#attributePrefix(i) ⇒ Object



330
331
332
# File 'lib/xamplr/from-xml-orig.rb', line 330

def attributePrefix(i)
  return @attributePrefix[i]
end

#attributeQName(i) ⇒ Object



326
327
328
# File 'lib/xamplr/from-xml-orig.rb', line 326

def attributeQName(i)
  return @attributeQName[i]
end

#attributeValue(i) ⇒ Object



334
335
336
# File 'lib/xamplr/from-xml-orig.rb', line 334

def attributeValue(i)
  return @attributeValue[i]
end

#columnObject



346
347
348
# File 'lib/xamplr/from-xml-orig.rb', line 346

def column
  return column
end

#depthObject



338
339
340
# File 'lib/xamplr/from-xml-orig.rb', line 338

def depth
  return depth
end

#lineObject



342
343
344
# File 'lib/xamplr/from-xml-orig.rb', line 342

def line
  return line
end

#next_interesting_eventObject



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/xamplr/from-xml-orig.rb', line 278

def next_interesting_event
  if (endDocument?) then
    return Xampl_PP::END_DOCUMENT
  end

  boring = true
  while boring do
    event = nextEvent
    case event
      when Xampl_PP::START_DOCUMENT
        boring = true
      when Xampl_PP::END_DOCUMENT
        boring = false
      when Xampl_PP::START_ELEMENT
        boring = false
      when Xampl_PP::END_ELEMENT
        boring = false
      when Xampl_PP::TEXT
        boring = false
      when Xampl_PP::CDATA_SECTION
        boring = false
      when Xampl_PP::ENTITY_REF
        boring = false
      when Xampl_PP::IGNORABLE_WHITESPACE
        boring = true
      when Xampl_PP::PROCESSING_INSTRUCTION
        boring = true
      when Xampl_PP::COMMENT
        boring = true
      when Xampl_PP::DOCTYPE
        boring = true
    end
  end
  return event
end

#parse(filename, tokenise_content = true, is_realising = false) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/xamplr/from-xml-orig.rb', line 71

def parse(filename, tokenise_content=true, is_realising=false)
  begin
    setup_parse(filename, tokenise_content, is_realising)
    element, ignore = parse_element
    return element
  rescue Exception => e
    puts "trouble parsing file: '#{filename}'"
    puts "Exception: #{e}"
    raise
  end
end

#parse_element(parent = nil, target = nil) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/xamplr/from-xml-orig.rb', line 106

def parse_element(parent=nil, target=nil)
  next_interesting_event unless parent

  existing_element = nil
  element = nil

  requires_caching = false

  if startElement? then
    if ((nil != @namespace) and (0 < @namespace.size)) then
      klass_name = "{#{@namespace}}#{@name}"
      klasses = FromXML_original.registered(klass_name)
      if (0 == klasses.size) then
        xml_text = XMLText.new
        xml_text.build(self)
        xml_text = parent.note_adding_text_content(xml_text, @is_realising)
        parent.add_content(xml_text, @tokenise_content) if xml_text
# puts "#{__LINE__ }:: add_content [#{xml_text}] --> [#{parent.content}]"
        return xml_text, false
      end
      if (1 < klasses.size) then
        raise XamplException.new("there is more than one '#{@name}' tag in namespace '#{@namespace}'\nplease report this error")
      end
    else
      klasses = FromXML_original.registered(@name)
      if (0 == klasses.size) then
        raise XamplException.new("do not recognise tag '#{@name}' (no namespace specified)")
      end
      if (1 < klasses.size) then
        raise XamplException.new("there is more than one '#{@name}' tag (no namespace specified)")
      end
    end

    unless @is_realising then
      @attributeValue.size.times do |i|
        FromXML_original.tokenise_string @attributeValue[i]
      end
    end

    if target then
      element = target
      target.load_needed = false
      target = nil
      element.init_attributes(@attributeName, @attributeNamespace, @attributeValue)
      element.note_attributes_initialised(@is_realising)
    else
      if klasses[0].persisted? then
        @attributeName.each_index do |i|
          if @attributeName[i] == klasses[0].persisted?.to_s then
            existing_element = Xampl.find_known(klasses[0], @attributeValue[i])
            if existing_element then
              # so we've found the element. Now what??? We can do several
              # reasonable things:
              #
              #    1) continue parsing into the found element
              #    2) simply return the found element
              #    3) replace the found element with the new element
              #
              # The first one is dubious, so we won't.
              # The second and third option both make complete sense
              #
              # We are going to do the second
              #
              # BTW, 'existing element' means a representation of this element already in memory
              # puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
              # puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
              # puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
              # puts "FOUND AN EXISTING THING... #{ klasses[0] } #{ @attributeValue[i] }"
              # puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
              # puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
              # puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
              # caller(0).each { | trace | puts "  #{trace}"}
              #                   existing_element.reset_contents
              #                   element = existing_element
              #                   existing_element = nil
              #                  puts "#{File.basename(__FILE__)} #{__LINE__} EXISTING ELEMENT: #{ existing_element }"
              #                  puts "#{File.basename(__FILE__)} #{__LINE__} WOW, must handle the existing element correctly"
              element = existing_element #TODO -- IS THIS RIGHT????????????????????????
            end
            unless element then
              element = klasses[0].new
              requires_caching = @recovering
#                  puts "#{File.basename(__FILE__)} #{__LINE__} WOW, what about recovering????"
              #TODO -- IS THIS RIGHT????????????????????????
              requires_caching = true #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
              unless @recovering then
                element.force_load if parent
              end
              element.note_created(@is_realising)
            end

            break
          end
        end
      end

      unless element then
        element = klasses[0].new
        element.note_created(@is_realising)
      end

      element.note_initialise_attributes_with(@attributeName, @attributeNamespace, @attributeValue, @is_realising)
      element.init_attributes(@attributeName, @attributeNamespace, @attributeValue)
      element.note_attributes_initialised(@is_realising)

      if requires_caching and element and element.persist_required then
        # puts "ELEMENT: #{element}, #{element.class.name}"
        Xampl.cache(element)
        # found = Xampl.find_known(element.class, element.get_the_index)
        # puts "OK? #{found == element} found: #{found}, element: #{element}"
        # puts "=============================================================================="
      end

      #element = element.note_add_to_parent(parent, @is_realising)
      #element.append_to(parent) if parent
    end

    while not endDocument?
      case nextEvent
        when START_DOCUMENT
          return element if @recovering
          return existing_element || element
        when END_DOCUMENT
          return element if @recovering
          return existing_element || element
        when START_ELEMENT
          child, ignore_child = parse_element(element)
          unless ignore_child then
            case child
              when XamplObject then
                child = child.note_add_to_parent(element, @is_realising) if child
                child = element.note_add_child(child, @is_realising) if element
                child.append_to(element) if element and child
              when XMLText then
                puts "UNRECOGNISED Well-formed XML: #{child.to_s[0..25]}..."
              else
                puts "WHAT IS THIS??? #{child.class.name}"
            end
#                 if child.kind_of? XamplObject then
            #                   child = child.note_add_to_parent(element, @is_realising) if child
            #                   child = element.note_add_child(child, @is_realising) if element
            #                   child.append_to(element) if element and child
            #                 else
            #                   puts "WHAT IS THIS??? #{child.class.name}"
            #                 end
          end
        when END_ELEMENT
          element = element.note_closed(@is_realising)
          return element if @recovering
          return existing_element || element
        when TEXT, CDATA_SECTION, ENTITY_REF
          if element.has_mixed_content then
            the_text = element.note_adding_text_content(@text, @is_realising)
            # element.add_content(the_text, @tokenise_content)
            element << the_text
# puts "#{__LINE__ }:: add_content [#{the_text}]"
          else
            unless whitespace? then
              the_text = element.note_adding_text_content(@text, @is_realising)
              # 16 Mar 2007 -- this was making preformatted text content impossible
              # element.add_content(the_text, @tokenise_content)
              element.add_content(the_text, false)
# puts "#{__LINE__ }:: add_content [#{the_text}] --> [#{element.content}]"
            end
          end
      end
    end
  end
  return element if @recovering
  return existing_element || element
end

#parse_string(string, tokenise_content = true, is_realising = false, target = nil) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/xamplr/from-xml-orig.rb', line 87

def parse_string(string, tokenise_content=true, is_realising=false, target=nil)
  begin
    setup_parse_string(string, tokenise_content, is_realising)
    element, ignore = parse_element(nil, target)
    return element
  rescue Exception => e
    puts "trouble parsing string: '#{string}'"
    puts "Exception: #{e}"
    raise
  end
end

#realise_string(string, tokenise_content = true, target = nil) ⇒ Object



83
84
85
# File 'lib/xamplr/from-xml-orig.rb', line 83

def realise_string(string, tokenise_content=true, target=nil)
  return parse_string(string, tokenise_content, true, target)
end

#resolve(name) ⇒ Object



43
44
45
# File 'lib/xamplr/from-xml-orig.rb', line 43

def resolve(name)
  return name
end

#setup_parse(filename, tokenise_content = true, is_realising = false) ⇒ Object



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

def setup_parse(filename, tokenise_content=true, is_realising=false)
  @processNamespace = true
  @reportNamespaceAttributes = false
  @checkWellFormed = false
  @resolver = self

  @is_realising = is_realising
  @tokenise_content = tokenise_content

  setInput(File.new(filename))
end

#setup_parse_string(string, tokenise_content = true, is_realising = false) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/xamplr/from-xml-orig.rb', line 59

def setup_parse_string(string, tokenise_content=true, is_realising=false)
  @processNamespace = true
  @reportNamespaceAttributes = false
  @checkWellFormed = false
  @resolver = self

  @is_realising = is_realising
  @tokenise_content = tokenise_content

  setInput(string)
end