SubstringFilter.fromPattern constructor

SubstringFilter.fromPattern(
  1. String attributeName,
  2. String pattern
)

Implementation

SubstringFilter.fromPattern(String attributeName, String pattern)
    : _any = const [],
      super(Filter.TYPE_SUBSTRING) {
  // todo: We probaby need to properly escape special chars = and *
  if (pattern.length <= 2 || !pattern.contains('*')) {
    throw LdapUsageException(
        'Invalid substring pattern: expecting attr=match: $pattern');
  }

  _attributeName = attributeName;
  // now parse initial, any, final

  var x = pattern.split('*');

  if (x.length == 1) {
    throw LdapUsageException(
        'Invalid substring pattern: missing *: $pattern');
  }

  if (!x.any((s) => s.isNotEmpty)) {
    throw LdapUsageException(
        'Invalid substring pattern: use \'present\' filter instead: \'$pattern\'');
  }

  /*
   *  foo*
   *  *foo
   *
   *  foo*bar
   *  foo*bar*baz*boo
   */

  if (x[0] != '') {
    _initial = x[0];
  }
  if (x.last != '') {
    _final = x.last;
  }
  _any = [];
  for (var i = 1; i < x.length - 1; ++i) {
    _any.add(x[i]);
  }
}