Exemplo n.º 1
0
        /// <summary>
        /// Resolves all abstract rules in the schema by replacing the
        /// <c>extends</c> elements by the contents of the abstract rule
        /// identified.
        /// </summary>
        /// <param name="xSchema">Validation schema</param>
        /// <param name="nsManager">Namespace manager</param>
        /// <exception cref="ArgumentNullException" />
        public static void ResolveAbstractRules(XDocument xSchema, XmlNamespaceManager nsManager)
        {
            if (xSchema == null)
            {
                throw new ArgumentNullException("xSchema");
            }

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

            // select abstract rules
            List<AbstractRule> listAbstractRules = new List<AbstractRule>();
            foreach (XElement xAbstractRule in xSchema.XPathSelectElements("//sch:rule[@abstract='true' and @id]", nsManager))
            {
                AbstractRule rule = new AbstractRule();
                rule.Id = xAbstractRule.Attribute(XName.Get("id")).Value;
                rule.Element = xAbstractRule;

                listAbstractRules.Add(rule);
            }

            if (listAbstractRules.Count > 0)
            {
                // select extends
                List<XElement> listExtends = new List<XElement>();
                foreach (XElement xExtends in xSchema.XPathSelectElements("//sch:extends[@rule]", nsManager))
                {
                    listExtends.Add(xExtends);
                }

                // replace selected extends with abstract rule contents
                foreach (XElement xExtends in listExtends)
                {
                    string rule = xExtends.Attribute(XName.Get("rule")).Value;

                    // select abstract rule contents
                    List<XElement> content = new List<XElement>();
                    foreach (AbstractRule abstractRule in listAbstractRules)
                    {
                        if (abstractRule.Id == rule)
                        {
                            content.AddRange(abstractRule.Element.Descendants());
                        }
                    }

                    // replace with content
                    if (content.Count > 0)
                    {
                        xExtends.ReplaceWith(content.ToArray());
                    }
                }

                // remove selected abstract rules
                foreach (AbstractRule abstractRule in listAbstractRules)
                {
                    XElement xAbstractRule = abstractRule.Element;
                    XElement xPattern = xAbstractRule.Parent;
                    xAbstractRule.Remove();
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Resolves all abstract rules in the schema by replacing the
        /// <c>extends</c> elements by the contents of the abstract rule
        /// identified.
        /// </summary>
        /// <param name="xSchema">Validation schema</param>
        /// <param name="nsManager">Namespace manager</param>
        /// <exception cref="ArgumentNullException" />
        public static void ResolveAbstractRules(XDocument xSchema, XmlNamespaceManager nsManager)
        {
            if (xSchema == null)
            {
                throw new ArgumentNullException("xSchema");
            }

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

            // select abstract rules
            List <AbstractRule> listAbstractRules = new List <AbstractRule>();

            foreach (XElement xAbstractRule in xSchema.XPathSelectElements("//sch:rule[@abstract='true' and @id]", nsManager))
            {
                AbstractRule rule = new AbstractRule();
                rule.Id      = xAbstractRule.Attribute(XName.Get("id")).Value;
                rule.Element = xAbstractRule;

                listAbstractRules.Add(rule);
            }

            if (listAbstractRules.Count > 0)
            {
                // select extends
                List <XElement> listExtends = new List <XElement>();
                foreach (XElement xExtends in xSchema.XPathSelectElements("//sch:extends[@rule]", nsManager))
                {
                    listExtends.Add(xExtends);
                }

                // replace selected extends with abstract rule contents
                foreach (XElement xExtends in listExtends)
                {
                    string rule = xExtends.Attribute(XName.Get("rule")).Value;

                    // select abstract rule contents
                    List <XElement> content = new List <XElement>();
                    foreach (AbstractRule abstractRule in listAbstractRules)
                    {
                        if (abstractRule.Id == rule)
                        {
                            content.AddRange(abstractRule.Element.Descendants());
                        }
                    }

                    // replace with content
                    if (content.Count > 0)
                    {
                        xExtends.ReplaceWith(content.ToArray());
                    }
                }

                // remove selected abstract rules
                foreach (AbstractRule abstractRule in listAbstractRules)
                {
                    XElement xAbstractRule = abstractRule.Element;
                    XElement xPattern      = xAbstractRule.Parent;
                    xAbstractRule.Remove();
                }
            }
        }