public static ADFilter ParseFilterString(string filter) { Debug.Assert(filter != null); //perfomr the matthing. On success all info we are interested in //will be stored in the named captures. try { Match m = s_mFilter.Match(filter); if (!m.Success) { return null; } ADFilter result = new ADFilter(); if (m.Groups["item"].ToString().Length != 0) { //we have am "item"filter which can be //of type "present", "simple", "substring", or "extensible" if (m.Groups["present"].ToString().Length != 0) { //present filter, e.g. (objectClass=*) result.Type = ADFilter.FilterType.Present; Debug.Assert(m.Groups["presentattr"].ToString().Length != 0); result.Filter.Present = m.Groups["presentattr"].ToString(); } else if (m.Groups["simple"].ToString().Length != 0) { //simple filter, e.g. (simpleattr =|~=|>=|<= simplevalue) ADAttribute simple = new ADAttribute(); if (m.Groups["simplevalue"].ToString().Length != 0) { ADValue val = StringFilterValueToADValue(m.Groups["simplevalue"].ToString()); simple.Values.Add(val); } simple.Name = m.Groups["simpleattr"].ToString(); //the variours types of relationships we might have switch (m.Groups["filtertype"].ToString()) { case "=": result.Type = ADFilter.FilterType.EqualityMatch; result.Filter.EqualityMatch = simple; break; case "~=": result.Type = ADFilter.FilterType.ApproxMatch; result.Filter.ApproxMatch = simple; break; case "<=": result.Type = ADFilter.FilterType.LessOrEqual; result.Filter.LessOrEqual = simple; break; case ">=": result.Type = ADFilter.FilterType.GreaterOrEqual; result.Filter.GreaterOrEqual = simple; break; default: //this should not occur Debug.Fail("FilterParser.ParseFilterString: Invalid simple filter"); //treat like a parse error return null; } } else if (m.Groups["substr"].ToString().Length != 0) { //we have a substring filter. Get the various parts of it result.Type = ADFilter.FilterType.Substrings; ADSubstringFilter substr = new ADSubstringFilter(); substr.Initial = StringFilterValueToADValue(m.Groups["initialvalue"].ToString()); substr.Final = StringFilterValueToADValue(m.Groups["finalvalue"].ToString()); if (m.Groups["anyvalue"].ToString().Length != 0) { foreach (Capture c in m.Groups["anyvalue"].Captures) { substr.Any.Add(StringFilterValueToADValue(c.ToString())); } } substr.Name = m.Groups["substrattr"].ToString(); result.Filter.Substrings = substr; } else if (m.Groups["extensible"].ToString().Length != 0) { //extensible filter result.Type = ADFilter.FilterType.ExtensibleMatch; ADExtenMatchFilter exten = new ADExtenMatchFilter(); exten.Value = StringFilterValueToADValue(m.Groups["extenvalue"].ToString()); exten.DNAttributes = (m.Groups["dnattr"].ToString().Length != 0); exten.Name = m.Groups["extenattr"].ToString(); exten.MatchingRule = m.Groups["matchrule"].ToString(); result.Filter.ExtensibleMatch = exten; } else { //this should not occur Debug.Fail("Invalid item filter"); //treat like a parse error return null; } } else { //compound recursive filter //extract the filter lists ArrayList filters = new ArrayList(); string filterList = m.Groups["filterlist"].ToString().Trim(); while (filterList.Length > 0) { if (filterList[0] != '(') { //this is a parse error: invalid filter return null; } int strIdx = 1; int left = 1; //count opening brackest bool gotSubfilter = false; while (strIdx < filterList.Length && !gotSubfilter) { if (filterList[strIdx] == '(') { left++; } if (filterList[strIdx] == ')') { if (left < 1) { //unbalanced parenthesis return null; } else if (left == 1) { //the end of the subfilter gotSubfilter = true; } else { left--; } } strIdx++; } if (!gotSubfilter) { //the filter list did not consist entirely of subfilters return null; } filters.Add(filterList.Substring(0, strIdx)); filterList = filterList.Substring(strIdx).TrimStart(); } ADFilter eltFilter = null; switch (m.Groups["filtercomp"].ToString()) { case "|": result.Type = ADFilter.FilterType.Or; result.Filter.Or = new ArrayList(); foreach (String f in filters) { eltFilter = ParseFilterString(f); if (eltFilter == null) { return null; } result.Filter.Or.Add(eltFilter); } if (result.Filter.Or.Count < 1) { return null; } break; case "&": result.Type = ADFilter.FilterType.And; result.Filter.And = new ArrayList(); foreach (String f in filters) { eltFilter = ParseFilterString(f); if (eltFilter == null) { return null; } result.Filter.And.Add(eltFilter); } if (result.Filter.And.Count < 1) { return null; } break; case "!": result.Type = ADFilter.FilterType.Not; eltFilter = ParseFilterString((string)filters[0]); //Note that for ease of defining the filter grammar we allow //more than one filter after '!'. We catch this here if (filters.Count > 1 || eltFilter == null) { return null; } result.Filter.Not = eltFilter; break; default: //this should not occur Debug.Fail("Invalid filter composition"); //treat like a parse error return null; } } return result; }//end of try catch (RegexMatchTimeoutException) { Debug.WriteLine("The input filter String: {0} exceeded the regex match timeout of {1} seconds.", filter, mFilterTimeOutInSeconds); return null; } }
public void WriteFilter(ADFilter filter, bool filterTags, XmlWriter mXmlWriter, string strNamespace) { if (filterTags) { if (strNamespace != null) { mXmlWriter.WriteStartElement("filter", strNamespace); } else { mXmlWriter.WriteStartElement("filter"); } } switch (filter.Type) { case ADFilter.FilterType.And: if (strNamespace == null) { mXmlWriter.WriteStartElement("and"); break; } mXmlWriter.WriteStartElement("and", strNamespace); break; case ADFilter.FilterType.Or: if (strNamespace == null) { mXmlWriter.WriteStartElement("or"); } else { mXmlWriter.WriteStartElement("or", strNamespace); } foreach (object obj3 in filter.Filter.Or) { this.WriteFilter((ADFilter) obj3, false, mXmlWriter, strNamespace); } mXmlWriter.WriteEndElement(); goto Label_03E0; case ADFilter.FilterType.Not: if (strNamespace == null) { mXmlWriter.WriteStartElement("not"); } else { mXmlWriter.WriteStartElement("not", strNamespace); } this.WriteFilter(filter.Filter.Not, false, mXmlWriter, strNamespace); mXmlWriter.WriteEndElement(); goto Label_03E0; case ADFilter.FilterType.EqualityMatch: this.WriteAttrib("equalityMatch", filter.Filter.EqualityMatch, mXmlWriter, strNamespace); goto Label_03E0; case ADFilter.FilterType.Substrings: { ADSubstringFilter substrings = filter.Filter.Substrings; if (strNamespace == null) { mXmlWriter.WriteStartElement("substrings"); } else { mXmlWriter.WriteStartElement("substrings", strNamespace); } mXmlWriter.WriteAttributeString("name", substrings.Name); if (substrings.Initial != null) { this.WriteValue("initial", substrings.Initial, mXmlWriter, strNamespace); } if (substrings.Any != null) { foreach (object obj4 in substrings.Any) { this.WriteValue("any", (ADValue) obj4, mXmlWriter, strNamespace); } } if (substrings.Final != null) { this.WriteValue("final", substrings.Final, mXmlWriter, strNamespace); } mXmlWriter.WriteEndElement(); goto Label_03E0; } case ADFilter.FilterType.GreaterOrEqual: this.WriteAttrib("greaterOrEqual", filter.Filter.GreaterOrEqual, mXmlWriter, strNamespace); goto Label_03E0; case ADFilter.FilterType.LessOrEqual: this.WriteAttrib("lessOrEqual", filter.Filter.LessOrEqual, mXmlWriter, strNamespace); goto Label_03E0; case ADFilter.FilterType.Present: if (strNamespace == null) { mXmlWriter.WriteStartElement("present"); } else { mXmlWriter.WriteStartElement("present", strNamespace); } mXmlWriter.WriteAttributeString("name", filter.Filter.Present); mXmlWriter.WriteEndElement(); goto Label_03E0; case ADFilter.FilterType.ApproxMatch: this.WriteAttrib("approxMatch", filter.Filter.ApproxMatch, mXmlWriter, strNamespace); goto Label_03E0; case ADFilter.FilterType.ExtensibleMatch: { ADExtenMatchFilter extensibleMatch = filter.Filter.ExtensibleMatch; if (strNamespace == null) { mXmlWriter.WriteStartElement("extensibleMatch"); } else { mXmlWriter.WriteStartElement("extensibleMatch", strNamespace); } if ((extensibleMatch.Name != null) && (extensibleMatch.Name.Length != 0)) { mXmlWriter.WriteAttributeString("name", extensibleMatch.Name); } if ((extensibleMatch.MatchingRule != null) && (extensibleMatch.MatchingRule.Length != 0)) { mXmlWriter.WriteAttributeString("matchingRule", extensibleMatch.MatchingRule); } mXmlWriter.WriteAttributeString("dnAttributes", XmlConvert.ToString(extensibleMatch.DNAttributes)); this.WriteValue("value", extensibleMatch.Value, mXmlWriter, strNamespace); mXmlWriter.WriteEndElement(); goto Label_03E0; } default: throw new ArgumentException(System.DirectoryServices.Protocols.Res.GetString("InvalidFilterType", new object[] { filter.Type })); } foreach (object obj2 in filter.Filter.And) { this.WriteFilter((ADFilter) obj2, false, mXmlWriter, strNamespace); } mXmlWriter.WriteEndElement(); Label_03E0: if (filterTags) { mXmlWriter.WriteEndElement(); } }
public static ADFilter ParseFilterString(string filter) { Match match = mFilter.Match(filter); if (match.Success) { ADFilter filter2 = new ADFilter(); if (match.Groups["item"].ToString().Length == 0) { int num; ArrayList list = new ArrayList(); for (string str = match.Groups["filterlist"].ToString().Trim(); str.Length > 0; str = str.Substring(num).TrimStart(new char[0])) { if (str[0] != '(') { return null; } num = 1; int num2 = 1; bool flag = false; while ((num < str.Length) && !flag) { if (str[num] == '(') { num2++; } if (str[num] == ')') { if (num2 < 1) { return null; } if (num2 == 1) { flag = true; } else { num2--; } } num++; } if (!flag) { return null; } list.Add(str.Substring(0, num)); } ADFilter filter5 = null; switch (match.Groups["filtercomp"].ToString()) { case "|": filter2.Type = ADFilter.FilterType.Or; filter2.Filter.Or = new ArrayList(); foreach (string str2 in list) { filter5 = ParseFilterString(str2); if (filter5 == null) { return null; } filter2.Filter.Or.Add(filter5); } if (filter2.Filter.Or.Count >= 1) { return filter2; } return null; case "&": filter2.Type = ADFilter.FilterType.And; filter2.Filter.And = new ArrayList(); foreach (string str3 in list) { filter5 = ParseFilterString(str3); if (filter5 == null) { return null; } filter2.Filter.And.Add(filter5); } if (filter2.Filter.And.Count >= 1) { return filter2; } return null; case "!": filter2.Type = ADFilter.FilterType.Not; filter5 = ParseFilterString((string) list[0]); if ((list.Count > 1) || (filter5 == null)) { return null; } filter2.Filter.Not = filter5; return filter2; } return null; } if (match.Groups["present"].ToString().Length != 0) { filter2.Type = ADFilter.FilterType.Present; filter2.Filter.Present = match.Groups["presentattr"].ToString(); return filter2; } if (match.Groups["simple"].ToString().Length == 0) { if (match.Groups["substr"].ToString().Length != 0) { filter2.Type = ADFilter.FilterType.Substrings; ADSubstringFilter filter3 = new ADSubstringFilter { Initial = StringFilterValueToADValue(match.Groups["initialvalue"].ToString()), Final = StringFilterValueToADValue(match.Groups["finalvalue"].ToString()) }; if (match.Groups["anyvalue"].ToString().Length != 0) { foreach (Capture capture in match.Groups["anyvalue"].Captures) { filter3.Any.Add(StringFilterValueToADValue(capture.ToString())); } } filter3.Name = match.Groups["substrattr"].ToString(); filter2.Filter.Substrings = filter3; return filter2; } if (match.Groups["extensible"].ToString().Length != 0) { filter2.Type = ADFilter.FilterType.ExtensibleMatch; ADExtenMatchFilter filter4 = new ADExtenMatchFilter { Value = StringFilterValueToADValue(match.Groups["extenvalue"].ToString()), DNAttributes = match.Groups["dnattr"].ToString().Length != 0, Name = match.Groups["extenattr"].ToString(), MatchingRule = match.Groups["matchrule"].ToString() }; filter2.Filter.ExtensibleMatch = filter4; return filter2; } return null; } ADAttribute attribute = new ADAttribute(); if (match.Groups["simplevalue"].ToString().Length != 0) { ADValue value2 = StringFilterValueToADValue(match.Groups["simplevalue"].ToString()); attribute.Values.Add(value2); } attribute.Name = match.Groups["simpleattr"].ToString(); switch (match.Groups["filtertype"].ToString()) { case "=": filter2.Type = ADFilter.FilterType.EqualityMatch; filter2.Filter.EqualityMatch = attribute; return filter2; case "~=": filter2.Type = ADFilter.FilterType.ApproxMatch; filter2.Filter.ApproxMatch = attribute; return filter2; case "<=": filter2.Type = ADFilter.FilterType.LessOrEqual; filter2.Filter.LessOrEqual = attribute; return filter2; case ">=": filter2.Type = ADFilter.FilterType.GreaterOrEqual; filter2.Filter.GreaterOrEqual = attribute; return filter2; } } return null; }
public void WriteFilter(ADFilter filter, bool filterTags, XmlWriter mXmlWriter, string strNamespace) { if (filterTags) { if (strNamespace == null) { mXmlWriter.WriteStartElement("filter"); } else { mXmlWriter.WriteStartElement("filter", strNamespace); } } ADFilter.FilterType type = filter.Type; if (type == ADFilter.FilterType.And) { if (strNamespace == null) { mXmlWriter.WriteStartElement("and"); } else { mXmlWriter.WriteStartElement("and", strNamespace); } foreach (object and in filter.Filter.And) { this.WriteFilter((ADFilter)and, false, mXmlWriter, strNamespace); } mXmlWriter.WriteEndElement(); } else if (type == ADFilter.FilterType.Or) { if (strNamespace == null) { mXmlWriter.WriteStartElement("or"); } else { mXmlWriter.WriteStartElement("or", strNamespace); } foreach (object or in filter.Filter.Or) { this.WriteFilter((ADFilter)or, false, mXmlWriter, strNamespace); } mXmlWriter.WriteEndElement(); } else if (type == ADFilter.FilterType.Not) { if (strNamespace == null) { mXmlWriter.WriteStartElement("not"); } else { mXmlWriter.WriteStartElement("not", strNamespace); } this.WriteFilter(filter.Filter.Not, false, mXmlWriter, strNamespace); mXmlWriter.WriteEndElement(); } else if (type == ADFilter.FilterType.EqualityMatch) { this.WriteAttrib("equalityMatch", filter.Filter.EqualityMatch, mXmlWriter, strNamespace); } else if (type == ADFilter.FilterType.Substrings) { ADSubstringFilter substrings = filter.Filter.Substrings; if (strNamespace == null) { mXmlWriter.WriteStartElement("substrings"); } else { mXmlWriter.WriteStartElement("substrings", strNamespace); } mXmlWriter.WriteAttributeString("name", substrings.Name); if (substrings.Initial != null) { this.WriteValue("initial", substrings.Initial, mXmlWriter, strNamespace); } if (substrings.Any != null) { foreach (object any in substrings.Any) { this.WriteValue("any", (ADValue)any, mXmlWriter, strNamespace); } } if (substrings.Final != null) { this.WriteValue("final", substrings.Final, mXmlWriter, strNamespace); } mXmlWriter.WriteEndElement(); } else if (type == ADFilter.FilterType.GreaterOrEqual) { this.WriteAttrib("greaterOrEqual", filter.Filter.GreaterOrEqual, mXmlWriter, strNamespace); } else if (type == ADFilter.FilterType.LessOrEqual) { this.WriteAttrib("lessOrEqual", filter.Filter.LessOrEqual, mXmlWriter, strNamespace); } else if (type == ADFilter.FilterType.Present) { if (strNamespace == null) { mXmlWriter.WriteStartElement("present"); } else { mXmlWriter.WriteStartElement("present", strNamespace); } mXmlWriter.WriteAttributeString("name", filter.Filter.Present); mXmlWriter.WriteEndElement(); } else if (type == ADFilter.FilterType.ApproxMatch) { this.WriteAttrib("approxMatch", filter.Filter.ApproxMatch, mXmlWriter, strNamespace); } else if (type == ADFilter.FilterType.ExtensibleMatch) { ADExtenMatchFilter extensibleMatch = filter.Filter.ExtensibleMatch; if (strNamespace == null) { mXmlWriter.WriteStartElement("extensibleMatch"); } else { mXmlWriter.WriteStartElement("extensibleMatch", strNamespace); } if (extensibleMatch.Name != null && extensibleMatch.Name.Length != 0) { mXmlWriter.WriteAttributeString("name", extensibleMatch.Name); } if (extensibleMatch.MatchingRule != null && extensibleMatch.MatchingRule.Length != 0) { mXmlWriter.WriteAttributeString("matchingRule", extensibleMatch.MatchingRule); } mXmlWriter.WriteAttributeString("dnAttributes", XmlConvert.ToString(extensibleMatch.DNAttributes)); this.WriteValue("value", extensibleMatch.Value, mXmlWriter, strNamespace); mXmlWriter.WriteEndElement(); } else { object[] objArray = new object[1]; objArray[0] = (object)filter.Type; throw new ArgumentException(Res.GetString("InvalidFilterType", objArray)); } if (filterTags) { mXmlWriter.WriteEndElement(); } }
public static ADFilter ParseFilterString(string filter) { ADFilter aDFilter = null; ADFilter aDFilter1 = null; try { Match match = FilterParser.mFilter.Match(filter); if (match.Success) { ADFilter arrayLists = new ADFilter(); if (match.Groups["item"].ToString().Length == 0) { ArrayList arrayLists1 = new ArrayList(); string str = match.Groups["filterlist"].ToString().Trim(); while (str.Length > 0) { if (str[0] == '(') { int num = 1; int num1 = 1; bool flag = false; while (num < str.Length && !flag) { if (str[num] == '(') { num1++; } if (str[num] == ')') { if (num1 >= 1) { if (num1 != 1) { num1--; } else { flag = true; } } else { aDFilter1 = null; return(aDFilter1); } } num++; } if (flag) { arrayLists1.Add(str.Substring(0, num)); str = str.Substring(num).TrimStart(new char[0]); } else { aDFilter1 = null; return(aDFilter1); } } else { aDFilter1 = null; return(aDFilter1); } } string str1 = match.Groups["filtercomp"].ToString(); string str2 = str1; if (str1 != null) { if (str2 == "|") { arrayLists.Type = ADFilter.FilterType.Or; arrayLists.Filter.Or = new ArrayList(); foreach (string arrayList in arrayLists1) { aDFilter = FilterParser.ParseFilterString(arrayList); if (aDFilter != null) { arrayLists.Filter.Or.Add(aDFilter); } else { aDFilter1 = null; return(aDFilter1); } } if (arrayLists.Filter.Or.Count < 1) { aDFilter1 = null; return(aDFilter1); } else { goto Label1; } } else { if (str2 == "&") { arrayLists.Type = ADFilter.FilterType.And; arrayLists.Filter.And = new ArrayList(); foreach (string arrayList1 in arrayLists1) { aDFilter = FilterParser.ParseFilterString(arrayList1); if (aDFilter != null) { arrayLists.Filter.And.Add(aDFilter); } else { aDFilter1 = null; return(aDFilter1); } } if (arrayLists.Filter.And.Count < 1) { aDFilter1 = null; return(aDFilter1); } else { goto Label1; } } else { if (str2 != "!") { aDFilter1 = null; return(aDFilter1); } arrayLists.Type = ADFilter.FilterType.Not; aDFilter = FilterParser.ParseFilterString((string)arrayLists1[0]); if (arrayLists1.Count > 1 || aDFilter == null) { aDFilter1 = null; return(aDFilter1); } else { arrayLists.Filter.Not = aDFilter; goto Label1; } } } } aDFilter1 = null; return(aDFilter1); } else { if (match.Groups["present"].ToString().Length == 0) { if (match.Groups["simple"].ToString().Length == 0) { if (match.Groups["substr"].ToString().Length == 0) { if (match.Groups["extensible"].ToString().Length == 0) { aDFilter1 = null; return(aDFilter1); } else { arrayLists.Type = ADFilter.FilterType.ExtensibleMatch; ADExtenMatchFilter aDExtenMatchFilter = new ADExtenMatchFilter(); aDExtenMatchFilter.Value = FilterParser.StringFilterValueToADValue(match.Groups["extenvalue"].ToString()); aDExtenMatchFilter.DNAttributes = match.Groups["dnattr"].ToString().Length != 0; aDExtenMatchFilter.Name = match.Groups["extenattr"].ToString(); aDExtenMatchFilter.MatchingRule = match.Groups["matchrule"].ToString(); arrayLists.Filter.ExtensibleMatch = aDExtenMatchFilter; } } else { arrayLists.Type = ADFilter.FilterType.Substrings; ADSubstringFilter aDSubstringFilter = new ADSubstringFilter(); aDSubstringFilter.Initial = FilterParser.StringFilterValueToADValue(match.Groups["initialvalue"].ToString()); aDSubstringFilter.Final = FilterParser.StringFilterValueToADValue(match.Groups["finalvalue"].ToString()); if (match.Groups["anyvalue"].ToString().Length != 0) { foreach (Capture capture in match.Groups["anyvalue"].Captures) { aDSubstringFilter.Any.Add(FilterParser.StringFilterValueToADValue(capture.ToString())); } } aDSubstringFilter.Name = match.Groups["substrattr"].ToString(); arrayLists.Filter.Substrings = aDSubstringFilter; } } else { ADAttribute aDAttribute = new ADAttribute(); if (match.Groups["simplevalue"].ToString().Length != 0) { ADValue aDValue = FilterParser.StringFilterValueToADValue(match.Groups["simplevalue"].ToString()); aDAttribute.Values.Add(aDValue); } aDAttribute.Name = match.Groups["simpleattr"].ToString(); string str3 = match.Groups["filtertype"].ToString(); string str4 = str3; if (str3 != null) { if (str4 == "=") { arrayLists.Type = ADFilter.FilterType.EqualityMatch; arrayLists.Filter.EqualityMatch = aDAttribute; goto Label1; } else { if (str4 == "~=") { arrayLists.Type = ADFilter.FilterType.ApproxMatch; arrayLists.Filter.ApproxMatch = aDAttribute; goto Label1; } else { if (str4 == "<=") { arrayLists.Type = ADFilter.FilterType.LessOrEqual; arrayLists.Filter.LessOrEqual = aDAttribute; goto Label1; } else { if (str4 != ">=") { aDFilter1 = null; return(aDFilter1); } arrayLists.Type = ADFilter.FilterType.GreaterOrEqual; arrayLists.Filter.GreaterOrEqual = aDAttribute; goto Label1; } } } } aDFilter1 = null; return(aDFilter1); } } else { arrayLists.Type = ADFilter.FilterType.Present; arrayLists.Filter.Present = match.Groups["presentattr"].ToString(); } } Label1: aDFilter1 = arrayLists; } else { aDFilter1 = null; } } catch (Exception regexMatchTimeoutException) { aDFilter1 = null; } return(aDFilter1); }
public static ADFilter ParseFilterString(string filter) { ADFilter aDFilter = null; ADFilter aDFilter1 = null; try { Match match = FilterParser.mFilter.Match(filter); if (match.Success) { ADFilter arrayLists = new ADFilter(); if (match.Groups["item"].ToString().Length == 0) { ArrayList arrayLists1 = new ArrayList(); string str = match.Groups["filterlist"].ToString().Trim(); while (str.Length > 0) { if (str[0] == '(') { int num = 1; int num1 = 1; bool flag = false; while (num < str.Length && !flag) { if (str[num] == '(') { num1++; } if (str[num] == ')') { if (num1 >= 1) { if (num1 != 1) { num1--; } else { flag = true; } } else { aDFilter1 = null; return aDFilter1; } } num++; } if (flag) { arrayLists1.Add(str.Substring(0, num)); str = str.Substring(num).TrimStart(new char[0]); } else { aDFilter1 = null; return aDFilter1; } } else { aDFilter1 = null; return aDFilter1; } } string str1 = match.Groups["filtercomp"].ToString(); string str2 = str1; if (str1 != null) { if (str2 == "|") { arrayLists.Type = ADFilter.FilterType.Or; arrayLists.Filter.Or = new ArrayList(); foreach (string arrayList in arrayLists1) { aDFilter = FilterParser.ParseFilterString(arrayList); if (aDFilter != null) { arrayLists.Filter.Or.Add(aDFilter); } else { aDFilter1 = null; return aDFilter1; } } if (arrayLists.Filter.Or.Count < 1) { aDFilter1 = null; return aDFilter1; } else { goto Label1; } } else { if (str2 == "&") { arrayLists.Type = ADFilter.FilterType.And; arrayLists.Filter.And = new ArrayList(); foreach (string arrayList1 in arrayLists1) { aDFilter = FilterParser.ParseFilterString(arrayList1); if (aDFilter != null) { arrayLists.Filter.And.Add(aDFilter); } else { aDFilter1 = null; return aDFilter1; } } if (arrayLists.Filter.And.Count < 1) { aDFilter1 = null; return aDFilter1; } else { goto Label1; } } else { if (str2 != "!") { aDFilter1 = null; return aDFilter1; } arrayLists.Type = ADFilter.FilterType.Not; aDFilter = FilterParser.ParseFilterString((string)arrayLists1[0]); if (arrayLists1.Count > 1 || aDFilter == null) { aDFilter1 = null; return aDFilter1; } else { arrayLists.Filter.Not = aDFilter; goto Label1; } } } } aDFilter1 = null; return aDFilter1; } else { if (match.Groups["present"].ToString().Length == 0) { if (match.Groups["simple"].ToString().Length == 0) { if (match.Groups["substr"].ToString().Length == 0) { if (match.Groups["extensible"].ToString().Length == 0) { aDFilter1 = null; return aDFilter1; } else { arrayLists.Type = ADFilter.FilterType.ExtensibleMatch; ADExtenMatchFilter aDExtenMatchFilter = new ADExtenMatchFilter(); aDExtenMatchFilter.Value = FilterParser.StringFilterValueToADValue(match.Groups["extenvalue"].ToString()); aDExtenMatchFilter.DNAttributes = match.Groups["dnattr"].ToString().Length != 0; aDExtenMatchFilter.Name = match.Groups["extenattr"].ToString(); aDExtenMatchFilter.MatchingRule = match.Groups["matchrule"].ToString(); arrayLists.Filter.ExtensibleMatch = aDExtenMatchFilter; } } else { arrayLists.Type = ADFilter.FilterType.Substrings; ADSubstringFilter aDSubstringFilter = new ADSubstringFilter(); aDSubstringFilter.Initial = FilterParser.StringFilterValueToADValue(match.Groups["initialvalue"].ToString()); aDSubstringFilter.Final = FilterParser.StringFilterValueToADValue(match.Groups["finalvalue"].ToString()); if (match.Groups["anyvalue"].ToString().Length != 0) { foreach (Capture capture in match.Groups["anyvalue"].Captures) { aDSubstringFilter.Any.Add(FilterParser.StringFilterValueToADValue(capture.ToString())); } } aDSubstringFilter.Name = match.Groups["substrattr"].ToString(); arrayLists.Filter.Substrings = aDSubstringFilter; } } else { ADAttribute aDAttribute = new ADAttribute(); if (match.Groups["simplevalue"].ToString().Length != 0) { ADValue aDValue = FilterParser.StringFilterValueToADValue(match.Groups["simplevalue"].ToString()); aDAttribute.Values.Add(aDValue); } aDAttribute.Name = match.Groups["simpleattr"].ToString(); string str3 = match.Groups["filtertype"].ToString(); string str4 = str3; if (str3 != null) { if (str4 == "=") { arrayLists.Type = ADFilter.FilterType.EqualityMatch; arrayLists.Filter.EqualityMatch = aDAttribute; goto Label1; } else { if (str4 == "~=") { arrayLists.Type = ADFilter.FilterType.ApproxMatch; arrayLists.Filter.ApproxMatch = aDAttribute; goto Label1; } else { if (str4 == "<=") { arrayLists.Type = ADFilter.FilterType.LessOrEqual; arrayLists.Filter.LessOrEqual = aDAttribute; goto Label1; } else { if (str4 != ">=") { aDFilter1 = null; return aDFilter1; } arrayLists.Type = ADFilter.FilterType.GreaterOrEqual; arrayLists.Filter.GreaterOrEqual = aDAttribute; goto Label1; } } } } aDFilter1 = null; return aDFilter1; } } else { arrayLists.Type = ADFilter.FilterType.Present; arrayLists.Filter.Present = match.Groups["presentattr"].ToString(); } } Label1: aDFilter1 = arrayLists; } else { aDFilter1 = null; } } catch (Exception regexMatchTimeoutException) { aDFilter1 = null; } return aDFilter1; }
public void WriteFilter(ADFilter filter, bool filterTags, XmlWriter mXmlWriter, string strNamespace) { if (filterTags) { if (strNamespace != null) { mXmlWriter.WriteStartElement(DsmlConstants.ElementSearchReqFilter, strNamespace); } else { mXmlWriter.WriteStartElement(DsmlConstants.ElementSearchReqFilter); } } switch (filter.Type) { case ADFilter.FilterType.And: if (strNamespace != null) { mXmlWriter.WriteStartElement(DsmlConstants.ElementSearchReqFilterAnd, strNamespace); } else { mXmlWriter.WriteStartElement(DsmlConstants.ElementSearchReqFilterAnd); } foreach (object andClause in filter.Filter.And) { WriteFilter((ADFilter)andClause, false, mXmlWriter, strNamespace); } mXmlWriter.WriteEndElement(); break; case ADFilter.FilterType.Or: if (strNamespace != null) { mXmlWriter.WriteStartElement(DsmlConstants.ElementSearchReqFilterOr, strNamespace); } else { mXmlWriter.WriteStartElement(DsmlConstants.ElementSearchReqFilterOr); } foreach (object orClause in filter.Filter.Or) { WriteFilter((ADFilter)orClause, false, mXmlWriter, strNamespace); } mXmlWriter.WriteEndElement(); break; case ADFilter.FilterType.Not: if (strNamespace != null) { mXmlWriter.WriteStartElement(DsmlConstants.ElementSearchReqFilterNot, strNamespace); } else { mXmlWriter.WriteStartElement(DsmlConstants.ElementSearchReqFilterNot); } WriteFilter(filter.Filter.Not, false, mXmlWriter, strNamespace); mXmlWriter.WriteEndElement(); break; case ADFilter.FilterType.EqualityMatch: WriteAttrib(DsmlConstants.ElementSearchReqFilterEqual, filter.Filter.EqualityMatch, mXmlWriter, strNamespace); break; case ADFilter.FilterType.Present: if (strNamespace != null) { mXmlWriter.WriteStartElement(DsmlConstants.ElementSearchReqFilterPresent, strNamespace); } else { mXmlWriter.WriteStartElement(DsmlConstants.ElementSearchReqFilterPresent); } mXmlWriter.WriteAttributeString(DsmlConstants.AttrSearchReqFilterPresentName, filter.Filter.Present); mXmlWriter.WriteEndElement(); break; case ADFilter.FilterType.GreaterOrEqual: WriteAttrib(DsmlConstants.ElementSearchReqFilterGrteq, filter.Filter.GreaterOrEqual, mXmlWriter, strNamespace); break; case ADFilter.FilterType.LessOrEqual: WriteAttrib(DsmlConstants.ElementSearchReqFilterLesseq, filter.Filter.LessOrEqual, mXmlWriter, strNamespace); break; case ADFilter.FilterType.ApproxMatch: WriteAttrib(DsmlConstants.ElementSearchReqFilterApprox, filter.Filter.ApproxMatch, mXmlWriter, strNamespace); break; case ADFilter.FilterType.ExtensibleMatch: ADExtenMatchFilter exten = filter.Filter.ExtensibleMatch; if (strNamespace != null) { mXmlWriter.WriteStartElement(DsmlConstants.ElementSearchReqFilterExtenmatch, strNamespace); } else { mXmlWriter.WriteStartElement(DsmlConstants.ElementSearchReqFilterExtenmatch); } if ((exten.Name != null) && (exten.Name.Length != 0)) { mXmlWriter.WriteAttributeString( DsmlConstants.AttrSearchReqFilterExtenmatchName, exten.Name); } if ((exten.MatchingRule != null) && (exten.MatchingRule.Length != 0)) { mXmlWriter.WriteAttributeString( DsmlConstants.AttrSearchReqFilterExtenmatchMatchrule, exten.MatchingRule); } mXmlWriter.WriteAttributeString( DsmlConstants.AttrSearchReqFilterExtenmatchDnattr, XmlConvert.ToString(exten.DNAttributes)); WriteValue(DsmlConstants.ElementSearchReqFilterExtenmatchValue, exten.Value, mXmlWriter, strNamespace); mXmlWriter.WriteEndElement(); break; case ADFilter.FilterType.Substrings: //handle <substrings> ADSubstringFilter substr = filter.Filter.Substrings; if (strNamespace != null) { mXmlWriter.WriteStartElement(DsmlConstants.ElementSearchReqFilterSubstr, strNamespace); } else { mXmlWriter.WriteStartElement(DsmlConstants.ElementSearchReqFilterSubstr); } mXmlWriter.WriteAttributeString(DsmlConstants.AttrSearchReqFilterSubstrName, substr.Name); if (substr.Initial != null) { WriteValue(DsmlConstants.ElementSearchReqFilterSubstrInit, substr.Initial, mXmlWriter, strNamespace); } if (substr.Any != null) { foreach (object sub in substr.Any) { WriteValue(DsmlConstants.ElementSearchReqFilterSubstrAny, (ADValue)sub, mXmlWriter, strNamespace); } } if (substr.Final != null) { WriteValue(DsmlConstants.ElementSearchReqFilterSubstrFinal, substr.Final, mXmlWriter, strNamespace); } mXmlWriter.WriteEndElement(); break; default: Debug.Fail("Invalid substring filter"); //just in case ... this will be caught by the caller throw new ArgumentException(Res.GetString(Res.InvalidFilterType, filter.Type)); } if (filterTags) { mXmlWriter.WriteEndElement(); //</filter> } }
public static ADFilter ParseFilterString(string filter) { Debug.Assert(filter != null); //perform the matching. On success all info we are interested in //will be stored in the named captures. try { Match m = s_mFilter.Match(filter); if (!m.Success) { return(null); } ADFilter result = new ADFilter(); if (m.Groups["item"].ToString().Length != 0) { //we have am "item"filter which can be //of type "present", "simple", "substring", or "extensible" if (m.Groups["present"].ToString().Length != 0) { //present filter, e.g. (objectClass=*) result.Type = ADFilter.FilterType.Present; Debug.Assert(m.Groups["presentattr"].ToString().Length != 0); result.Filter.Present = m.Groups["presentattr"].ToString(); } else if (m.Groups["simple"].ToString().Length != 0) { //simple filter, e.g. (simpleattr =|~=|>=|<= simplevalue) ADAttribute simple = new ADAttribute(); if (m.Groups["simplevalue"].ToString().Length != 0) { ADValue val = StringFilterValueToADValue(m.Groups["simplevalue"].ToString()); simple.Values.Add(val); } simple.Name = m.Groups["simpleattr"].ToString(); //the variours types of relationships we might have switch (m.Groups["filtertype"].ToString()) { case "=": result.Type = ADFilter.FilterType.EqualityMatch; result.Filter.EqualityMatch = simple; break; case "~=": result.Type = ADFilter.FilterType.ApproxMatch; result.Filter.ApproxMatch = simple; break; case "<=": result.Type = ADFilter.FilterType.LessOrEqual; result.Filter.LessOrEqual = simple; break; case ">=": result.Type = ADFilter.FilterType.GreaterOrEqual; result.Filter.GreaterOrEqual = simple; break; default: //this should not occur Debug.Fail("FilterParser.ParseFilterString: Invalid simple filter"); //treat like a parse error return(null); } } else if (m.Groups["substr"].ToString().Length != 0) { //we have a substring filter. Get the various parts of it result.Type = ADFilter.FilterType.Substrings; ADSubstringFilter substr = new ADSubstringFilter(); substr.Initial = StringFilterValueToADValue(m.Groups["initialvalue"].ToString()); substr.Final = StringFilterValueToADValue(m.Groups["finalvalue"].ToString()); if (m.Groups["anyvalue"].ToString().Length != 0) { foreach (Capture c in m.Groups["anyvalue"].Captures) { substr.Any.Add(StringFilterValueToADValue(c.ToString())); } } substr.Name = m.Groups["substrattr"].ToString(); result.Filter.Substrings = substr; } else if (m.Groups["extensible"].ToString().Length != 0) { //extensible filter result.Type = ADFilter.FilterType.ExtensibleMatch; ADExtenMatchFilter exten = new ADExtenMatchFilter(); exten.Value = StringFilterValueToADValue(m.Groups["extenvalue"].ToString()); exten.DNAttributes = (m.Groups["dnattr"].ToString().Length != 0); exten.Name = m.Groups["extenattr"].ToString(); exten.MatchingRule = m.Groups["matchrule"].ToString(); result.Filter.ExtensibleMatch = exten; } else { //this should not occur Debug.Fail("Invalid item filter"); //treat like a parse error return(null); } } else { //compound recursive filter //extract the filter lists ArrayList filters = new ArrayList(); string filterList = m.Groups["filterlist"].ToString().Trim(); while (filterList.Length > 0) { if (filterList[0] != '(') { //this is a parse error: invalid filter return(null); } int strIdx = 1; int left = 1; //count opening brackest bool gotSubfilter = false; while (strIdx < filterList.Length && !gotSubfilter) { if (filterList[strIdx] == '(') { left++; } if (filterList[strIdx] == ')') { if (left < 1) { //unbalanced parenthesis return(null); } else if (left == 1) { //the end of the subfilter gotSubfilter = true; } else { left--; } } strIdx++; } if (!gotSubfilter) { //the filter list did not consist entirely of subfilters return(null); } filters.Add(filterList.Substring(0, strIdx)); filterList = filterList.Substring(strIdx).TrimStart(); } ADFilter eltFilter = null; switch (m.Groups["filtercomp"].ToString()) { case "|": result.Type = ADFilter.FilterType.Or; result.Filter.Or = new ArrayList(); foreach (String f in filters) { eltFilter = ParseFilterString(f); if (eltFilter == null) { return(null); } result.Filter.Or.Add(eltFilter); } if (result.Filter.Or.Count < 1) { return(null); } break; case "&": result.Type = ADFilter.FilterType.And; result.Filter.And = new ArrayList(); foreach (String f in filters) { eltFilter = ParseFilterString(f); if (eltFilter == null) { return(null); } result.Filter.And.Add(eltFilter); } if (result.Filter.And.Count < 1) { return(null); } break; case "!": result.Type = ADFilter.FilterType.Not; eltFilter = ParseFilterString((string)filters[0]); //Note that for ease of defining the filter grammar we allow //more than one filter after '!'. We catch this here if (filters.Count > 1 || eltFilter == null) { return(null); } result.Filter.Not = eltFilter; break; default: //this should not occur Debug.Fail("Invalid filter composition"); //treat like a parse error return(null); } } return(result); }//end of try catch (RegexMatchTimeoutException) { Debug.WriteLine("The input filter String: {0} exceeded the regex match timeout of {1} seconds.", filter, mFilterTimeOutInSeconds); return(null); } }