Class: Xampl::CopyXML

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

Constant Summary collapse

@@base_pid =
Time.now.to_i.to_s + "_"
@@gen_pid =
0

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, #initialize, #method_missing, #reset, #short_circuit, #start, #substitute_in_visit

Constructor Details

This class inherits a constructor from Xampl::Visitor

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.



306
307
308
# File 'lib/xamplr/visitors.rb', line 306

def body
  @body
end

#ns_to_prefixObject

Returns the value of attribute ns_to_prefix.



306
307
308
# File 'lib/xamplr/visitors.rb', line 306

def ns_to_prefix
  @ns_to_prefix
end

#start_bodyObject

Returns the value of attribute start_body.



306
307
308
# File 'lib/xamplr/visitors.rb', line 306

def start_body
  @start_body
end

Class Method Details

.copy(root, translate_pids = {}) ⇒ Object



308
309
310
# File 'lib/xamplr/visitors.rb', line 308

def CopyXML.copy(root, translate_pids={})
  CopyXML.new.make_copy(root, translate_pids)
end

Instance Method Details

#after_visit(xampl) ⇒ Object



549
550
551
# File 'lib/xamplr/visitors.rb', line 549

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



524
525
526
# File 'lib/xamplr/visitors.rb', line 524

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

#after_visit_mixed_content(xampl) ⇒ Object



537
538
539
# File 'lib/xamplr/visitors.rb', line 537

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

#attr_esc(s) ⇒ Object



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/xamplr/visitors.rb', line 396

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



423
424
425
426
427
428
429
430
431
432
433
# File 'lib/xamplr/visitors.rb', line 423

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

#before_visit(xampl) ⇒ Object



541
542
543
544
545
546
547
# File 'lib/xamplr/visitors.rb', line 541

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



514
515
516
517
518
519
520
521
522
# File 'lib/xamplr/visitors.rb', line 514

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



528
529
530
531
532
533
534
535
# File 'lib/xamplr/visitors.rb', line 528

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

#before_visit_simple_content(xampl) ⇒ Object



503
504
505
506
507
508
509
510
511
512
# File 'lib/xamplr/visitors.rb', line 503

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



498
499
500
501
# File 'lib/xamplr/visitors.rb', line 498

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

#content_esc(s) ⇒ Object



412
413
414
415
416
417
418
419
420
421
# File 'lib/xamplr/visitors.rb', line 412

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

#copy_xampl(root) ⇒ Object



348
349
350
# File 'lib/xamplr/visitors.rb', line 348

def copy_xampl(root)
  start(root).done
end

#cycle(xampl) ⇒ Object

Raises:



372
373
374
375
# File 'lib/xamplr/visitors.rb', line 372

def cycle(xampl)
  raise XamplException.new(:cycle_detected_in_xampl_cluster) unless xampl.kind_of?(XamplPersistedObject)
  return true
end

#define_nsObject



486
487
488
489
490
491
492
# File 'lib/xamplr/visitors.rb', line 486

def define_ns
  result = ""
  ns_to_prefix.each{ | ns, prefix |
    result = sprintf("%s xmlns:%s='%s'", result, prefix[0..-2], ns)
  }
  return result
end

#doneObject



494
495
496
# File 'lib/xamplr/visitors.rb', line 494

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

#end_element(xampl) ⇒ Object



480
481
482
483
484
# File 'lib/xamplr/visitors.rb', line 480

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

#get_the_new_pid(xampl) ⇒ Object



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/xamplr/visitors.rb', line 355

def get_the_new_pid(xampl)
  current_pid = xampl.get_the_index
  @persisted_xampl_found[current_pid] = xampl

  new_pid = @pid_translations_old_to_new[current_pid]

  unless new_pid then
    @@gen_pid += 1
    new_pid = @@base_pid + @@gen_pid.to_s

    @pid_translations_old_to_new[current_pid] = new_pid
    @pid_translations_new_to_old[new_pid] = current_pid
  end

  return new_pid
end

#make_copy(root, translate_pids) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/xamplr/visitors.rb', line 312

def make_copy(root, translate_pids)
  @was_attr = false

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

  @pid_translations_old_to_new = translate_pids
  @pid_translations_new_to_old = translate_pids.invert

  @persisted_xampl_found = { @current_root.get_the_index => root }
  @copies_by_old_pid = {}

  while true do
    copy_these = []

    @persisted_xampl_found.each do | pid, xampl |
      copy_these << xampl unless @copies_by_old_pid[pid]
    end

    break if 0 == copy_these.length

    @persisted_xampl_found = {}
    copy_these.each do | xampl |
      @current_root = xampl
      @out = ""
      @copies_by_old_pid[@current_root.get_the_index] = @out

      copy_xampl(@current_root)
    end
  end

  return @copies_by_old_pid
end

#persist_attribute(xampl) ⇒ Object



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/xamplr/visitors.rb', line 435

def persist_attribute(xampl)
  @attr_list = []
  pattr = xampl.indexed_by.to_s
  if (nil != xampl.attributes) then
    xampl.attributes.each{ | 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

#register_ns(ns) ⇒ Object



381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/xamplr/visitors.rb', line 381

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



377
378
379
# File 'lib/xamplr/visitors.rb', line 377

def revisit(xampl)
  return true
end

#show_attributesObject



451
452
453
454
455
456
457
458
# File 'lib/xamplr/visitors.rb', line 451

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

#start_element(xampl) ⇒ Object



460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/xamplr/visitors.rb', line 460

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
      @no_children = true
      persist_attribute(xampl)
    else
      attribute(xampl)
    end
    @body << tag_info << show_attributes
  end
end

#visit_string(string) ⇒ Object



553
554
555
# File 'lib/xamplr/visitors.rb', line 553

def visit_string(string)
  @body << string
end