Click here to Skip to main content
15,891,848 members
Articles / Programming Languages / C#

NAnt Task to Capture Out of Bounds Source Monitor Metrics

Rate me:
Please Sign up or sign in to vote.
3.00/5 (1 vote)
12 Jun 2009CPOL5 min read 22.2K   61   6  
NAnt user task that adds alarms on top of Source Monitor metrics to highlight source code that does not follow coding rules.
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;

namespace NAnt.SMAnalyzerTasks
{
    /// <summary>
    /// Class to read Source Monitor XML detailed file.
    /// </summary>
    /// <remarks>
    /// This class has been developed and tested for Source Monitor version 2.5.
    /// </remarks>
    class SMFileReader : ISMFileReader
    {
        private string m_sSMFileName;
        private string m_sVersion;
        private string m_sProjectName;
        private StreamReader m_Stream = null;
        private XmlReader m_Reader = null;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="sFileReader">the file name of the Source Monitor File to read.</param>
        public SMFileReader(string sFileReader)
        {
            m_sSMFileName = sFileReader;
            m_sVersion = "";
            m_sProjectName = "";
        }

        /// <summary>
        /// Open the XML file to read.
        /// </summary>
        /// <remarks>
        /// Open the file and read it version.
        /// <br>Initialize internal reader objects m_Stream and m_Reader.</br>
        /// <br>Read the version of the XML file.</br>
        /// </remarks>
        /// <returns>true if opening of the file has been successfull.</returns>
        public bool Open()
        {
            try
            {
                m_Stream = new StreamReader(m_sSMFileName);
                m_Reader = XmlReader.Create(m_Stream);
                m_sVersion = _ReadVersion();
                m_sProjectName = _ReadProjectName();
                return true;
            }
            catch (Exception)
            {
                m_Stream = null;
                m_Reader = null;
            }
            return false;
        }
        
        /// <summary>
        /// Close the XML file beeing read.
        /// </summary>
        public void Close()
        {
            if (null != m_Stream)
            {
                m_Stream.Close();
            }
            if (null != m_Reader)
            {
                m_Reader.Close();
            }
        }

        public string Version
        {
            get { return m_sVersion; }
        }

        public string ProjectName
        {
            get { return m_sProjectName; }
        }


        /// <summary>
        /// Read one file section from the XML file. 
        /// </summary>
        /// <param name="_Metrics">the list of metrics found</param>
        /// <see cref="GetNextFile"/>
        /// <returns>true if the read has been successful and can continue.</returns>
        public bool GetFirstFile(out SMFileMetrics _Metrics)
        {
            bool bFound = false;
            SMFileMetrics oMetrics= null;
            try
            {
                m_Reader.Read();
                m_Reader.MoveToContent();
                m_Reader.ReadToDescendant("checkpoints");
                m_Reader.ReadToDescendant("checkpoint");
                m_Reader.ReadToDescendant("files");
                m_Reader.ReadToFollowing("file");
                _ReadOneFile(m_Reader.ReadSubtree(), out oMetrics);
                bFound = true;
            }
            catch (Exception)
            {}
            _Metrics = oMetrics;
            return bFound;
        }

        /// <summary>
        /// Read next file section from the XML file.
        /// </summary>
        /// <remarks>must be called after an inital call to GetFirstFile().</remarks>
        /// <see cref="GetFirstFile"/>
        /// <param name="_Metrics">the metrics found</param>
        /// <returns>true if the read has been successful and can continue</returns>
        public bool GetNextFile(out SMFileMetrics _Metrics)
        {
            SMFileMetrics oMetrics = null;
            bool bNext = m_Reader.ReadToNextSibling("file");
            if (bNext)
            {
                _ReadOneFile(m_Reader.ReadSubtree(), out oMetrics);
            }
            _Metrics = oMetrics;
            return bNext;
        }

        /// <summary>
        /// Read the current file within the XML structure.
        /// </summary>
        /// <param name="_oReader">the current XML reader to use</param>
        /// <param name="_Metrics">the metrics list read form the structure.</param>
        private void _ReadOneFile(XmlReader _oReader, out SMFileMetrics _Metrics) 
        {
            SMFileMetrics oMetrics= null;
            try
            {
                _oReader.MoveToContent();
                string sFileName = _oReader.GetAttribute("file_name");
                string sMetricId;
                string sMetricValue;

                oMetrics = new SMFileMetrics(sFileName);
                _oReader.ReadToFollowing("metric");
                do
                {
                    sMetricId = _oReader.GetAttribute("id");
                    sMetricValue= _oReader.ReadString();
                    oMetrics.AddMetric(sMetricId, sMetricValue);
                }
                while (_oReader.ReadToNextSibling("metric"));
                _oReader.Close();
            }
            catch (Exception)
            {
                oMetrics = null;
            }
            _Metrics = oMetrics;
        }

        /// <summary>
        /// read the version of the xml file
        /// </summary>
        /// <returns>return the value of //sourcemonitor_metrics/project[@version]</returns>
        private string _ReadVersion()
        {
            string sVersion = "";
            if (null != m_Reader)
            {
                while (m_Reader.Read())
                {
                    if (m_Reader.NodeType == XmlNodeType.Element)
                    {
                        if (m_Reader.Name == "project")
                        {
                            sVersion = m_Reader.GetAttribute("version");
                            break;
                        }
                    }
                }
            }
            return sVersion;
        }

        private string _ReadProjectName()
        {
            string sProjectName = "";
            if (null != m_Reader)
            {
                m_Reader.MoveToContent();
                m_Reader.ReadToFollowing("project_name");
                sProjectName = m_Reader.ReadElementString();
            }
            return sProjectName;
        }
    }
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Schlumberger
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions