Click here to Skip to main content
15,885,676 members
Articles / Programming Languages / XML

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

Rate me:
Please Sign up or sign in to vote.
4.00/5 (5 votes)
18 Oct 20058 min read 53.1K   495   44  
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 Comnicate.CodeDom.Xml.ParserSchemas.Rules.Evaluation;

namespace Comnicate.CodeDom.Xml.ParserSchemas.Rules
{
    /// <summary>
    /// Represents the abstract class for all rule supporting value validation but not supporting child rules evaluation.
    /// </summary>
    public abstract class ValidatingNonParentalRule : NonParentalRule, IValidatingRule
    {
        #region Fields

        // Value validator instance
        private IValueValidator valueValidator;

        #endregion

        #region .ctors

        /// <summary>
        /// Initializes a new instance of the <see cref="ValidatingNonParentalRule"/> class.
        /// </summary>
        /// <param name="nodeName">Node name.</param>
        protected ValidatingNonParentalRule(string nodeName) : base(nodeName, NodeValueOptions.GetValue | NodeValueOptions.RequireValue) { }

        /// <summary>
        /// Initializes a new instance of the <see cref="ValidatingNonParentalRule"/> class.
        /// </summary>
        /// <param name="nodeName">Node name.</param>
        /// <param name="valueValidator">An instance of a class implementing the <see cref="IValueValidator"/> interface that will validate the value of the attribute.</param>
        protected ValidatingNonParentalRule(string nodeName, IValueValidator valueValidator) : this(nodeName, null, NodeValueOptions.GetValue | NodeValueOptions.RequireValue, valueValidator) { }

        /// <summary>
        /// Initializes a new instance of the <see cref="ValidatingNonParentalRule"/> class.
        /// </summary>
        /// <param name="nodeName">Node name.</param>
        /// <param name="namespaceUri">Namespace uri.</param>
        protected ValidatingNonParentalRule(string nodeName, Uri namespaceUri) : base(nodeName, namespaceUri, NodeValueOptions.GetValue | NodeValueOptions.RequireValue) { }

        /// <summary>
        /// Initializes a new instance of the <see cref="ValidatingNonParentalRule"/> class.
        /// </summary>
        /// <param name="nodeName">Node name.</param>
        /// <param name="namespaceUri">Namespace uri.</param>
        /// <param name="valueValidator">An instance of a class implementing the <see cref="IValueValidator"/> interface that will validate the value of the attribute.</param>        
        protected ValidatingNonParentalRule(string nodeName, Uri namespaceUri, IValueValidator valueValidator) : this(nodeName, namespaceUri, NodeValueOptions.GetValue | NodeValueOptions.RequireValue, valueValidator) { }

        /// <summary>
        /// Initializes a new instance of the <see cref="ValidatingNonParentalRule"/> class.
        /// </summary>
        /// <param name="nodeName">Node name.</param>
        /// <param name="options">Options indicating how to resolve the node value.</param>
        protected ValidatingNonParentalRule(string nodeName, NodeValueOptions options) : base(nodeName, options) { }

        /// <summary>
        /// Initializes a new instance of the <see cref="ValidatingNonParentalRule"/> class.
        /// </summary>
        /// <param name="nodeName">Node name.</param>
        /// <param name="options">Options indicating how to resolve the node value.</param>
        /// <param name="valueValidator">An instance of a class implementing the <see cref="IValueValidator"/> interface that will validate the value of the attribute.</param>        
        protected ValidatingNonParentalRule(string nodeName, NodeValueOptions options, IValueValidator valueValidator) : this(nodeName, null, options, valueValidator) { }

        /// <summary>
        /// Initializes a new instance of the <see cref="ValidatingNonParentalRule"/> class.
        /// </summary>
        /// <param name="nodeName">Node name.</param>
        /// <param name="namespaceUri">Namespace Uri.</param>
        /// <param name="options">Options indicating how to resolve the node value.</param>
        protected ValidatingNonParentalRule(string nodeName, Uri namespaceUri, NodeValueOptions options) : base(nodeName, namespaceUri, options) { }

        /// <summary>
        /// Initializes a new instance of the <see cref="ValidatingNonParentalRule"/> class.
        /// </summary>
        /// <param name="nodeName">Node name.</param>
        /// <param name="namespaceUri">Namespace Uri.</param>
        /// <param name="options">Options indicating how to resolve the node value.</param>
        /// <param name="valueValidator">An instance of a class implementing the <see cref="IValueValidator"/> interface that will validate the value of the attribute.</param>                
        protected ValidatingNonParentalRule(string nodeName, Uri namespaceUri, NodeValueOptions options, IValueValidator valueValidator) : base(nodeName, namespaceUri, options)
        {
            // Init
            this.valueValidator = valueValidator;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the node value validator.
        /// </summary>
        public IValueValidator ValueValidator
        {
            get
            {
                return this.valueValidator;
            }
            set
            {
                if (value == null) throw new ArgumentNullException("value");                
                this.valueValidator = value;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Validates the node value.
        /// </summary>
        /// <param name="value">Value to validate.</param>
        public void ValidateValue(string value)
        {
            // Validator provided?
            if (this.ValueValidator != null)
                // Is value valid?
                if (!this.ValueValidator.IsValidValue(value, this))
                    throw new ValidationException(String.Format(Resources.ExceptionMsg_Formatable2_ValueIsInvalid, value, this.ToString()), this, null, null);
        }

        #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


Written By
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 | ;-)

Comments and Discussions