Example #1
0
        /// <summary>
        /// Saves the given code document results into a cache document.
        /// </summary>
        /// <param name="document">
        /// The document to save.
        /// </param>
        /// <param name="parser">
        /// The parser that created the document.
        /// </param>
        /// <param name="settingsTimeStamp">
        /// The time when the settings were last updated.
        /// </param>
        /// <returns>
        /// Returns true if the document was saved.
        /// </returns>
        public bool SaveDocumentResults(CodeDocument document, SourceParser parser, DateTime settingsTimeStamp)
        {
            Param.AssertNotNull(document, "document");
            Param.AssertNotNull(parser, "parser");
            Param.Ignore(settingsTimeStamp);

            bool success = false;

            lock (this)
            {
                try
                {
                    XmlDocument xml;
                    if (!this.documentHash.ContainsKey(document.SourceCode.Project.Location))
                    {
                        XmlNode temp;
                        xml = this.OpenResultsCache(document.SourceCode, parser, out temp);
                        if (xml != null)
                        {
                            this.documentHash.Add(document.SourceCode.Project.Location, xml);
                        }
                    }
                    else
                    {
                        xml = this.documentHash[document.SourceCode.Project.Location];
                    }

                    if (xml != null)
                    {
                        XmlNode remove =
                            xml.DocumentElement.SelectSingleNode(
                                string.Format(CultureInfo.InvariantCulture, "sourcecode[@name=\"{0}\"][@parser=\"{1}\"]", document.SourceCode.Name, parser.Id));
                        if (remove != null)
                        {
                            xml.DocumentElement.RemoveChild(remove);
                        }
                    }
                    else
                    {
                        xml = new XmlDocument();

                        // Create the document node.
                        xml.AppendChild(xml.CreateElement("stylecopresultscache"));

                        // Add the version.
                        XmlNode versionNode = xml.CreateElement("version");
                        xml.DocumentElement.AppendChild(versionNode);
                        versionNode.InnerText = ResultsCache.Version;

                        if (this.documentHash.ContainsKey(document.SourceCode.Project.Location))
                        {
                            this.documentHash.Remove(document.SourceCode.Project.Location);
                        }

                        this.documentHash.Add(document.SourceCode.Project.Location, xml);
                    }

                    XmlNode      root = xml.CreateElement("sourcecode");
                    XmlAttribute name = xml.CreateAttribute("name");
                    name.Value = document.SourceCode.Name;
                    root.Attributes.Append(name);
                    xml.DocumentElement.AppendChild(root);

                    // Create the timestamps node.
                    // We need to store the timestamp of all files that were used to create the violation.
                    // Parser, Rules, settings, source file, spell checker, and dictionaries.
                    XmlElement node = xml.CreateElement("timestamps");
                    root.AppendChild(node);

                    this.AddTimestampToXml(xml, node, "styleCop", this.core.TimeStamp);

                    this.AddTimestampToXml(xml, node, "settingsFile", settingsTimeStamp);

                    // Stores the last write time of the source code.
                    this.AddTimestampToXml(xml, node, "sourceFile", document.SourceCode.TimeStamp);

                    // Store all the rules and parser timestamps
                    this.AddTimestampToXml(xml, node, "parser", document.SourceCode.Parser.TimeStamp);

                    foreach (SourceAnalyzer analyzer in document.SourceCode.Parser.Analyzers)
                    {
                        this.AddTimestampToXml(xml, node, analyzer.Id, analyzer.TimeStamp);
                        this.AddHashCodeToXml(xml, node, analyzer.Id + ".FilesHashCode", analyzer.GetDependantFilesHashCode(document.SourceCode.Project.Culture));
                    }

                    // Add the parser ID attribute.
                    if (document.SourceCode.Parser != null)
                    {
                        XmlAttribute attribute = xml.CreateAttribute("parser");
                        root.Attributes.Append(attribute);
                        attribute.Value = document.SourceCode.Parser.Id;
                    }

                    // Create the violations node.
                    node = xml.CreateElement("violations");
                    root.AppendChild(node);

                    // Add the violations.
                    SourceParser.ExportViolations(document, xml, node);

                    success = true;
                }
                catch (XmlException)
                {
                }
            }

            return(success);
        }
