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.



329
330
331
# File 'lib/xamplr/visitors.rb', line 329

def body
  @body
end

#ns_to_prefixObject

Returns the value of attribute ns_to_prefix.



329
330
331
# File 'lib/xamplr/visitors.rb', line 329

def ns_to_prefix
  @ns_to_prefix
end

#start_bodyObject

Returns the value of attribute start_body.



329
330
331
# File 'lib/xamplr/visitors.rb', line 329

def start_body
  @start_body
end

Class Method Details

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



331
332
333
# File 'lib/xamplr/visitors.rb', line 331

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

Instance Method Details

#after_visit(xampl) ⇒ Object



573
574
575
# File 'lib/xamplr/visitors.rb', line 573

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



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

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

#after_visit_mixed_content(xampl) ⇒ Object



561
562
563
# File 'lib/xamplr/visitors.rb', line 561

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

#attr_esc(s) ⇒ Object



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

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



447
448
449
450
451
452
453
454
455
456
457
# File 'lib/xamplr/visitors.rb', line 447

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



565
566
567
568
569
570
571
# File 'lib/xamplr/visitors.rb', line 565

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



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

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



552
553
554
555
556
557
558
559
# File 'lib/xamplr/visitors.rb', line 552

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

#before_visit_simple_content(xampl) ⇒ Object



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

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



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

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

#content_esc(s) ⇒ Object



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

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



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

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

#cycle(xampl) ⇒ Object

Raises:



396
397
398
399
# File 'lib/xamplr/visitors.rb', line 396

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

#define_nsObject



510
511
512
513
514
515
516
# File 'lib/xamplr/visitors.rb', line 510

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



518
519
520
# File 'lib/xamplr/visitors.rb', line 518

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

#end_element(xampl) ⇒ Object



504
505
506
507
508
# File 'lib/xamplr/visitors.rb', line 504

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

#get_the_new_pid(xampl) ⇒ Object



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

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



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/xamplr/visitors.rb', line 335

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 }
  @persisted_xampl_found       = {(@current_root || root).get_the_index => root} #TODO -- is root ever okay here?
  @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



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

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



405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/xamplr/visitors.rb', line 405

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



401
402
403
# File 'lib/xamplr/visitors.rb', line 401

def revisit(xampl)
  return true
end

#show_attributesObject



475
476
477
478
479
480
481
482
# File 'lib/xamplr/visitors.rb', line 475

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

#start_element(xampl) ⇒ Object



484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'lib/xamplr/visitors.rb', line 484

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



577
578
579
# File 'lib/xamplr/visitors.rb', line 577

def visit_string(string)
  @body << string
end