Class: REXML::Validation::Interleave

Inherits:
Choice show all
Defined in:
lib/rexml/validation/relaxng.rb

Instance Method Summary collapse

Methods inherited from Choice

#<<

Methods inherited from State

#<<, #previous=, #to_s

Constructor Details

#initialize(context) ⇒ Interleave

Returns a new instance of Interleave.



439
440
441
442
# File 'lib/rexml/validation/relaxng.rb', line 439

def initialize context
  super
  @choice = 0
end

Instance Method Details

#expectedObject



510
511
512
513
514
515
516
517
518
519
# File 'lib/rexml/validation/relaxng.rb', line 510

def expected
  return [@events[@current]] if @events[@current]
  return @choices[@choice..-1].collect do |x|
    if x[0].kind_of? State
      x[0].expected
    else
      x[0]
    end
  end.flatten
end

#inspectObject



521
522
523
# File 'lib/rexml/validation/relaxng.rb', line 521

def inspect
  "< #{to_s} #{@choices.collect{|e| e.collect{|f|f.to_s}.join(', ')}.join(' and ')} >"
end

#matches?(event) ⇒ Boolean

Returns:

  • (Boolean)


505
506
507
508
# File 'lib/rexml/validation/relaxng.rb', line 505

def matches?( event )
  return @events[@current].matches?( event ) if @events[@current]
  !@choices[@choice..-1].find{|evt| evt[0].matches?(event)}.nil?
end

#next(event) ⇒ Object



476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# File 'lib/rexml/validation/relaxng.rb', line 476

def next( event )
  # Find the next series
  next_current(event) unless @events[@current]
  return nil unless @events[@current]

  expand_ref_in( @events, @current ) if @events[@current].class == Ref
  if ( @events[@current].kind_of? State )
    @current += 1
    @events[@current-1].previous = self
    return @events[@current-1].next( event )
  end
  return @previous.pop.next( event ) if @events[@current].nil?
  if ( @events[@current].matches?(event) )
    @current += 1
    if @events[@current].nil?
      return self unless @choices[@choice].nil?
      return @previous.pop
    elsif @events[@current].kind_of? State
      @current += 1
      @events[@current-1].previous = self
      return @events[@current-1]
    else
      return self
    end
  else
    return nil
  end
end

#next_current(event) ⇒ Object



448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/rexml/validation/relaxng.rb', line 448

def next_current( event )
  # Expand references
  c = 0 ; max = @choices.size
  while c < max
    if @choices[c][0].class == Ref
      expand_ref_in( @choices[c], 0 )
      @choices += @choices[c]
      @choices.delete( @choices[c] )
      max -= 1
    else
      c += 1
    end
  end
  @events = @choices[@choice..-1].find { |evt| evt[0].matches? event }
  @current = 0
  if @events
    # reorder the choices
    old = @choices[@choice]
    idx = @choices.index( @events )
    @choices[@choice] = @events
    @choices[idx] = old
    @choice += 1
  end

  @events = [] unless @events
end

#resetObject



444
445
446
# File 'lib/rexml/validation/relaxng.rb', line 444

def reset
  @choice = 0
end