Example #2
0
        /// <summary>
        /// Saves the given code document results into a cache document.
        /// </summary>
        /// <param name="document">The document to save.</param>
        /// <param name="parser">The parser that created the document.</param>
        /// <param name="settingsTimeStamp">The time when the settings were last updated.</param>
        /// <returns>Returns true if the document was saved.</returns>
        public bool SaveDocumentResults(ICodeDocument document, SourceParser parser, DateTime settingsTimeStamp)
        {
            Param.AssertNotNull(document, "document");
            Param.AssertNotNull(parser, "parser");
            Param.Ignore(settingsTimeStamp);

            bool success = false;

            lock (this)
            {
                XmlDocument xml = null;

                try
                {
                    if (!this.documentHash.ContainsKey(document.SourceCode.Project.Location))
                    {
                        XmlNode temp;
                        xml = this.OpenResultsCache(document.SourceCode, parser, out temp);
                        if (xml != null)
                        {
                            this.documentHash.Add(document.SourceCode.Project.Location, xml);
                        }
                    }
                    else
                    {
                        xml = this.documentHash[document.SourceCode.Project.Location];
                    }

                    if (xml != null)
                    {
                        XmlNode remove = xml.DocumentElement.SelectSingleNode(
                            string.Format(CultureInfo.InvariantCulture, "sourcecode[@name=\"{0}\"][@parser=\"{1}\"]", document.SourceCode.Name, parser.Id));
                        if (remove != null)
                        {
                            xml.DocumentElement.RemoveChild(remove);
                        }
                    }
                    else
                    {
                        xml = new XmlDocument();

                        // Create the document node.
                        xml.AppendChild(xml.CreateElement("stylecopresultscache"));

                        // Add the version.
                        XmlNode versionNode = xml.CreateElement("version");
                        xml.DocumentElement.AppendChild(versionNode);
                        versionNode.InnerText = ResultsCache.Version;

                        if (this.documentHash.ContainsKey(document.SourceCode.Project.Location))
                        {
                            this.documentHash.Remove(document.SourceCode.Project.Location);
                        }

                        this.documentHash.Add(document.SourceCode.Project.Location, xml);
                    }

                    XmlNode      root = xml.CreateElement("sourcecode");
                    XmlAttribute name = xml.CreateAttribute("name");
                    name.Value = document.SourceCode.Name;
                    root.Attributes.Append(name);
                    xml.DocumentElement.AppendChild(root);

                    // Save the last write time of the settings.
                    XmlNode settingsNode = xml.CreateElement("settings");
                    root.AppendChild(settingsNode);

                    XmlNode node = xml.CreateElement("timestamp");
                    settingsNode.AppendChild(node);
                    node.InnerText = settingsTimeStamp.ToString(CultureInfo.InvariantCulture);

                    node = xml.CreateElement("milliseconds");
                    settingsNode.AppendChild(node);
                    node.InnerText = settingsTimeStamp.Millisecond.ToString(CultureInfo.InvariantCulture);

                    // Get the last write time of the source code.
                    DateTime writeTime = document.SourceCode.TimeStamp;

                    // Add the timestamp.
                    node = xml.CreateElement("timestamp");
                    root.AppendChild(node);
                    node.InnerText = writeTime.ToString(CultureInfo.InvariantCulture);

                    node = xml.CreateElement("milliseconds");
                    root.AppendChild(node);
                    node.InnerText = writeTime.Millisecond.ToString(CultureInfo.InvariantCulture);

                    // Add the parser ID attribute.
                    if (document.SourceCode.Parser != null)
                    {
                        XmlAttribute attribute = xml.CreateAttribute("parser");
                        root.Attributes.Append(attribute);
                        attribute.Value = document.SourceCode.Parser.Id;
                    }

                    // Create the violations node.
                    node = xml.CreateElement("violations");
                    root.AppendChild(node);

                    // Add the violations.
                    SourceParser.ExportViolations(document, xml, node);

                    success = true;
                }
                catch (XmlException)
                {
                }
            }

            return(success);
        }