A Rule instance encapsulates a validation rule that can be tested against a XmlDocument.
Inheritance: Validator
Exemplo n.º 1
0
        /// <summary>
        /// Parse the <b>RuleSet</b> definitions in the configuration file
        /// indicated by the URI.
        /// </summary>
        /// <param name="uri">The URI of the business rule configuration file.</param>
        private static void ParseRuleSets(string uri)
        {
            RuleSet   ruleSet = null;
            XmlReader reader  = XmlReader.Create(uri);

            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                case XmlNodeType.Element:
                {
                    if (reader.LocalName.Equals("forceLoad"))
                    {
                        string platform       = reader ["platform"];
                        string implementation = reader ["class"];

                        if (platform.Equals(".Net"))
                        {
                            try {
                                Type type = Type.GetType(implementation);

                                if (type != null)
                                {
                                    // Access each member to ensure it has a chance to initialise
                                    MemberInfo [] members = type.GetMembers();
                                    if (members != null)
                                    {
                                        foreach (MemberInfo member in members)
                                        {
                                            if (member.MemberType == MemberTypes.Field)
                                            {
                                                type.InvokeMember(member.Name, BindingFlags.GetField, null, null, null);
                                            }
                                        }
                                    }
                                    break;
                                }
                                else
                                {
                                    logger.Error("Could not find load rule class '" + implementation + "'");
                                }
                            }
                            catch (Exception error) {
                                logger.Error("Could not force load rule class '" + implementation + "'", error);
                            }
                        }
                    }
                    else if (reader.LocalName.Equals("ruleSet"))
                    {
                        string name = reader ["name"];

                        ruleSet = new RuleSet(name);
                    }
                    else if (reader.LocalName.Equals("addRule"))
                    {
                        string name = reader ["name"];
                        Rule   rule = Rule.ForName(name);

                        if (rule != null)
                        {
                            if (ruleSet != null)
                            {
                                ruleSet.Add(rule);
                            }
                            else
                            {
                                logger.Error("Syntax error in rule file - addRule outside of RuleSet");
                            }
                        }
                        else
                        {
                            logger.Error("Reference to undefined rule '" + name + "' in addRule");
                        }
                    }
                    else if (reader.LocalName.Equals("removeRule"))
                    {
                        string name = reader ["name"];
                        Rule   rule = Rule.ForName(name);

                        if (rule != null)
                        {
                            if (ruleSet != null)
                            {
                                ruleSet.Remove(rule);
                            }
                            else
                            {
                                logger.Error("Syntax error in rule file - removeRule outside of RuleSet");
                            }
                        }
                        else
                        {
                            logger.Error("Reference to undefined rule '" + name + "' in addRule");
                        }
                    }
                    else if (reader.LocalName.Equals("addRuleSet"))
                    {
                        string  name  = reader ["name"];
                        RuleSet rules = ForName(name);

                        if (rules != null)
                        {
                            if (ruleSet != null)
                            {
                                if (rules != ruleSet)
                                {
                                    ruleSet.Add(rules);
                                }
                                else
                                {
                                    logger.Error("Attempt to recursively define ruleset '" + name + "'");
                                }
                            }
                            else
                            {
                                logger.Error("Syntax error in rule file - addRuleSet outside of RuleSet");
                            }
                        }
                        else
                        {
                            logger.Error("Reference to undefined rule '" + name + "' in addRule");
                        }
                    }
                    break;
                }

                case XmlNodeType.EndElement:
                {
                    if (reader.LocalName.Equals("ruleSet"))
                    {
                        ruleSet = null;
                    }

                    break;
                }
                }
            }
            reader.Close();
        }
Exemplo n.º 2
0
 /// <summary>
 /// Attempts to remove a given <see cref="Rule"/> from the collection
 /// held by the <B>RuleSet</B>.
 /// </summary>
 /// <param name="rule">The <see cref="Rule"/> to be removed.</param>
 /// <returns> The <see cref="Rule"/> instance removed from the set or
 /// <c>null</c> if there was no match.</returns>
 public Rule Remove(Rule rule)
 {
     return(Remove(rule.Name));
 }
Exemplo n.º 3
0
 /// <summary>
 /// Attempts to remove a given <see cref="Rule"/> from the collection
 /// held by the <B>RuleSet</B>.
 /// </summary>
 /// <param name="rule">The <see cref="Rule"/> to be removed.</param>
 /// <returns> The <see cref="Rule"/> instance removed from the set or
 /// <c>null</c> if there was no match.</returns>
 public Rule Remove(Rule rule)
 {
     return (Remove (rule.Name));
 }
Exemplo n.º 4
0
 /// <summary>
 /// Adds the indicated <see cref="Rule"/> instance to the <B>RuleSet</B>
 /// A <see cref="Rule"/> may be referenced by several <B>RuleSet</B>
 /// instances simultaneously.
 /// <para>If the <see cref="Rule"/> has the same name as a previously
 /// added one then it will replace it. This feature can be used to
 /// overwrite standard rules with customized ones.</para>
 /// </summary>
 /// <param name="rule">The <see cref="Rule"/> to be added.</param>
 public void Add(Rule rule)
 {
     rules [rule.Name] = rule;
 }
Exemplo n.º 5
0
 /// <summary>
 /// Adds the indicated <see cref="Rule"/> instance to the <B>RuleSet</B>
 /// A <see cref="Rule"/> may be referenced by several <B>RuleSet</B>
 /// instances simultaneously.
 /// <para>If the <see cref="Rule"/> has the same name as a previously
 /// added one then it will replace it. This feature can be used to
 /// overwrite standard rules with customized ones.</para>
 /// </summary>
 /// <param name="rule">The <see cref="Rule"/> to be added.</param>
 public void Add(Rule rule)
 {
     rules [rule.Name] = rule;
 }