Ejemplo n.º 1
0
 public bool LoadResults(SourceCode sourceCode, SourceParser parser, DateTime writeTime, DateTime settingsTimeStamp)
 {
     bool flag = false;
     lock (this)
     {
         XmlNode item = null;
         XmlDocument document = this.OpenResultsCache(sourceCode, parser, out item);
         if ((document == null) || (item == null))
         {
             return flag;
         }
         try
         {
             XmlElement timeStampNode = item["settings"];
             if (((timeStampNode != null) && IsNodeUpToDate(timeStampNode, settingsTimeStamp)) && IsNodeUpToDate(item, writeTime))
             {
                 XmlNode parentNode = item.SelectSingleNode("violations");
                 if ((parentNode != null) && parser.ImportViolations(sourceCode, parentNode))
                 {
                     flag = true;
                 }
             }
         }
         catch (XmlException)
         {
         }
         if (!this.documentHash.ContainsKey(sourceCode.Project.Location))
         {
             this.documentHash.Add(sourceCode.Project.Location, document);
         }
     }
     return flag;
 }
Ejemplo n.º 2
0
 protected SourceCode(CodeProject project, SourceParser parser)
 {
     this.violations = new Dictionary<string, Violation>();
     Param.RequireNotNull(project, "project");
     Param.RequireNotNull(parser, "parser");
     this.project = project;
     this.parser = parser;
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the SourceCode class.
        /// </summary>
        /// <param name="project">The project that contains this document.</param>
        /// <param name="parser">The parser that created this document.</param>
        protected SourceCode(CodeProject project, SourceParser parser)
        {
            Param.RequireNotNull(project, "project");
            Param.RequireNotNull(parser, "parser");

            this.project = project;
            this.parser = parser;
        }
Ejemplo n.º 4
0
 public CodeFile(string path, CodeProject project, SourceParser parser, IEnumerable<Configuration> configurations)
     : base(project, parser, configurations)
 {
     Param.RequireNotNull(path, "path");
     Param.RequireNotNull(project, "project");
     Param.RequireNotNull(parser, "parser");
     this.path = path;
     if ((!path.StartsWith(@"\\", StringComparison.Ordinal) && (path.Length >= 2)) && (path[1] != ':'))
     {
         string currentDirectory = Directory.GetCurrentDirectory();
         if (currentDirectory.EndsWith(@"\", StringComparison.Ordinal))
         {
             currentDirectory = currentDirectory.Substring(0, currentDirectory.Length - 1);
         }
         if (path.StartsWith(@"\", StringComparison.Ordinal))
         {
             path = currentDirectory.Substring(0, 2) + path;
         }
         else
         {
             path = currentDirectory + @"\" + path;
         }
     }
     int length = path.LastIndexOf(@"\", StringComparison.Ordinal);
     if (-1 == length)
     {
         this.name = this.path;
     }
     else
     {
         this.name = path.Substring(length + 1, (path.Length - length) - 1);
         this.folder = path.Substring(0, length);
         if (this.folder != null)
         {
             this.folder = StyleCopCore.CleanPath(this.folder);
         }
     }
     length = this.name.LastIndexOf(".", StringComparison.Ordinal);
     if (-1 == length)
     {
         this.fileType = string.Empty;
     }
     else
     {
         this.fileType = this.name.Substring(length + 1, (this.name.Length - length) - 1).ToUpperInvariant();
     }
 }
 public override void AddParser(SourceParser parser)
 {
     Param.RequireNotNull(parser, "parser");
     ICollection<string> fileTypes = parser.FileTypes;
     if (fileTypes != null)
     {
         foreach (string str in fileTypes)
         {
             if (str != null)
             {
                 List<SourceParser> list = null;
                 if (!this.fileTypes.TryGetValue(str, out list))
                 {
                     list = new List<SourceParser>(1);
                     this.fileTypes.Add(str, list);
                 }
                 list.Add(parser);
             }
         }
     }
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Runs the analyzers against the given document.
        /// </summary>
        /// <param name="document">The document to analyze.</param>
        /// <param name="parser">The parser that created the document.</param>
        /// <param name="analyzers">The analyzers to run against the document.</param>
        /// <param name="passNumber">The current pass number.</param>
        /// <returns>Returns true if analysis was run, or false if analysis was delayed until the next pass.</returns>
        private bool TestAndRunAnalyzers(
            CodeDocument document, SourceParser parser, IEnumerable<SourceAnalyzer> analyzers, int passNumber)
        {
            Param.AssertNotNull(document, "document");
            Param.AssertNotNull(parser, "parser");
            Param.Ignore(analyzers);
            Param.Ignore(passNumber);

            if (analyzers == null)
            {
                return true;
            }

            // Determine whether any of the analyzers wish to delay parsing until the next pass.
            bool delay = false;
            foreach (SourceAnalyzer analyzer in analyzers)
            {
                if (analyzer.DelayAnalysis(document, passNumber))
                {
                    delay = true;
                    break;
                }
            }

            if (!delay)
            {
                this.RunAnalyzers(document, parser, analyzers);
            }

            return !delay;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Runs the list of analyzers against the given document.
        /// </summary>
        /// <param name="document">The document to analyze.</param>
        /// <param name="parser">The parser that created the document.</param>
        /// <param name="analyzers">The list of analyzsers to run against the document.</param>
        private void RunAnalyzers(
            CodeDocument document, SourceParser parser, IEnumerable<SourceAnalyzer> analyzers)
        {
            Param.AssertNotNull(document, "document");
            Param.AssertNotNull(parser, "parser");
            Param.Ignore(analyzers, "analyzers");

            if (analyzers != null)
            {
                if (parser.SkipAnalysisForDocument(document))
                {
                    this.data.Core.SignalOutput(
                        MessageImportance.Normal,
                        string.Format(CultureInfo.CurrentCulture, "Skipping {0}...", document.SourceCode.Name));
                }
                else
                {
                    // Loop through each of the analyzers attached to the parser.
                    foreach (SourceAnalyzer analyzer in analyzers)
                    {
                        // Make sure the user hasn't cancelled us.
                        if (this.data.Core.Cancel)
                        {
                            break;
                        }

                        SourceParser.ClearAnalyzerTags(document);
                        try
                        {
                            analyzer.AnalyzeDocument(document);
                        }
                        catch (System.Exception)
                        {
                            string details = string.Format(
                                    CultureInfo.CurrentCulture,
                                    "Exception thrown by analyzer '{0}' while processing '{1}'.",
                                    analyzer.Name,
                                    document.SourceCode.Path);

                            this.data.Core.SignalOutput(MessageImportance.High, details);
                            throw;
                        }
                    }
                }
            }
        }
Ejemplo n.º 8
0
 public CodeFile(string path, CodeProject project, SourceParser parser)
     : this(path, project, parser, null)
 {
 }
Ejemplo n.º 9
0
 public abstract void AddParser(SourceParser parser);
Ejemplo n.º 10
0
 /// <summary>
 /// Creates a new <see cref="CodeFile"/> instance with the given values.
 /// </summary>
 /// <param name="path">The path to the code file.</param>
 /// <param name="project">The project that contains this file.</param>
 /// <param name="parser">The parser that created this file object.</param>
 /// <param name="context">Optional context information.</param>
 /// <returns>Returns the newly created <see cref="CodeFile"/>.</returns>
 protected virtual CodeFile CreateCodeFile(string path, CodeProject project, SourceParser parser, object context)
 {
     Param.Ignore(path, project, parser, context);
     return new CodeFile(path, project, parser);
 }
Ejemplo n.º 11
0
 internal void SetParser(SourceParser item)
 {
     this.parser = item;
 }
Ejemplo n.º 12
0
 private XmlDocument OpenResultsCache(SourceCode sourceCode, SourceParser parser, out XmlNode item)
 {
     item = null;
     XmlDocument document = null;
     try
     {
         lock (this)
         {
             if (this.documentHash.TryGetValue(sourceCode.Project.Location, out document))
             {
                 item = document.DocumentElement.SelectSingleNode(string.Format(CultureInfo.InvariantCulture, "sourcecode[@name=\"{0}\"][@parser=\"{1}\"]", new object[] { sourceCode.Name, parser.Id }));
                 return document;
             }
             document = this.core.Environment.LoadResultsCache(sourceCode.Project.Location);
             if (document == null)
             {
                 return document;
             }
             XmlElement element = document["stylecopresultscache"]["version"];
             if (element.InnerText == "10")
             {
                 item = document.DocumentElement.SelectSingleNode(string.Format(CultureInfo.InvariantCulture, "sourcecode[@name=\"{0}\"][@parser=\"{1}\"]", new object[] { sourceCode.Name, parser.Id }));
                 return document;
             }
             return null;
         }
     }
     catch (XmlException)
     {
         document = null;
     }
     catch (NullReferenceException)
     {
         document = null;
     }
     return document;
 }
Ejemplo n.º 13
0
 public bool SaveDocumentResults(CodeDocument document, SourceParser parser, DateTime settingsTimeStamp)
 {
     bool flag = false;
     lock (this)
     {
         XmlDocument document2 = null;
         try
         {
             if (!this.documentHash.ContainsKey(document.SourceCode.Project.Location))
             {
                 XmlNode node;
                 document2 = this.OpenResultsCache(document.SourceCode, parser, out node);
                 if (document2 != null)
                 {
                     this.documentHash.Add(document.SourceCode.Project.Location, document2);
                 }
             }
             else
             {
                 document2 = this.documentHash[document.SourceCode.Project.Location];
             }
             if (document2 != null)
             {
                 XmlNode oldChild = document2.DocumentElement.SelectSingleNode(string.Format(CultureInfo.InvariantCulture, "sourcecode[@name=\"{0}\"][@parser=\"{1}\"]", new object[] { document.SourceCode.Name, parser.Id }));
                 if (oldChild != null)
                 {
                     document2.DocumentElement.RemoveChild(oldChild);
                 }
             }
             else
             {
                 document2 = new XmlDocument();
                 document2.AppendChild(document2.CreateElement("stylecopresultscache"));
                 XmlNode node3 = document2.CreateElement("version");
                 document2.DocumentElement.AppendChild(node3);
                 node3.InnerText = "10";
                 if (this.documentHash.ContainsKey(document.SourceCode.Project.Location))
                 {
                     this.documentHash.Remove(document.SourceCode.Project.Location);
                 }
                 this.documentHash.Add(document.SourceCode.Project.Location, document2);
             }
             XmlNode newChild = document2.CreateElement("sourcecode");
             XmlAttribute attribute = document2.CreateAttribute("name");
             attribute.Value = document.SourceCode.Name;
             newChild.Attributes.Append(attribute);
             document2.DocumentElement.AppendChild(newChild);
             XmlNode node5 = document2.CreateElement("settings");
             newChild.AppendChild(node5);
             XmlNode node6 = document2.CreateElement("timestamp");
             node5.AppendChild(node6);
             node6.InnerText = settingsTimeStamp.ToString(CultureInfo.InvariantCulture);
             node6 = document2.CreateElement("milliseconds");
             node5.AppendChild(node6);
             node6.InnerText = settingsTimeStamp.Millisecond.ToString(CultureInfo.InvariantCulture);
             DateTime timeStamp = document.SourceCode.TimeStamp;
             node6 = document2.CreateElement("timestamp");
             newChild.AppendChild(node6);
             node6.InnerText = timeStamp.ToString(CultureInfo.InvariantCulture);
             node6 = document2.CreateElement("milliseconds");
             newChild.AppendChild(node6);
             node6.InnerText = timeStamp.Millisecond.ToString(CultureInfo.InvariantCulture);
             if (document.SourceCode.Parser != null)
             {
                 XmlAttribute attribute2 = document2.CreateAttribute("parser");
                 newChild.Attributes.Append(attribute2);
                 attribute2.Value = document.SourceCode.Parser.Id;
             }
             node6 = document2.CreateElement("violations");
             newChild.AppendChild(node6);
             SourceParser.ExportViolations(document, document2, node6);
             return true;
         }
         catch (XmlException)
         {
             return flag;
         }
     }
 }
Ejemplo n.º 14
0
        /// <summary>
        /// Sets the parser that this analyzer is attached to.
        /// </summary>
        /// <param name="item">The parser object that this analyzer is attached to.</param>
        internal void SetParser(SourceParser item)
        {
            Param.Ignore(item);

            // Set the reference to the parser object.
            this.parser = item;
        }
Ejemplo n.º 15
0
 private void RunAnalyzers(CodeDocument document, SourceParser parser, ICollection<SourceAnalyzer> analyzers)
 {
     if (analyzers != null)
     {
         if (parser.SkipAnalysisForDocument(document))
         {
             this.data.Core.SignalOutput(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Skipping {0}...", new object[] { document.SourceCode.Name }));
         }
         else
         {
             foreach (SourceAnalyzer analyzer in analyzers)
             {
                 if (this.data.Core.Cancel)
                 {
                     return;
                 }
                 SourceParser.ClearAnalyzerTags(document);
                 try
                 {
                     analyzer.AnalyzeDocument(document);
                     continue;
                 }
                 catch (Exception)
                 {
                     string output = string.Format(CultureInfo.CurrentCulture, "Exception thrown by analyzer '{0}' while processing '{1}'.", new object[] { analyzer.Name, document.SourceCode.Path });
                     this.data.Core.SignalOutput(MessageImportance.High, output);
                     throw;
                 }
             }
         }
     }
 }
Ejemplo n.º 16
0
 private bool TestAndRunAnalyzers(CodeDocument document, SourceParser parser, ICollection<SourceAnalyzer> analyzers, int passNumber)
 {
     if (analyzers == null)
     {
         return true;
     }
     bool flag = false;
     foreach (SourceAnalyzer analyzer in analyzers)
     {
         if (analyzer.DelayAnalysis(document, passNumber))
         {
             flag = true;
             break;
         }
     }
     if (!flag)
     {
         this.RunAnalyzers(document, parser, analyzers);
     }
     return !flag;
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Initializes a new instance of the SourceCode class.
 /// </summary>
 /// <param name="project">The project that contains this document.</param>
 /// <param name="parser">The parser that created this document.</param>
 /// <param name="configurations">The list of configurations for the document.</param>
 protected SourceCode(CodeProject project, SourceParser parser, IEnumerable<Configuration> configurations)
     : this(project, parser)
 {
     Param.Ignore(project, parser, configurations);
     this.configurations = configurations;
 }
Ejemplo n.º 18
0
        /// <summary>
        /// Invoked when a new parser is loaded.
        /// </summary>
        /// <param name="parser">The new parser.</param>
        public override void AddParser(SourceParser parser)
        {
            Param.RequireNotNull(parser, "parser");

            // Add the parser to the code extensions table.
            ICollection<string> fileTypesForParser = parser.FileTypes;

            if (fileTypesForParser != null)
            {
                // Loop through each of the file types.
                foreach (string fileTypeForParser in fileTypesForParser)
                {
                    if (fileTypeForParser != null)
                    {
                        List<SourceParser> list = null;
                        if (!this.fileTypes.TryGetValue(fileTypeForParser, out list))
                        {
                            list = new List<SourceParser>(1);
                            this.fileTypes.Add(fileTypeForParser, list);
                        }

                        list.Add(parser);
                    }
                }
            }
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Initializes a new instance of the CodeFile class.
        /// </summary>
        /// <param name="path">The path to the code file.</param>
        /// <param name="project">The project that contains this file.</param>
        /// <param name="parser">The parser that created this file object.</param>
        /// <param name="configurations">The list of configurations for the file.</param>
        public CodeFile(string path, CodeProject project, SourceParser parser, IEnumerable<Configuration> configurations)
            : base(project, parser, configurations)
        {
            Param.RequireNotNull(path, "path");
            Param.RequireNotNull(project, "project");
            Param.RequireNotNull(parser, "parser");
            Param.Ignore(configurations);

            this.path = path;

            // If this is not a full path, then we need to add the current directory.
            if (!path.StartsWith(@"\\", StringComparison.Ordinal) && path.Length >= 2 && path[1] != ':')
            {
                // Get the current directory. Remove the trailing slash if it exists.
                string directory = Directory.GetCurrentDirectory();
                if (directory.EndsWith(@"\", StringComparison.Ordinal))
                {
                    directory = directory.Substring(0, directory.Length - 1);
                }

                // Check whether the path starts with a single slash or not.
                if (path.StartsWith(@"\", StringComparison.Ordinal))
                {
                    // Prepend the drive letter.
                    string newPath = directory.Substring(0, 2) + path;
                    path = newPath;
                }
                else
                {
                    // Prepend the current directory.
                    string newPath = directory + @"\" + path;
                    path = newPath;
                }
            }

            // Strip out the name of the file.
            int index = path.LastIndexOf(@"\", StringComparison.Ordinal);
            if (-1 == index)
            {
                this.name = this.path;
            }
            else
            {
                this.name = path.Substring(index + 1, path.Length - index - 1);
                this.folder = path.Substring(0, index);

                if (this.folder != null)
                {
                    // Trim the path and convert it to lowercase characters
                    // so that we can do string matches and find other files and
                    // projects under the same path.
                    this.folder = StyleCopCore.CleanPath(this.folder);
                }
            }

            // Strip out the file extension.
            index = this.name.LastIndexOf(".", StringComparison.Ordinal);
            if (-1 == index)
            {
                this.fileType = string.Empty;
            }
            else
            {
                this.fileType = this.name.Substring(index + 1, this.name.Length - index - 1).ToUpperInvariant();
            }
        }