Novell.Directory.Ldap.Rfc2251.RfcFilter.parse C# (CSharp) Method

parse() private method

Parses an RFC 2251 filter string into an ASN.1 Ldap Filter object.
private parse ( System filterExpr ) : Asn1Tagged
filterExpr System
return Novell.Directory.Ldap.Asn1.Asn1Tagged
        private Asn1Tagged parse(System.String filterExpr)
        {
            if ((System.Object) filterExpr == null || filterExpr.Equals(""))
            {
                filterExpr = new System.Text.StringBuilder("(objectclass=*)").ToString();
            }
            int idx;
            if ((idx = filterExpr.IndexOf((System.Char) '\\')) != - 1)
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder(filterExpr);
                int i = idx;
                while (i < (sb.Length - 1))
                {
                    char c = sb[i++];
                    if (c == '\\')
                    {
                        // found '\' (backslash)
                        // If V2 escape, turn to a V3 escape
                        c = sb[i];
                        if (c == '*' || c == '(' || c == ')' || c == '\\')
                        {
                            // Ldap v2 filter, convert them into hex chars
                            sb.Remove(i, i + 1 - i);
                            sb.Insert(i, System.Convert.ToString((int) c, 16));
                            i += 2;
                        }
                    }
                }
                filterExpr = sb.ToString();
            }

            // missing opening and closing parentheses, must be V2, add parentheses
            if ((filterExpr[0] != '(') && (filterExpr[filterExpr.Length - 1] != ')'))
            {
                filterExpr = "(" + filterExpr + ")";
            }

            char ch = filterExpr[0];
            int len = filterExpr.Length;

            // missing opening parenthesis ?
            if (ch != '(')
            {
                throw new LdapLocalException(ExceptionMessages.MISSING_LEFT_PAREN, LdapException.FILTER_ERROR);
            }

            // missing closing parenthesis ?
            if (filterExpr[len - 1] != ')')
            {
                throw new LdapLocalException(ExceptionMessages.MISSING_RIGHT_PAREN, LdapException.FILTER_ERROR);
            }

            // unmatched parentheses ?
            int parenCount = 0;
            for (int i = 0; i < len; i++)
            {
                if (filterExpr[i] == '(')
                {
                    parenCount++;
                }

                if (filterExpr[i] == ')')
                {
                    parenCount--;
                }
            }

            if (parenCount > 0)
            {
                throw new LdapLocalException(ExceptionMessages.MISSING_RIGHT_PAREN, LdapException.FILTER_ERROR);
            }

            if (parenCount < 0)
            {
                throw new LdapLocalException(ExceptionMessages.MISSING_LEFT_PAREN, LdapException.FILTER_ERROR);
            }
            ft = new FilterTokenizer(this, filterExpr);

            return parseFilter();
        }