Class: Net::LDAP::FilterParser

Inherits:
Object
  • Object
show all
Defined in:
vendor/plugins/ruby-net-ldap-0.0.4/lib/net/ldap/filter.rb

Overview

:nodoc:

Instance Attribute Summary

Instance Method Summary

Constructor Details

- (FilterParser) initialize(str)

A new instance of FilterParser



300
301
302
303
# File 'vendor/plugins/ruby-net-ldap-0.0.4/lib/net/ldap/filter.rb', line 300

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

Instance Attribute Details

- (Object) filter (readonly)

Returns the value of attribute filter



298
299
300
# File 'vendor/plugins/ruby-net-ldap-0.0.4/lib/net/ldap/filter.rb', line 298

def filter
  @filter
end

Instance Method Details

- (Object) parse(scanner)



305
306
307
# File 'vendor/plugins/ruby-net-ldap-0.0.4/lib/net/ldap/filter.rb', line 305

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

- (Object) parse_filter_branch(scanner)

Added a greatly-augmented filter contributed by Andre Nathan for detecting special characters in values. (15Aug06)



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'vendor/plugins/ruby-net-ldap-0.0.4/lib/net/ldap/filter.rb', line 355

def parse_filter_branch scanner
  scanner.scan(/\s*/)
  if token = scanner.scan( /[\w\-_]+/ )
    scanner.scan(/\s*/)
    if op = scanner.scan( /\=|\<\=|\<|\>\=|\>|\!\=/ )
      scanner.scan(/\s*/)
      #if value = scanner.scan( /[\w\*\.]+/ ) (ORG)
      if value = scanner.scan( /[\w\*\.\+\-@=#\$%&!]+/ )
        case op
        when "="
          Filter.eq( 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

- (Object) parse_paren_expression(scanner)



309
310
311
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
348
349
350
351
# File 'vendor/plugins/ruby-net-ldap-0.0.4/lib/net/ldap/filter.rb', line 309

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