Class: Net::LDAP::FilterParser

Inherits:
Object
  • Object
show all
Defined in:
lib/net/ldap/filter.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ FilterParser

Returns a new instance of FilterParser.



559
560
561
562
# File 'lib/net/ldap/filter.rb', line 559

def initialize str
  require 'strscan'
  @filter = parse( StringScanner.new( str )) or raise Net::LDAP::LdapError.new( "invalid filter syntax" )
end

Instance Attribute Details

#filterObject (readonly)

Returns the value of attribute filter.



557
558
559
# File 'lib/net/ldap/filter.rb', line 557

def filter
  @filter
end

Instance Method Details

#parse(scanner) ⇒ Object



564
565
566
# File 'lib/net/ldap/filter.rb', line 564

def parse scanner
  parse_filter_branch(scanner) or parse_paren_expression(scanner)
end

#parse_filter_branch(scanner) ⇒ Object

Added a greatly-augmented filter contributed by Andre Nathan for detecting special characters in values. (15Aug06) Added blanks to the attribute filter (26Oct06)



615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
# File 'lib/net/ldap/filter.rb', line 615

def parse_filter_branch scanner
  scanner.scan(/\s*/)
  if token = scanner.scan( /[\d\w\-_\:\.]*[\d\w]/ )
    scanner.scan(/\s*/)
    if op = scanner.scan( /\=|\:\=|\<\=|\<|\>\=|\>|\!\=/ )
      scanner.scan(/\s*/)
      if value = scanner.scan( /(?:[\w\*\.\+\-@=,#\$%&! ]|\\[a-fA-F\d]{2,2})+/ )
        case op
        when "="
          Filter.eq( token, value )
        when ":="
          Filter.ex( token, value )
        when "!="
          Filter.ne( token, value )
        when "<"
          Filter.lt( token, value )
        when "<="
          Filter.le( token, value )
        when ">"
          Filter.gt( token, value )
        when ">="
          Filter.ge( token, value )
        end
      end
    end
  end
end

#parse_paren_expression(scanner) ⇒ Object



568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
# File 'lib/net/ldap/filter.rb', line 568

def parse_paren_expression scanner
  if scanner.scan(/\s*\(\s*/)
    b = if scanner.scan(/\s*\&\s*/)
      a = nil
      branches = []
      while br = parse_paren_expression(scanner)
        branches << br
      end
      if branches.length >= 2
        a = branches.shift
        while branches.length > 0
          a = a & branches.shift
        end
        a
      end
    elsif scanner.scan(/\s*\|\s*/)
      # TODO: DRY!
      a = nil
      branches = []
      while br = parse_paren_expression(scanner)
        branches << br
      end
      if branches.length >= 2
        a = branches.shift
        while branches.length > 0
          a = a | branches.shift
        end
        a
      end
    elsif scanner.scan(/\s*\!\s*/)
      br = parse_paren_expression(scanner)
      if br
        ~ br
      end
    else
      parse_filter_branch( scanner )
    end

    if b and scanner.scan( /\s*\)\s*/ )
      b
    end
  end
end