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



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

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



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

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

#after_visit_mixed_content(xampl) ⇒ Object



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

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

#attr_esc(s) ⇒ Object



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

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



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

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



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

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



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

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



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

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

#before_visit_simple_content(xampl) ⇒ Object



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

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



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

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

#content_esc(s) ⇒ Object



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

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



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

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

#cycle(xampl) ⇒ Object

Raises:



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

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

#define_nsObject



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

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



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

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

#end_element(xampl) ⇒ Object



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

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

#get_the_new_pid(xampl) ⇒ Object



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

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
347
# 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 }
  @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



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

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



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

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



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

def revisit(xampl)
  return true
end

#show_attributesObject



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

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

#start_element(xampl) ⇒ Object



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

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



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

def visit_string(string)
  @body << string
end