From: Oleg T. <he...@us...> - 2005-10-17 11:03:59
|
Update of /cvsroot/mvp-xml/EXSLT/v2/src/Exslt In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6152/v2/src/Exslt Modified Files: ExsltContext.cs ExsltNodeList.cs ExsltSets.cs Log Message: Index: ExsltContext.cs =================================================================== RCS file: /cvsroot/mvp-xml/EXSLT/v2/src/Exslt/ExsltContext.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- ExsltContext.cs 16 Oct 2005 20:07:08 -0000 1.1 +++ ExsltContext.cs 17 Oct 2005 11:03:48 -0000 1.2 @@ -4,328 +4,343 @@ using System.Xml.Xsl; using System.Xml; using System.Xml.XPath; -using System.Reflection; #endregion -namespace Mvp.Xml.Exslt +namespace Mvp.Xml.Exslt { - /// <summary> - /// Custom XsltContext for resolving EXSLT functions in - /// XPath-only environment. - /// </summary> - public class ExsltContext : XsltContext { - - #region Private Fields and Properties - - private XmlNameTable _nt; - - /// <summary> - /// Bitwise enumeration used to specify which EXSLT functions should be accessible to - /// in the ExsltContext object. The default value is ExsltFunctionNamespace.All - /// </summary> - private ExsltFunctionNamespace _supportedFunctions = ExsltFunctionNamespace.All; + /// <summary> + /// Custom XsltContext for resolving EXSLT functions in + /// XPath-only environment. + /// </summary> + public class ExsltContext : XsltContext + { - /// <summary> - /// Extension object which implements the functions in the http://exslt.org/math namespace - /// </summary> - private ExsltMath exsltMath = new ExsltMath(); + #region Private Fields and Properties - - /// <summary> - /// Extension object which implements the functions in the http://exslt.org/dates-and-times namespace - /// </summary> - private ExsltDatesAndTimes exsltDatesAndTimes = new ExsltDatesAndTimes(); + private XmlNameTable _nt; - /// <summary> - /// Extension object which implements the functions in the http://exslt.org/regular-expressions namespace - /// </summary> - private ExsltRegularExpressions exsltRegularExpressions = new ExsltRegularExpressions(); + /// <summary> + /// Bitwise enumeration used to specify which EXSLT functions should be accessible to + /// in the ExsltContext object. The default value is ExsltFunctionNamespace.All + /// </summary> + private ExsltFunctionNamespace _supportedFunctions = ExsltFunctionNamespace.All; - /// <summary> - /// Extension object which implements the functions in the http://exslt.org/strings namespace - /// </summary> - private ExsltStrings exsltStrings = new ExsltStrings(); + /// <summary> + /// Extension object which implements the functions in the http://exslt.org/math namespace + /// </summary> + private ExsltMath exsltMath = new ExsltMath(); - /// <summary> - /// Extension object which implements the functions in the http://exslt.org/sets namespace - /// </summary> - private ExsltSets exsltSets = new ExsltSets(); - /// <summary> - /// Extension object which implements the functions in the http://exslt.org/random namespace - /// </summary> - private ExsltRandom exsltRandom = new ExsltRandom(); + /// <summary> + /// Extension object which implements the functions in the http://exslt.org/dates-and-times namespace + /// </summary> + private ExsltDatesAndTimes exsltDatesAndTimes = new ExsltDatesAndTimes(); - /// <summary> - /// Extension object which implements the functions in the http://gotdotnet.com/exslt/dates-and-times namespace - /// </summary> - private GDNDatesAndTimes gdnDatesAndTimes = new GDNDatesAndTimes(); - - /// <summary> - /// Extension object which implements the functions in the http://gotdotnet.com/exslt/regular-expressions namespace - /// </summary> - private GDNRegularExpressions gdnRegularExpressions = new GDNRegularExpressions(); - - /// <summary> - /// Extension object which implements the functions in the http://gotdotnet.com/exslt/math namespace - /// </summary> - private GDNMath gdnMath = new GDNMath(); - - /// <summary> - /// Extension object which implements the functions in the http://gotdotnet.com/exslt/sets namespace - /// </summary> - private GDNSets gdnSets = new GDNSets(); - - /// <summary> - /// Extension object which implements the functions in the http://gotdotnet.com/exslt/strings namespace - /// </summary> - private GDNStrings gdnStrings = new GDNStrings(); + /// <summary> + /// Extension object which implements the functions in the http://exslt.org/regular-expressions namespace + /// </summary> + private ExsltRegularExpressions exsltRegularExpressions = new ExsltRegularExpressions(); - /// <summary> - /// Extension object which implements the functions in the http://gotdotnet.com/exslt/dynamic namespace - /// </summary> - private GDNDynamic gdnDynamic = new GDNDynamic(); - - #endregion - - #region Constructors - - public ExsltContext(XmlNameTable nt) : base((NameTable)nt) { - _nt = nt; - AddExtensionNamespaces(); - } - - public ExsltContext(NameTable nt, ExsltFunctionNamespace supportedFunctions) - : this(nt) { - SupportedFunctions = supportedFunctions; - } - - #endregion + /// <summary> + /// Extension object which implements the functions in the http://exslt.org/strings namespace + /// </summary> + private ExsltStrings exsltStrings = new ExsltStrings(); - #region Private methods - private void AddExtensionNamespaces() - { - //remove all our extension objects in case the ExsltContext is being reused - RemoveNamespace("math", ExsltNamespaces.Math); - RemoveNamespace("date", ExsltNamespaces.DatesAndTimes); - RemoveNamespace("regexp", ExsltNamespaces.RegularExpressions); - RemoveNamespace("str", ExsltNamespaces.Strings); - RemoveNamespace("set", ExsltNamespaces.Sets); - RemoveNamespace("random", ExsltNamespaces.Random); - RemoveNamespace("date2", ExsltNamespaces.GDNDatesAndTimes); - RemoveNamespace("math2", ExsltNamespaces.GDNMath); - RemoveNamespace("regexp2", ExsltNamespaces.GDNRegularExpressions); - RemoveNamespace("set2", ExsltNamespaces.GDNSets); - RemoveNamespace("str2", ExsltNamespaces.GDNStrings); - RemoveNamespace("dyn2", ExsltNamespaces.GDNDynamic); + /// <summary> + /// Extension object which implements the functions in the http://exslt.org/sets namespace + /// </summary> + private ExsltSets exsltSets = new ExsltSets(); - //add extension objects as specified by SupportedFunctions - if ((this.SupportedFunctions & ExsltFunctionNamespace.Math) > 0) - AddNamespace("math", ExsltNamespaces.Math); + /// <summary> + /// Extension object which implements the functions in the http://exslt.org/random namespace + /// </summary> + private ExsltRandom exsltRandom = new ExsltRandom(); - if ((this.SupportedFunctions & ExsltFunctionNamespace.DatesAndTimes) > 0) - AddNamespace("date", ExsltNamespaces.DatesAndTimes); + /// <summary> + /// Extension object which implements the functions in the http://gotdotnet.com/exslt/dates-and-times namespace + /// </summary> + private GDNDatesAndTimes gdnDatesAndTimes = new GDNDatesAndTimes(); - if ((this.SupportedFunctions & ExsltFunctionNamespace.RegularExpressions) > 0) - AddNamespace("regexp", ExsltNamespaces.RegularExpressions); + /// <summary> + /// Extension object which implements the functions in the http://gotdotnet.com/exslt/regular-expressions namespace + /// </summary> + private GDNRegularExpressions gdnRegularExpressions = new GDNRegularExpressions(); - if ((this.SupportedFunctions & ExsltFunctionNamespace.Strings) > 0) - AddNamespace("str", ExsltNamespaces.Strings); + /// <summary> + /// Extension object which implements the functions in the http://gotdotnet.com/exslt/math namespace + /// </summary> + private GDNMath gdnMath = new GDNMath(); - if ((this.SupportedFunctions & ExsltFunctionNamespace.Sets) > 0) - AddNamespace("set", ExsltNamespaces.Sets); + /// <summary> + /// Extension object which implements the functions in the http://gotdotnet.com/exslt/sets namespace + /// </summary> + private GDNSets gdnSets = new GDNSets(); - if ((this.SupportedFunctions & ExsltFunctionNamespace.Random) > 0) - AddNamespace("random", ExsltNamespaces.Random); + /// <summary> + /// Extension object which implements the functions in the http://gotdotnet.com/exslt/strings namespace + /// </summary> + private GDNStrings gdnStrings = new GDNStrings(); - if ((this.SupportedFunctions & ExsltFunctionNamespace.GDNDatesAndTimes) > 0) - AddNamespace("date2", ExsltNamespaces.GDNDatesAndTimes); + /// <summary> + /// Extension object which implements the functions in the http://gotdotnet.com/exslt/dynamic namespace + /// </summary> + private GDNDynamic gdnDynamic = new GDNDynamic(); - if ((this.SupportedFunctions & ExsltFunctionNamespace.GDNMath) > 0) - AddNamespace("math2", ExsltNamespaces.GDNMath); + #endregion - if ((this.SupportedFunctions & ExsltFunctionNamespace.GDNRegularExpressions) > 0) - AddNamespace("regexp2", ExsltNamespaces.GDNRegularExpressions); + #region Constructors - if ((this.SupportedFunctions & ExsltFunctionNamespace.GDNSets) > 0) - AddNamespace("set2", ExsltNamespaces.GDNSets); + public ExsltContext(XmlNameTable nt) + : base((NameTable)nt) + { + _nt = nt; + AddExtensionNamespaces(); + } - if ((this.SupportedFunctions & ExsltFunctionNamespace.GDNStrings) > 0) - AddNamespace("str2", ExsltNamespaces.GDNStrings); + public ExsltContext(NameTable nt, ExsltFunctionNamespace supportedFunctions) + : this(nt) + { + SupportedFunctions = supportedFunctions; + } - if ((this.SupportedFunctions & ExsltFunctionNamespace.GDNDynamic) > 0) - AddNamespace("dyn2", ExsltNamespaces.GDNDynamic); - } - #endregion - - #region Public Properties - /// <summary> - /// Bitwise enumeration used to specify which EXSLT functions should be accessible to - /// in the ExsltContext. The default value is ExsltFunctionNamespace.All - /// </summary> - public ExsltFunctionNamespace SupportedFunctions { - set { - if (Enum.IsDefined(typeof(ExsltFunctionNamespace), value)) - _supportedFunctions = value; - } - get { return _supportedFunctions; } - } - #endregion + #endregion - #region XsltContext Overrides - public override int CompareDocument(string baseUri, string nextbaseUri) - { - return 0; - } + #region Private methods + private void AddExtensionNamespaces() + { + //remove all our extension objects in case the ExsltContext is being reused + RemoveNamespace("math", ExsltNamespaces.Math); + RemoveNamespace("date", ExsltNamespaces.DatesAndTimes); + RemoveNamespace("regexp", ExsltNamespaces.RegularExpressions); + RemoveNamespace("str", ExsltNamespaces.Strings); + RemoveNamespace("set", ExsltNamespaces.Sets); + RemoveNamespace("random", ExsltNamespaces.Random); + RemoveNamespace("date2", ExsltNamespaces.GDNDatesAndTimes); + RemoveNamespace("math2", ExsltNamespaces.GDNMath); + RemoveNamespace("regexp2", ExsltNamespaces.GDNRegularExpressions); + RemoveNamespace("set2", ExsltNamespaces.GDNSets); + RemoveNamespace("str2", ExsltNamespaces.GDNStrings); + RemoveNamespace("dyn2", ExsltNamespaces.GDNDynamic); - public override bool PreserveWhitespace(XPathNavigator node) - { - return true; - } + //add extension objects as specified by SupportedFunctions + if ((this.SupportedFunctions & ExsltFunctionNamespace.Math) > 0) + AddNamespace("math", ExsltNamespaces.Math); - public override bool Whitespace - { - get { return true; } - } + if ((this.SupportedFunctions & ExsltFunctionNamespace.DatesAndTimes) > 0) + AddNamespace("date", ExsltNamespaces.DatesAndTimes); - /// <summary> - /// Resolves variables. - /// </summary> - /// <param name="prefix">The variable's prefix</param> - /// <param name="name">The variable's name</param> - /// <returns></returns> - public override IXsltContextVariable ResolveVariable(string prefix, string name) - { - return null; - } + if ((this.SupportedFunctions & ExsltFunctionNamespace.RegularExpressions) > 0) + AddNamespace("regexp", ExsltNamespaces.RegularExpressions); - /// <summary> - /// Resolves custom function in XPath expression. - /// </summary> - /// <param name="prefix">The prefix of the function as it appears in the XPath expression.</param> - /// <param name="name">The name of the function.</param> - /// <param name="argTypes">An array of argument types for the function being resolved. - /// This allows you to select between methods with the same name (for example, overloaded - /// methods). </param> - /// <returns>An IXsltContextFunction representing the function.</returns> - public override IXsltContextFunction ResolveFunction(string prefix, string name, - XPathResultType[] argTypes) + if ((this.SupportedFunctions & ExsltFunctionNamespace.Strings) > 0) + AddNamespace("str", ExsltNamespaces.Strings); + + if ((this.SupportedFunctions & ExsltFunctionNamespace.Sets) > 0) + AddNamespace("set", ExsltNamespaces.Sets); + + if ((this.SupportedFunctions & ExsltFunctionNamespace.Random) > 0) + AddNamespace("random", ExsltNamespaces.Random); + + if ((this.SupportedFunctions & ExsltFunctionNamespace.GDNDatesAndTimes) > 0) + AddNamespace("date2", ExsltNamespaces.GDNDatesAndTimes); + + if ((this.SupportedFunctions & ExsltFunctionNamespace.GDNMath) > 0) + AddNamespace("math2", ExsltNamespaces.GDNMath); + + if ((this.SupportedFunctions & ExsltFunctionNamespace.GDNRegularExpressions) > 0) + AddNamespace("regexp2", ExsltNamespaces.GDNRegularExpressions); + + if ((this.SupportedFunctions & ExsltFunctionNamespace.GDNSets) > 0) + AddNamespace("set2", ExsltNamespaces.GDNSets); + + if ((this.SupportedFunctions & ExsltFunctionNamespace.GDNStrings) > 0) + AddNamespace("str2", ExsltNamespaces.GDNStrings); + + if ((this.SupportedFunctions & ExsltFunctionNamespace.GDNDynamic) > 0) + AddNamespace("dyn2", ExsltNamespaces.GDNDynamic); + } + #endregion + + #region Public Properties + /// <summary> + /// Bitwise enumeration used to specify which EXSLT functions should be accessible to + /// in the ExsltContext. The default value is ExsltFunctionNamespace.All + /// </summary> + public ExsltFunctionNamespace SupportedFunctions + { + set + { + if (Enum.IsDefined(typeof(ExsltFunctionNamespace), value)) + _supportedFunctions = value; + } + get { return _supportedFunctions; } + } + #endregion + + #region XsltContext Overrides + public override int CompareDocument(string baseUri, string nextbaseUri) + { + return 0; + } + + public override bool PreserveWhitespace(XPathNavigator node) + { + return true; + } + + public override bool Whitespace + { + get { return true; } + } + + /// <summary> + /// Resolves variables. + /// </summary> + /// <param name="prefix">The variable's prefix</param> + /// <param name="name">The variable's name</param> + /// <returns></returns> + public override IXsltContextVariable ResolveVariable(string prefix, string name) + { + return null; + } + + /// <summary> + /// Resolves custom function in XPath expression. + /// </summary> + /// <param name="prefix">The prefix of the function as it appears in the XPath expression.</param> + /// <param name="name">The name of the function.</param> + /// <param name="argTypes">An array of argument types for the function being resolved. + /// This allows you to select between methods with the same name (for example, overloaded + /// methods). </param> + /// <returns>An IXsltContextFunction representing the function.</returns> + public override IXsltContextFunction ResolveFunction(string prefix, string name, + XPathResultType[] argTypes) + { + switch (LookupNamespace(_nt.Get(prefix))) + { + case ExsltNamespaces.DatesAndTimes: + return GetExtensionFunctionImplementation(exsltDatesAndTimes, name, argTypes); + case ExsltNamespaces.Math: + return GetExtensionFunctionImplementation(exsltMath, name, argTypes); + case ExsltNamespaces.RegularExpressions: + return GetExtensionFunctionImplementation(exsltRegularExpressions, name, argTypes); + case ExsltNamespaces.Sets: + return GetExtensionFunctionImplementation(exsltSets, name, argTypes); + case ExsltNamespaces.Strings: + return GetExtensionFunctionImplementation(exsltStrings, name, argTypes); + case ExsltNamespaces.Random: + return GetExtensionFunctionImplementation(exsltRandom, name, argTypes); + case ExsltNamespaces.GDNDatesAndTimes: + return GetExtensionFunctionImplementation(gdnDatesAndTimes, name, argTypes); + case ExsltNamespaces.GDNMath: + return GetExtensionFunctionImplementation(gdnMath, name, argTypes); + case ExsltNamespaces.GDNRegularExpressions: + return GetExtensionFunctionImplementation(gdnRegularExpressions, name, argTypes); + case ExsltNamespaces.GDNSets: + return GetExtensionFunctionImplementation(gdnSets, name, argTypes); + case ExsltNamespaces.GDNStrings: + return GetExtensionFunctionImplementation(gdnStrings, name, argTypes); + case ExsltNamespaces.GDNDynamic: + return GetExtensionFunctionImplementation(gdnDynamic, name, argTypes); + default: + throw new XPathException(string.Format("Unrecognized extension function namespace: prefix='{0}', namespace URI='{1}'", + prefix, LookupNamespace(_nt.Get(prefix))), null); + } + } + #endregion + + /// <summary> + /// Finds appropriate implementation for an extension function - public + /// method with the same number of arguments and compatible argument types. + /// </summary> + /// <param name="obj">Extension object</param> + /// <param name="name">Function name</param> + /// <param name="argTypes">Types of arguments</param> + /// <returns></returns> + private ExsltContextFunction GetExtensionFunctionImplementation(object obj, string name, XPathResultType[] argTypes) + { + //For each method in object's type + foreach (MethodInfo mi in obj.GetType().GetMethods()) + { + //We are interested in methods with given name + if (mi.Name == name) { - switch (LookupNamespace(_nt.Get(prefix))) - { - case ExsltNamespaces.DatesAndTimes: - return GetExtensionFunctionImplementation(exsltDatesAndTimes, name, argTypes); - case ExsltNamespaces.Math: - return GetExtensionFunctionImplementation(exsltMath, name, argTypes); - case ExsltNamespaces.RegularExpressions: - return GetExtensionFunctionImplementation(exsltRegularExpressions, name, argTypes); - case ExsltNamespaces.Sets: - return GetExtensionFunctionImplementation(exsltSets, name, argTypes); - case ExsltNamespaces.Strings: - return GetExtensionFunctionImplementation(exsltStrings, name, argTypes); - case ExsltNamespaces.Random: - return GetExtensionFunctionImplementation(exsltRandom, name, argTypes); - case ExsltNamespaces.GDNDatesAndTimes: - return GetExtensionFunctionImplementation(gdnDatesAndTimes, name, argTypes); - case ExsltNamespaces.GDNMath: - return GetExtensionFunctionImplementation(gdnMath, name, argTypes); - case ExsltNamespaces.GDNRegularExpressions: - return GetExtensionFunctionImplementation(gdnRegularExpressions, name, argTypes); - case ExsltNamespaces.GDNSets: - return GetExtensionFunctionImplementation(gdnSets, name, argTypes); - case ExsltNamespaces.GDNStrings: - return GetExtensionFunctionImplementation(gdnStrings, name, argTypes); - case ExsltNamespaces.GDNDynamic: - return GetExtensionFunctionImplementation(gdnDynamic, name, argTypes); - default: - throw new XPathException(string.Format("Unrecognized extension function namespace: prefix='{0}', namespace URI='{1}'", - prefix, LookupNamespace(_nt.Get(prefix))), null); + ParameterInfo[] parameters = mi.GetParameters(); + ////We are interested in methods with given number of arguments + if (parameters.Length == argTypes.Length) + { + bool mismatch = false; + //Now let's check out if parameter types are compatible with actual ones + for (int i = 0; i < parameters.Length; i++) + { + ParameterInfo pi = parameters[i]; + XPathResultType paramType = ConvertToXPathType(pi.ParameterType); + if (paramType == XPathResultType.Any || paramType == argTypes[i]) + continue; + else + { + mismatch = true; + break; + } } - } - #endregion - - /// <summary> - /// Finds appropriate implementation for an extension function - public - /// method with the same number of arguments and compatible argument types. - /// </summary> - /// <param name="obj">Extension object</param> - /// <param name="name">Function name</param> - /// <param name="argTypes">Types of arguments</param> - /// <returns></returns> - private ExsltContextFunction GetExtensionFunctionImplementation(object obj, string name, XPathResultType[] argTypes) { - //For each method in object's type - foreach (MethodInfo mi in obj.GetType().GetMethods()) { - //We are interested in methods with given name - if (mi.Name == name) { - ParameterInfo[] parameters = mi.GetParameters(); - ////We are interested in methods with given number of arguments - if (parameters.Length == argTypes.Length) { - bool mismatch = false; - //Now let's check out if parameter types are compatible with actual ones - for(int i=0; i<parameters.Length; i++) { - ParameterInfo pi = parameters[i]; - XPathResultType paramType = ConvertToXPathType(pi.ParameterType); - if (paramType == XPathResultType.Any || paramType == argTypes[i]) - continue; - else { - mismatch = true; - break; - } - } - if (!mismatch) - //Create lightweight wrapper around method info - return new ExsltContextFunction(mi, argTypes, obj); - } - } - } - throw new XPathException("Extension function not found: " + name, null); - } - - /// <summary> - /// Converts CLI type to XPathResultType type. - /// </summary> - /// <param name="type"></param> - /// <returns></returns> - public static XPathResultType ConvertToXPathType(Type type) { - switch (Type.GetTypeCode(type)) { - case TypeCode.Boolean: - return XPathResultType.Boolean; - case TypeCode.String: - return XPathResultType.String; - case TypeCode.Object: - if (typeof(IXPathNavigable).IsAssignableFrom(type) || - typeof(XPathNavigator).IsAssignableFrom(type)) - return XPathResultType.Navigator; - else if (typeof(XPathNodeIterator).IsAssignableFrom(type)) - return XPathResultType.NodeSet; - else - return XPathResultType.Any; - case TypeCode.DateTime: - case TypeCode.DBNull: - case TypeCode.Empty: - return XPathResultType.Error; - default: - return XPathResultType.Number; - } - } - - /// <summary> - /// This is a workaround for some problem, see - /// http://www.tkachenko.com/blog/archives/000042.html for more - /// details. - /// </summary> - /// <param name="prefix">Prefix to be resolved</param> - /// <returns>Resolved namespace</returns> - public override string LookupNamespace(string prefix) - { - if (prefix == String.Empty) - return prefix; - string uri = base.LookupNamespace(NameTable.Get(prefix)); - if (uri == null) - throw new XsltException("Undeclared namespace prefix - " + prefix, null); + if (!mismatch) + //Create lightweight wrapper around method info + return new ExsltContextFunction(mi, argTypes, obj); + } + } + } + throw new XPathException("Extension function not found: " + name, null); + } - return uri; - } - } + /// <summary> + /// Converts CLI type to XPathResultType type. + /// </summary> + /// <param name="type"></param> + /// <returns></returns> + public static XPathResultType ConvertToXPathType(Type type) + { + switch (Type.GetTypeCode(type)) + { + case TypeCode.Boolean: + return XPathResultType.Boolean; + case TypeCode.String: + return XPathResultType.String; + case TypeCode.Object: + if (typeof(IXPathNavigable).IsAssignableFrom(type) || + typeof(XPathNavigator).IsAssignableFrom(type)) + return XPathResultType.Navigator; + else if (typeof(XPathNodeIterator).IsAssignableFrom(type)) + return XPathResultType.NodeSet; + else + return XPathResultType.Any; + case TypeCode.DateTime: + case TypeCode.DBNull: + case TypeCode.Empty: + return XPathResultType.Error; + default: + return XPathResultType.Number; + } + } + + //TODO: test it + + ///// <summary> + ///// This is a workaround for some problem, see + ///// http://www.tkachenko.com/blog/archives/000042.html for more + ///// details. + ///// </summary> + ///// <param name="prefix">Prefix to be resolved</param> + ///// <returns>Resolved namespace</returns> + //public override string LookupNamespace(string prefix) + //{ + // if (prefix == String.Empty) + // return prefix; + // string uri = base.LookupNamespace(NameTable.Get(prefix)); + // if (uri == null) + // throw new XsltException("Undeclared namespace prefix - " + prefix, null); + + // return uri; + //} + } } // namespace GotDotNet.Exslt \ No newline at end of file Index: ExsltSets.cs =================================================================== RCS file: /cvsroot/mvp-xml/EXSLT/v2/src/Exslt/ExsltSets.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- ExsltSets.cs 16 Oct 2005 20:07:08 -0000 1.1 +++ ExsltSets.cs 17 Oct 2005 11:03:49 -0000 1.2 @@ -26,7 +26,7 @@ /// <returns>The those nodes that are in the node set /// passed as the first argument that are not in the node set /// passed as the second argument.</returns> - public XPathNodeIterator difference(XPathNodeIterator nodeset1, XPathNodeIterator nodeset2) + public XPathNavigator[] difference(XPathNodeIterator nodeset1, XPathNodeIterator nodeset2) { if(nodeset2.Count > 166) return difference2(nodeset1, nodeset2); @@ -47,7 +47,7 @@ } } - return nodelist1; + return nodelist1.ToArray(); } /// <summary> /// Implements an optimized algorithm for the following function Index: ExsltNodeList.cs =================================================================== RCS file: /cvsroot/mvp-xml/EXSLT/v2/src/Exslt/ExsltNodeList.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- ExsltNodeList.cs 16 Oct 2005 20:07:08 -0000 1.1 +++ ExsltNodeList.cs 17 Oct 2005 11:03:49 -0000 1.2 @@ -8,189 +8,208 @@ namespace Mvp.Xml.Exslt { - /// <summary> - /// A list that holds XPathNavigator objects - /// </summary> - internal class ExsltNodeList - { + /// <summary> + /// A list that holds XPathNavigator objects + /// </summary> + internal class ExsltNodeList + { - #region Private Fields and Properties + #region Private Fields and Properties - /// <summary> - /// The inner arraylist used by this class. - /// </summary> - internal IList<XPathNavigator> innerList = new List<XPathNavigator>(); + /// <summary> + /// The inner arraylist used by this class. + /// </summary> + private List<XPathNavigator> innerList = new List<XPathNavigator>(); - #endregion + #endregion - - #region Public Fields and Properties - - /// <summary> - /// Gets or sets the element at the specified index - /// </summary> - public XPathNavigator this[int index] { - get {return (XPathNavigator) this.innerList[index];} - set { this.innerList[index] = value; } - } - /// <summary> - /// Gets the number of items in the list - /// </summary> - public int Count {get { return this.innerList.Count;}} + #region Public Fields and Properties - #endregion - + /// <summary> + /// Gets or sets the element at the specified index + /// </summary> + public XPathNavigator this[int index] + { + get { return innerList[index]; } + set { innerList[index] = value; } + } - #region Constructors - - public ExsltNodeList(){} + /// <summary> + /// Gets the number of items in the list + /// </summary> + public int Count { get { return innerList.Count; } } + #endregion - /// <summary> - /// Initializes the ExsltNodeList with the specified XPathNodeIterator. All nodes - /// in the iterator are placed in the list. - /// </summary> - /// <param name="iterator">The iterator to load the nodelist from</param> - public ExsltNodeList(XPathNodeIterator iterator): this(iterator, false) {} + #region Constructors - - /// <summary> - /// Initializes the ExsltNodeList with the specified XPathNodeIterator. All nodes - /// in the iterator are placed in the list. - /// </summary> - /// <param name="iterator">The iterator to load the nodelist from</param> - /// <param name="removeDuplicates">A flag that indicates whether duplicate nodes - /// should be loaded into the nodelist or only node with unique identity should - /// be added</param> - public ExsltNodeList(XPathNodeIterator iterator, bool removeDuplicates){ - - XPathNodeIterator it = iterator.Clone(); + public ExsltNodeList() { } - while(it.MoveNext()){ - - if(removeDuplicates){ - if(this.Contains(it.Current)){ - continue; - } - } - - this.Add(it.Current.Clone()); - } - } + /// <summary> + /// Initializes the ExsltNodeList with the specified XPathNodeIterator. All nodes + /// in the iterator are placed in the list. + /// </summary> + /// <param name="iterator">The iterator to load the nodelist from</param> + public ExsltNodeList(XPathNodeIterator iterator) : this(iterator, false) { } - #endregion - #region Public Methods + /// <summary> + /// Initializes the ExsltNodeList with the specified XPathNodeIterator. All nodes + /// in the iterator are placed in the list. + /// </summary> + /// <param name="iterator">The iterator to load the nodelist from</param> + /// <param name="removeDuplicates">A flag that indicates whether duplicate nodes + /// should be loaded into the nodelist or only node with unique identity should + /// be added</param> + public ExsltNodeList(XPathNodeIterator iterator, bool removeDuplicates) + { + XPathNodeIterator it = iterator.Clone(); - /// <summary> - /// Returns an enumerator for the entire list. - /// </summary> - /// <returns>An enumerator for the entire list</returns> - public IEnumerator<XPathNavigator> GetEnumerator(){ - return this.innerList.GetEnumerator(); - } + while (it.MoveNext()) + { - /// <summary> - /// Adds an item to the list - /// </summary> - /// <param name="value">The item to add</param> - public void Add( XPathNavigator nav){ + if (removeDuplicates) + { + if (Contains(it.Current)) + { + continue; + } + } - this.innerList.Add(nav); - } + Add(it.Current.Clone()); + } + } - /// <summary> - /// Removes all items from the list. - /// </summary> - public void Clear(){ - this.innerList.Clear(); - } + #endregion - /// <summary> - /// Determines whether the list contains a navigator positioned at the same - /// location as the specified XPathNavigator. This - /// method relies on the IsSamePositon() method of the XPathNavightor. - /// </summary> - /// <param name="value">The object to locate in the list.</param> - /// <returns>true if the object is found in the list; otherwise, false.</returns> - public bool Contains(XPathNavigator value){ - - foreach(XPathNavigator nav in this.innerList){ - if(nav.IsSamePosition(value)){ - return true; - } - } - return false; - } - /// <summary> - /// Determines whether the list contains a navigator whose Value property matches - /// the target value - /// </summary> - /// <param name="value">The value to locate in the list.</param> - /// <returns>true if the value is found in the list; otherwise, false.</returns> - public bool ContainsValue(string value){ - - foreach(XPathNavigator nav in this.innerList){ - if(nav.Value.Equals(value)){ - return true; - } - } - return false; - } + #region Public Methods - /// <summary> - /// Determines the index of a specific item in the list. - /// </summary> - /// <param name="value">The object to locate in the list</param> - /// <returns>The index of value if found in the list; otherwise, -1.</returns> - public int IndexOf( object value ){ - - return this.innerList.IndexOf(value as XPathNavigator); - } - /// <summary> - /// Inserts an item to the list at the specified position. - /// </summary> - /// <param name="index">The zero-based index at which value should be inserted. </param> - /// <param name="value">The object to insert into the list</param> - public void Insert(int index,XPathNavigator nav ){ + /// <summary> + /// Returns an enumerator for the entire list. + /// </summary> + /// <returns>An enumerator for the entire list</returns> + public IEnumerator<XPathNavigator> GetEnumerator() + { + return innerList.GetEnumerator(); + } - this.innerList.Insert(index, nav); - } + /// <summary> + /// Adds an item to the list + /// </summary> + /// <param name="value">The item to add</param> + public void Add(XPathNavigator nav) + { + innerList.Add(nav); + } - /// <summary> - /// Removes the first occurrence of a specific object from the list. - /// </summary> - /// <param name="value">The object to remove from the list.</param> - public void Remove(XPathNavigator nav){ - - for(int i = 0; i < this.Count; i++){ - if(nav.IsSamePosition((XPathNavigator) this.innerList[i])){ - this.innerList.RemoveAt(i); - return; - } - } - } - /// <summary> - /// Removes the list item at the specified index. - /// </summary> - /// <param name="index">The zero-based index of the item to remove.</param> - public void RemoveAt(int index){ - - this.innerList.RemoveAt(index); - } + /// <summary> + /// Removes all items from the list. + /// </summary> + public void Clear() + { + innerList.Clear(); + } + /// <summary> + /// Determines whether the list contains a navigator positioned at the same + /// location as the specified XPathNavigator. This + /// method relies on the IsSamePositon() method of the XPathNavightor. + /// </summary> + /// <param name="value">The object to locate in the list.</param> + /// <returns>true if the object is found in the list; otherwise, false.</returns> + public bool Contains(XPathNavigator value) + { - #endregion + foreach (XPathNavigator nav in innerList) + { + if (nav.IsSamePosition(value)) + { + return true; + } + } + return false; + } - } + /// <summary> + /// Determines whether the list contains a navigator whose Value property matches + /// the target value + /// </summary> + /// <param name="value">The value to locate in the list.</param> + /// <returns>true if the value is found in the list; otherwise, false.</returns> + public bool ContainsValue(string value) + { + + foreach (XPathNavigator nav in innerList) + { + if (nav.Value.Equals(value)) + { + return true; + } + } + return false; + } + + /// <summary> + /// Determines the index of a specific item in the list. + /// </summary> + /// <param name="value">The object to locate in the list</param> + /// <returns>The index of value if found in the list; otherwise, -1.</returns> + public int IndexOf(object value) + { + + return innerList.IndexOf(value as XPathNavigator); + } + + /// <summary> + /// Inserts an item to the list at the specified position. + /// </summary> + /// <param name="index">The zero-based index at which value should be inserted. </param> + /// <param name="value">The object to insert into the list</param> + public void Insert(int index, XPathNavigator nav) + { + + innerList.Insert(index, nav); + } + + + /// <summary> + /// Removes the first occurrence of a specific object from the list. + /// </summary> + /// <param name="value">The object to remove from the list.</param> + public void Remove(XPathNavigator nav) + { + + for (int i = 0; i < Count; i++) + { + if (nav.IsSamePosition(innerList[i])) + { + innerList.RemoveAt(i); + return; + } + } + } + + /// <summary> + /// Removes the list item at the specified index. + /// </summary> + /// <param name="index">The zero-based index of the item to remove.</param> + public void RemoveAt(int index) + { + + innerList.RemoveAt(index); + } + + #endregion + + } } |