PageRenderTime 20ms CodeModel.GetById 12ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/UiException/StackTraceParser.cs

#
C# | 106 lines | 45 code | 17 blank | 44 comment | 1 complexity | 1bebd5a72b55b70b003b7dcfd4daefb4 MD5 | raw file
  1// ****************************************************************
  2// This is free software licensed under the NUnit license. You may
  3// obtain a copy of the license at http://nunit.org
  4// ****************************************************************
  5
  6using System;
  7using System.Collections.Generic;
  8using System.Text;
  9using NUnit.UiException.StackTraceAnalyzers;
 10using NUnit.UiException.StackTraceAnalysers;
 11
 12namespace NUnit.UiException
 13{
 14    /// <summary>
 15    /// StackTraceParser is the entry class for analyzing and converting a stack
 16    /// trace - as given by .NET - into a manageable and ordered set of ErrorItem
 17    /// instances.
 18    ///   StackTraceParser contains internaly a set of autonom, independent and
 19    /// interchangeable algorithms that makes the analysis of the stack robust and
 20    /// opened to changes. Its architecture is designed to abstract callers from
 21    /// secondary details such as the type of culture or file system that can
 22    /// both affect the format of the final stack as provided by .NET.
 23    ///   In the future, this class could easily be extended by exposing a
 24    /// kind of register() method that would allow client code to append
 25    /// new algorithms of analysis in its internal list.
 26    /// </summary>
 27    public class StackTraceParser
 28    {
 29        /// <summary>
 30        /// Output list build from the StackTrace analyze .
 31        /// </summary>
 32        private ErrorItemCollection _items;
 33
 34        /// <summary>
 35        /// One or more algorithms designed to locate function names
 36        /// inside a stack trace line.
 37        /// </summary>
 38        private IErrorParser _functionParsers;
 39
 40        /// <summary>
 41        /// One or more algorithms designed to locate path names
 42        /// inside a stack strace line.
 43        /// </summary>
 44        private IErrorParser _pathParsers;
 45
 46        /// <summary>
 47        /// One or more algorithms designed to locate line number
 48        /// information inside a stack strace line.
 49        /// </summary>
 50        private IErrorParser _lineNumberParsers;
 51
 52        /// <summary>
 53        /// Build a new instance of StackTraceParser.
 54        /// </summary>
 55        public StackTraceParser()            
 56        {
 57            _items = new ErrorItemCollection();
 58            
 59            _functionParsers = new FunctionParser();
 60            _pathParsers = new PathCompositeParser();
 61            _lineNumberParsers = new LineNumberParser();
 62
 63            return;
 64        }
 65               
 66        /// <summary>
 67        /// Gives access to the collection of ErrorItem
 68        /// build during the analyze of the StackTrace.
 69        /// </summary>
 70        public ErrorItemCollection Items
 71        {
 72            get { return (_items); }
 73        }
 74
 75        /// <summary>
 76        /// Reads and transforms the given stack trace into a manageable and ordered
 77        /// set of ErrorItem instances. The resulting set is stored into Items property.
 78        /// </summary>
 79        /// <param name="stackTrace">A string value that should contain a .Net stack trace.</param>
 80        public void Parse(string stackTrace)
 81        {
 82            DefaultTextManager lines;
 83            RawError rawError;
 84
 85            _items.Clear();
 86
 87            lines = new DefaultTextManager();
 88            lines.Text = stackTrace;
 89
 90            foreach (string line in lines)
 91            {
 92                rawError = new RawError(line);
 93
 94                if (!_functionParsers.TryParse(this, rawError))
 95                    continue;
 96
 97                _pathParsers.TryParse(this, rawError);
 98                _lineNumberParsers.TryParse(this, rawError);
 99
100                _items.Add(rawError.ToErrorItem());
101            }
102
103            return;
104        }                                        
105    }
106}