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.Evaluated;
using System.Xml.XPath;
using System.Text.RegularExpressions;
using System.Collections.Specialized;
using Comnicate.CodeDom.Xml.ParserSchemas.Rules;

namespace Comnicate.CodeDom.Xml.ParserSchemas.Rules
{
    /// <summary>
    /// Represents the rule matching a processing instruction.
    /// </summary>
    public class ProcessingInstructionRule : NonParentalRule, IOptionalEntriesSupporter
    {
        #region Fields

        // Required name/value pairs
        private StringCollection requiredProperties = new StringCollection();

        // Optional name/value pairs
        private StringCollection optionalProperties = new StringCollection();

        // Optional entries match option
        private OptionalEntriesMatchOption optionalMatchOption = OptionalEntriesMatchOption.DoNotRequireAnyOptionalEntries;

        // Maximum count of optional pairs to match
        private int maximumOptionalEntriesToMatch;

        // Regex pattern for matching name/value pairs of the PI.
        private readonly Regex regexPattern = new Regex(@"(?<name>\b\w+\b)\s*=\s*(""(?<value>[^""]*)""|'(?<value>[^']*)'|(?<value>[^""'<> ]+)\s*)+");

        #endregion

        #region .ctors

        /// <summary>
        /// Initializes a new instance of the <see cref="ProcessingInstructionRule"/> class.
        /// </summary>
        /// <param name="piName">Processing instruction name.</param>
        public ProcessingInstructionRule(string piName) : base(piName, NodeValueOptions.GetValue) { }

        /// <summary>
        /// Initializes a new instance of the <see cref="ProcessingInstructionRule"/> class.
        /// </summary>
        /// <param name="piName">Processing instruction name.</param>
        /// <param name="valueOptions">PI value handling options.</param>
        public ProcessingInstructionRule(string piName, NodeValueOptions valueOptions) : base(piName, valueOptions) { }

        #endregion

        #region Properties

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

        /// <summary>
        /// Gets dictionary containing PI name/value pairs that MUST be present in this PI.
        /// </summary>
        public StringCollection RequiredProperties
        {
            get
            {
                return this.requiredProperties;
            }
        }

        /// <summary>
        /// Gets dictionary containing PI name/value pairs that CAN be present in this PI.
        /// </summary>
        public StringCollection OptionalProperties
        {
            get
            {
                return this.optionalProperties;
            }
        }

        /// <summary>
        /// Gets or sets optional entries choice option.
        /// </summary>
        public OptionalEntriesMatchOption OptionalEntriesMatchOption
        {
            get
            {
                return this.optionalMatchOption;
            }
            set
            {
                this.optionalMatchOption = value;
            }
        }

        /// <summary>
        /// Gets or sets maximum count of optional name/value pairs to match.
        /// </summary>
        public int MaximumOptionalEntriesToMatch
        {
            get
            {
                return this.maximumOptionalEntriesToMatch;
            }
            set
            {
                // Cannot be negative...
                if (value < 0) throw new ArgumentOutOfRangeException("value");

                // If user set 0 and requires at least one entry at the same time...
                if (value == 0 && this.optionalMatchOption == OptionalEntriesMatchOption.RequireAtLeastOneOptionalEntry)
                    throw new ArgumentException(Resources.ExceptionMsg_MaximumChildsCountToMatchOptionMisunderstood, "value");

                // Set the value
                this.maximumOptionalEntriesToMatch = value;
            }
        }

        /// <summary>
        /// Regex pattern for matching name/value pairs of the PI.
        /// </summary>
        protected Regex RegexPattern
        {
            get
            {
                return this.regexPattern;
            }
        }

        #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 override EvaluatedNonParentalRule Evaluate(XPathNavigator nodeNavigator)
        {
            // Null?
            if (nodeNavigator == null) throw new ArgumentNullException("nodeNavigator");

            // Match pairs of the PI
            MatchCollection matches = regexPattern.Matches(nodeNavigator.Value);

            // Create pairs collection
            NameValueCollection properties = new NameValueCollection(matches.Count);
            
            // Init counter
            int count = 0;

            // Go through found matches...
            foreach (Match match in matches)
            {
                // Is this an optional pair?
                if (this.optionalProperties.Contains(match.Groups["name"].Value))
                {
                    // If we exceeded the limit...
                    if (this.maximumOptionalEntriesToMatch != 0 && count >= this.maximumOptionalEntriesToMatch)
                        continue;

                    // Increment counter
                    count++;
                }

                // Insert the pair into the collection
                properties[match.Groups["name"].Value] = match.Groups["value"].Value;
            }

            // Go through the required pair names...
            foreach (string requiredPropertyName in this.requiredProperties)
                // If we didn't matched it...
                if (properties[requiredPropertyName] == null) 
                    throw new Evaluation.MissingNodeValueException(this, nodeNavigator);

            // Evaluated rule
            EvaluatedProcessingInstructionRule result = EvaluatedProcessingInstructionRule.Create(this);

            // Init pairs collection
            result.Properties = properties;

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