Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Parser Schemas– Easy and Powerful parsing of XML-based languages

, 18 Oct 2005
An article on parsing XML files according to the specified schema.
/***********************************************************************\
 * Comnicate.CodeDom.Xml.ParserSchemas                                 *
 * Parses xml-based languages according to a user defined schema.      *
 * Copyright � 2005 Tomas Deml (as Comnicate!)                         *
 *                  tomasdeml@msn.com                                  *
 *                                                                     *
 * This library is free software; you can redistribute it and/or       *
 * modify it under the terms of the GNU Lesser General Public          *
 * License as published by the Free Software Foundation; either        *
 * version 2.1 of the License, or (at your option) any later version.  *
 *                                                                     *
 * This library is distributed in the hope that it will be useful,     *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of      *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU   *
 * Lesser General Public License for more details.                     *
\***********************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.XPath;
using Comnicate.CodeDom.Xml.ParserSchemas.Rules.Evaluated;
using Comnicate.CodeDom.Xml.ParserSchemas.Rules.Evaluation;

namespace Comnicate.CodeDom.Xml.ParserSchemas.Rules
{
    /// <summary>
    /// Represents the abstract class for all evaluable rules.
    /// </summary>
    /// <typeparam name="T">Type of an instance representing an evaluated rule. It must be a subclass of the <see cref="EvaluatedRule"/> class.</typeparam>
    public abstract class EvaluableRule<T> : Rule, IEvaluableRule where T : EvaluatedRule
    {
        #region .ctors
        // All these .ctors must be internal to avoid unwanted inheritance. Only children 'ParentalRule' and 'NonParentalRule' are intended for inheritance.

        internal EvaluableRule(string nodeName) : base(nodeName) { }

        internal EvaluableRule(string nodeName, Uri namespaceUri) : base(nodeName, namespaceUri) { }

        internal EvaluableRule(string nodeName, NodeValueOptions options) : base(nodeName, options) { }

        internal EvaluableRule(string nodeName, Uri namespaceUri, NodeValueOptions options)
            : base(nodeName, namespaceUri, options) { }

        #endregion

        #region Properties

        /// <summary>
        /// Gets the information about the type of the node navigator that can be matched by this rule.
        /// </summary>
        public abstract XPathNodeType MatchingNodeType { get; }

        #endregion

        #region Events

        /// <summary>
        /// Occurs after the rule is evaluated.
        /// </summary>
        public event EventHandler<EvaluationEventArgs<EvaluatedRule>> OnRuleEvaluated;

        /// <summary>
        /// Occurs before the rule is evaluated.
        /// </summary>
        public event EventHandler<EvaluationEventArgs<Rule>> OnRuleEvaluation;
        
        #endregion

        #region Methods

        /// <summary>
        /// Parses the node navigator the XPathNavigator instance is pointed to, according to the rule.
        /// </summary>
        /// <param name="nodeNavigator">XPathNavigator instance pointing to the node navigator to parse.</param>
        /// <returns>Evaluated rule.</returns>
        public abstract T Evaluate(XPathNavigator nodeNavigator);

        /// <summary>
        /// Parses the node navigator the XPathNavigator instance is pointed to, according to the rule.
        /// </summary>
        /// <param name="nodeNavigator">XPathNavigator instance pointing to the node navigator to parse.</param>
        /// <returns>Evaluated rule.</returns>
        EvaluatedRule IEvaluableRule.Evaluate(XPathNavigator nodeNavigator)
        {
            return this.Evaluate(nodeNavigator);
        }

        /// <summary>
        /// Inspects the node navigator the XPathNavigator instance is pointed to and say if this rule can match the node.
        /// </summary>
        /// <param name="nodeNavigator">XPathNavigator pointing to the node.</param>
        /// <returns>True if the node can be parsed by this rule; False if it cannot be parsed with this rule.</returns>
        public override bool IsMatch(XPathNavigator nodeNavigator)
        {
            return (base.IsMatch(nodeNavigator) && nodeNavigator.NodeType == this.MatchingNodeType);
        }

        /// <summary>
        /// Returns text representation of the rule.
        /// </summary>
        /// <returns>Text representation of the rule.</returns>
        public override string ToString()
        {
            // Prefix
            string prefix = String.Empty;

            // Assign prefix depending on the type the rule matches
            switch (this.MatchingNodeType)
            {
                case XPathNodeType.Attribute:
                    prefix = "@";
                    break;
                case XPathNodeType.ProcessingInstruction:
                    prefix = "?";
                    break;
            }

            // No namespace specified...
            if (this.NamespaceUri == null)
                return String.Format("{0}{1}", prefix, this.NodeName);
            else
                return String.Format("{0}:{1}{2}", this.NamespaceUri, prefix, this.NodeName);
        }

        /// <summary>
        /// Raises the <see cref="OnRuleEvaluated"/> event.
        /// </summary>
        /// <param name="eventData">Evaluated rule.</param>
        void IEvaluableRule.RaiseRuleEvaluatedEvent(EvaluatedRule eventData)
        {
            // Any subscribers?
            if (this.OnRuleEvaluated != null && eventData != null)
            {
                // Copy rule cookie
                eventData.ruleCookie = this.ruleCookie;

                // Fire the event
                this.OnRuleEvaluated(this, new EvaluationEventArgs<EvaluatedRule>(eventData));
            }
        }

        /// <summary>
        /// Raises the <see cref="OnRuleEvaluation"/> event.
        /// </summary>
        /// <param name="eventData">Evaluated rule.</param>
        void IEvaluableRule.RaiseRuleEvaluationEvent(EvaluatedRule eventData)
        {
            // Any subscribers?
            if (this.OnRuleEvaluation != null && eventData != null)
            {
                // Copy rule cookie
                eventData.ruleCookie = this.ruleCookie;

                // Fire the event
                this.OnRuleEvaluation(this, new EvaluationEventArgs<Rule>(eventData));
            }
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Tomas Deml

Czech Republic Czech Republic
I'm a student of the Low-voltage Electrical Engineering specialized on Computing from the Czech republic.
 
I'm a C# kind of guy, fan of .NET.
 
I've formed a programming group called 'Comnicate!'. Currently the only member of the group is myself. Wink | ;-)

| Advertise | Privacy | Mobile
Web03 | 2.8.140902.1 | Last Updated 18 Oct 2005
Article Copyright 2005 by Tomas Deml
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid