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 Comnicate.CodeDom.Xml.ParserSchemas.Rules.Evaluation;
using Comnicate.CodeDom.Xml.ParserSchemas.Rules.Evaluated;
using System.Xml.XPath;

namespace Comnicate.CodeDom.Xml.ParserSchemas.Rules
{
    /// <summary>
    /// Represents the rule matching an element and its child rules and then it transforms gathered information into an object using a provided method. 
    /// </summary>
    /// <typeparam name="T">Type of the transformed object.</typeparam>
    public class ElementTransformationRule<T> : ParentalRule, ITransformableRule<T>
    {
        #region Fields

        // Node transformer
        private INodeTransformer<T> nodeTransformer;

        #endregion

        #region .ctors

        /// <summary>
        /// Initializes a new instance of the <see cref="ElementTransformationRule&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="nodeName">Node name.</param>
        /// <param name="nodeTransformer">Instance of a class implementing the <see cref="INodeTransformer&lt;T&gt;"/> interface that will transform this rule into an object.</param>
        public ElementTransformationRule(string nodeName, INodeTransformer<T> nodeTransformer) : this(nodeName, null, NodeValueOptions.IgnoreValue, nodeTransformer) { }

        /// <summary>
        /// Initializes a new instance of the <see cref="ElementTransformationRule&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="nodeName">Node name.</param>
        /// <param name="valueOptions">Node value handling options.</param>
        /// <param name="nodeTransformer">Instance of a class implementing the <see cref="INodeTransformer&lt;T&gt;"/> interface that will transform this rule into an object.</param>        
        public ElementTransformationRule(string nodeName, NodeValueOptions valueOptions, INodeTransformer<T> nodeTransformer)
            : this(nodeName, null, valueOptions, nodeTransformer) { }

        /// <summary>
        /// Initializes a new instance of the <see cref="ElementTransformationRule&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="nodeName">Node name.</param>
        /// <param name="namespaceUri">Namespace Uri.</param>
        /// <param name="nodeTransformer">Instance of a class implementing the <see cref="INodeTransformer&lt;T&gt;"/> interface that will transform this rule into an object.</param>                
        public ElementTransformationRule(string nodeName, Uri namespaceUri, INodeTransformer<T> nodeTransformer)
            : this(nodeName, namespaceUri, NodeValueOptions.IgnoreValue, nodeTransformer) { }

        /// <summary>
        /// Initializes a new instance of the <see cref="ElementTransformationRule&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="nodeName">Node name.</param>
        /// <param name="namespaceUri">Namespace Uri.</param>
        /// <param name="valueOptions">Node value handling options.</param>
        /// <param name="nodeTransformer">Instance of a class implementing the <see cref="INodeTransformer&lt;T&gt;"/> interface that will transform this rule into an object.</param>                        
        public ElementTransformationRule(string nodeName, Uri namespaceUri, NodeValueOptions valueOptions, INodeTransformer<T> nodeTransformer) : base(nodeName, namespaceUri, valueOptions)
        {
            // Check node transformer
            if (nodeTransformer == null) throw new ArgumentNullException("nodeTransformer");

            // Init it
            this.nodeTransformer = nodeTransformer;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets the information about the type of node navigator that can be matched by this rule.
        /// </summary>
        public override XPathNodeType MatchingNodeType
        {
            get
            {
                return System.Xml.XPath.XPathNodeType.Element;
            }
        }

        /// <summary>
        /// Gets the instance of the class implementing the <see cref="INodeTransformer&lt;T&gt;"/> interface that will transform this rule into an object.
        /// </summary>
        public INodeTransformer<T> NodeTransformer
        {
            get 
            {
                return this.nodeTransformer;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Parses the node navigator the XPathNavigator instance is pointed to, according to the rule and transforms the rule using the <see cref="INodeTransformer&lt;T&gt;"/>.
        /// </summary>
        /// <param name="nodeNavigator">XPathNavigator instance pointing to the node navigator to parse.</param>
        /// <returns>Evaluated rule.</returns>
        public override EvaluatedParentalRule Evaluate(XPathNavigator nodeNavigator)
        {
            // Evaluated rule
            EvaluatedElementTransformationRule<T> result = EvaluatedElementTransformationRule<T>.Create(this);

            // Evaluate child rules
            this.EvaluateChildRules(nodeNavigator, result);

            // Transform the rule
            result.TransformationResult = this.nodeTransformer.Transform(result);

            // Return evaluated eventData
            return result;
        }

        #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
Web03 | 2.8.1411019.1 | Last Updated 18 Oct 2005
Article Copyright 2005 by Tomas Deml
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid