コード例 #1
0
        private FileInfo DetectMovesFromFile(FileInfo fileInfo)
        {
            AppendResult(fileInfo.DirectoryName, fileInfo.Name,
                         "=========================================" + Environment.NewLine +
                         "Detect Moves Start: " + DateTime.Now.ToString());

            LogProvider provider = new LogProvider();

            provider.OpenLog(fileInfo.FullName);

            MoveDetector moveDetector = MoveDetector.GetInstance();
            var          patterns     = moveDetector.DetectAsPatternInstances(provider);

            // Save the results to a file.
            DetectionResult result = new DetectionResult(provider.LogPath, patterns);

            result.SaveToFile(GetSaveFileName(fileInfo.DirectoryName, fileInfo.Name));
            result.ExportToCSV(GetSaveFileName(fileInfo.DirectoryName, fileInfo.Name, "csv"));

            var xmlDoc = new XmlDocument();

            xmlDoc.Load(fileInfo.FullName);

            var documentChanges = provider.LoggedEvents.OfType <DocumentChange>().ToList();

            foreach (MovePatternInstance pattern in patterns)
            {
                int startIndex = documentChanges.IndexOf(pattern.PrimaryEvent as DocumentChange);

                Delete delete = documentChanges[startIndex + 0] as Delete;
                Insert insert = documentChanges[startIndex + 1] as Insert;

                // Same file, same place
                if (pattern.FromFile == pattern.ToFile && delete.Offset == insert.Offset)
                {
                    // Just cancel them out.
                    xmlDoc.DocumentElement.RemoveChild(Event.FindCorrespondingXmlElementFromXmlDocument(xmlDoc, delete));
                    xmlDoc.DocumentElement.RemoveChild(Event.FindCorrespondingXmlElementFromXmlDocument(xmlDoc, insert));
                }
                else
                {
                    // Transform Insert -> Move and then remove Delete.
                    SetMoveElement(xmlDoc, pattern, delete, insert);
                    xmlDoc.DocumentElement.RemoveChild(Event.FindCorrespondingXmlElementFromXmlDocument(xmlDoc, delete));
                }
            }

            string newPath = Path.Combine(fileInfo.DirectoryName,
                                          _settings.Prefix + Path.GetFileNameWithoutExtension(fileInfo.Name) + _settings.Postfix + fileInfo.Extension);

            xmlDoc.Save(newPath);

            AppendResult(fileInfo.DirectoryName, fileInfo.Name,
                         string.Format("{0} moves have been detected and written in the log" + Environment.NewLine, patterns.Count()));

            return(new FileInfo(newPath));
        }
コード例 #2
0
        private string DetectMovesFromFile(FileInfo fileInfo)
        {
            LogProvider provider = new LogProvider();

            provider.OpenLog(fileInfo.FullName);

            MoveDetector moveDetector = MoveDetector.GetInstance();
            var          patterns     = moveDetector.DetectAsPatternInstances(provider);

            var xmlDoc = new XmlDocument();

            xmlDoc.Load(fileInfo.FullName);

            var documentChanges = provider.LoggedEvents.OfType <DocumentChange>().ToList();

            foreach (MovePatternInstance pattern in patterns)
            {
                int startIndex = documentChanges.IndexOf(pattern.PrimaryEvent as DocumentChange);

                Delete delete = documentChanges[startIndex + 0] as Delete;
                Insert insert = documentChanges[startIndex + 1] as Insert;

                // Same file, same place
                if (pattern.FromFile == pattern.ToFile && delete.Offset == insert.Offset)
                {
                    // Just cancel them out.
                    xmlDoc.DocumentElement.RemoveChild(Event.FindCorrespondingXmlElementFromXmlDocument(xmlDoc, delete));
                    xmlDoc.DocumentElement.RemoveChild(Event.FindCorrespondingXmlElementFromXmlDocument(xmlDoc, insert));
                }
                else
                {
                    // Transform Insert -> Move and then remove Delete.
                    SetMoveElement(xmlDoc, pattern, delete, insert);
                    xmlDoc.DocumentElement.RemoveChild(Event.FindCorrespondingXmlElementFromXmlDocument(xmlDoc, delete));
                }
            }

            string newPath = Path.Combine(fileInfo.DirectoryName,
                                          Path.GetFileNameWithoutExtension(fileInfo.Name) + textPostfix.Text + fileInfo.Extension);

            xmlDoc.Save(newPath);

            return(string.Format("[{0}] {1} moves have been detected and written in the log", fileInfo.FullName, patterns.Count()));
        }