예제 #1
0
        /// <summary>
        /// Resolves all inclusions by replacing each <c>include</c> element by the
        /// resource it references.
        /// </summary>
        /// <remarks>
        /// Potentially infinite <c>include</c> recursion is terminated after
        /// a finite number of steps.
        /// </remarks>
        /// <param name="xSchema">Validation schema.</param>
        /// <param name="resolver">Resolver of included elements</param>
        /// <param name="nsManager">Namespace manager</param>
        /// <exception cref="InvalidOperationException">If the <c>include</c>
        /// recursion exceeds a limit.</exception>
        /// <exception cref="ArgumentNullException" />
        public static void ResolveInclusions(XDocument xSchema, IInclusionResolver resolver, XmlNamespaceManager nsManager)
        {
            if (xSchema == null)
            {
                throw new ArgumentNullException("xSchema");
            }

            if (nsManager == null)
            {
                throw new ArgumentNullException("nsManager");
            }

            int maxSteps = 500; // recursion termination limit
            int i;

            for (i = 0; i < maxSteps; i++)
            {
                // select inclusions
                List <XElement> listIncludes = new List <XElement>();
                foreach (XElement xInclude in xSchema.XPathSelectElements("//sch:include", nsManager))
                {
                    listIncludes.Add(xInclude);
                }

                if (listIncludes.Count == 0)
                {
                    break;
                }

                // replace inclusions
                foreach (XElement xInclude in listIncludes)
                {
                    string    href  = xInclude.Attribute(XName.Get("href")).Value;
                    XDocument xHref = resolver.Resolve(href);
                    xInclude.ReplaceWith(xHref.Root);
                }
            }

            if (maxSteps == i)
            {
                throw new InvalidOperationException(String.Format(
                                                        "There is a possibility of infinite recursion of include elements. " +
                                                        "Terminated after the maximum of {0} steps.", maxSteps));
            }
        }
예제 #2
0
        /// <summary>
        /// Resolves all inclusions by replacing each <c>include</c> element by the
        /// resource it references.
        /// </summary>
        /// <remarks>
        /// Potentially infinite <c>include</c> recursion is terminated after
        /// a finite number of steps.
        /// </remarks>
        /// <param name="xSchema">Validation schema.</param>
        /// <param name="resolver">Resolver of included elements</param>
        /// <param name="nsManager">Namespace manager</param>
        /// <exception cref="InvalidOperationException">If the <c>include</c> 
        /// recursion exceeds a limit.</exception>
        /// <exception cref="ArgumentNullException" />
        public static void ResolveInclusions(XDocument xSchema, IInclusionResolver resolver, XmlNamespaceManager nsManager)
        {
            if (xSchema == null)
            {
                throw new ArgumentNullException("xSchema");
            }

            if (nsManager == null)
            {
                throw new ArgumentNullException("nsManager");
            }

            int maxSteps = 500; // recursion termination limit
            int i;
            for (i = 0; i < maxSteps; i++)
            {
                // select inclusions
                List<XElement> listIncludes = new List<XElement>();
                foreach (XElement xInclude in xSchema.XPathSelectElements("//sch:include", nsManager))
                {
                    listIncludes.Add(xInclude);
                }

                if (listIncludes.Count == 0)
                {
                    break;
                }

                // replace inclusions
                foreach (XElement xInclude in listIncludes)
                {
                    string href = xInclude.Attribute(XName.Get("href")).Value;
                    XDocument xHref = resolver.Resolve(href);
                    xInclude.ReplaceWith(xHref.Root);
                }
            }

            if (maxSteps == i)
            {
                throw new InvalidOperationException(String.Format(
                    "There is a possibility of infinite recursion of include elements. " +
                    "Terminated after the maximum of {0} steps.", maxSteps));
            }
        }