Click here to Skip to main content
11,486,673 members (73,898 online)
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 43.1K 448 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;

namespace Comnicate.CodeDom.Xml.ParserSchemas.Rules.Evaluated
{
    /// <summary>
    /// Represents the abstract class for all evaluated rules.
    /// </summary>
    public abstract class EvaluatedRule : Rule
    {
        #region Fields

        // Node value
        private string value;

        #endregion

        #region .ctors

        /// <summary>
        /// Not intentended for an external access.
        /// </summary>
        /// <param name="rule">Rule</param>
        internal EvaluatedRule(Rule rule) : base(rule.NodeName, rule.NamespaceUri, rule.NodeValueOptions) { }

        #endregion

        #region Properties

        /// <summary>
        /// Gets the node value as string.
        /// </summary>
        public string Value
        {
            get
            {
                return this.value;
            }
            protected internal set
            {
                this.value = value;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Informs about the equality of two rules.
        /// </summary>
        /// <param name="obj">Another rule.</param>
        /// <returns>True if the rules are equal; False if they are different.</returns>
        public override bool Equals(object obj)
        {
            // Cast
            EvaluatedRule other = obj as EvaluatedRule;

            // Null?
            if (object.ReferenceEquals(other, null)) return false;

            // Base equals
            if (!base.Equals(other)) return false;

            // Check properties
            if (other.value != this.value) return false;

            // All passed
            return true;
        }

        /// <summary>
        /// Return unique code representing this rule.
        /// </summary>
        /// <returns>Rule hash code.</returns>
        public override int GetHashCode()
        {
            return (base.GetHashCode() & this.value.GetHashCode());
        }

        /// <summary>
        /// == operator overload.
        /// </summary>
        /// <param name="first">First rule.</param>
        /// <param name="second">Second rule.</param>
        /// <returns>True if the rules are equal; False if they are different.</returns>
        public static bool operator ==(EvaluatedRule first, EvaluatedRule second)
        {
            return object.Equals(first, second);
        }

        /// <summary>
        /// != operator overload.
        /// </summary>
        /// <param name="first">First rule.</param>
        /// <param name="second">Second rule.</param>
        /// <returns>True if the rules are not equal; False if they are equal.</returns>
        public static bool operator !=(EvaluatedRule first, EvaluatedRule second)
        {
            return !object.Equals(first, second);
        }

        /// <summary>
        /// Gets the node value.
        /// </summary>
        /// <typeparam name="T">Desired type of the value.</typeparam>
        /// <returns>Node value.</returns>
        public T ValueAs<T>()
        {
            return (T)Convert.ChangeType(this.value, typeof(T));
        }

        /// <summary>
        /// Gets the node value.
        /// </summary>
        /// <typeparam name="T">Desired type of the value.</typeparam>
        /// <param name="formatProvider">Value format provider.</param>
        /// <returns>Node value.</returns>
        public T ValueAs<T>(IFormatProvider formatProvider)
        {
            return (T)Convert.ChangeType(this.value, typeof(T), formatProvider);
        }

        /// <summary>
        /// Gets the node value.
        /// </summary>
        /// <param name="targetType">Value type.</param>
        /// <returns>Node value.</returns>
        public object ValueAs(Type targetType)
        {
            return Convert.ChangeType(this.value, targetType);
        }

        /// <summary>
        /// Gets the node value.
        /// </summary>
        /// <param name="targetType">Value type.</param>
        /// <param name="formatProvider">Value format provider.</param>
        /// <returns>Node value.</returns>
        public object ValueAs(Type targetType, IFormatProvider formatProvider)
        {
            return Convert.ChangeType(this.value, targetType, formatProvider);
        }

        /// <summary>
        /// Gets the node value.
        /// </summary>
        /// <param name="targetType">Value type.</param>
        /// <returns>Node value.</returns>
        public object ValueAs(TypeCode targetType)
        {
            return Convert.ChangeType(this.value, targetType);
        }

        /// <summary>
        /// Gets the node value.
        /// </summary>
        /// <param name="targetType">Value type.</param>
        /// <param name="formatProvider">Value format provider.</param>
        /// <returns>Node value.</returns>
        public object ValueAs(TypeCode targetType, IFormatProvider formatProvider)
        {
            return Convert.ChangeType(this.value, targetType, formatProvider);
        }

        #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 | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 18 Oct 2005
Article Copyright 2005 by Tomas Deml
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid