Ejemplo n.º 1
0
        static int Main(string[] args)
        {
            var version = Assembly.GetEntryAssembly().GetName().Version;

            Console.WriteLine(
                "==================\n"
                + string.Format(" FatAntelope v{0}.{1}\n", version.Major, version.Minor)
                + "==================\n");

            if (args == null || args.Length < 3 || args.Length > 4)
            {
                Console.WriteLine(
                    "Error: Unexpected number of paramters.\n"
                    + "Usage: FatAntelope source-file target-file output-file [transformed-file]\n"
                    + "  source-file : (Input) The original config file\n"
                    + "  target-file : (Input) The final config file\n"
                    + "  output-file : (Output) The output config transform patch file\n"
                    + "  transformed-file : (Optional Output) The config file resulting from applying the output-file to the source-file\n"
                    + "                     This file should be semantically equal to the target-file.\n");
                return((int)ExitCode.InvalidParameters);
            }

            Console.WriteLine("- Building xml trees . . .\n");
            var tree1 = BuildTree(args[0]);
            var tree2 = BuildTree(args[1]);

            Console.WriteLine("- Comparing xml trees . . .\n");
            XDiff.Diff(tree1, tree2);
            if (tree1.Root.Match == MatchType.Match && tree2.Root.Match == MatchType.Match && tree1.Root.Matching == tree2.Root)
            {
                Console.WriteLine("Warning: No difference found!\n");
                return((int)ExitCode.NoDifference);
            }

            if (tree1.Root.Match == MatchType.NoMatch || tree2.Root.Match == MatchType.NoMatch)
            {
                Console.Error.WriteLine("Error: Root nodes must have the same name!\n");
                return((int)ExitCode.RootNodeMismatch);
            }

            Console.WriteLine("- Writing XDT transform . . .\n");
            var writer = new XdtDiffWriter();
            var patch  = writer.GetDiff(tree2);

            patch.Save(new FileStream(args[2], FileMode.Create));

            /*if (args.Length > 3)
             * {
             *  Console.WriteLine("- Applying transform to source . . .\n");
             *  var source = new XmlTransformation();
             *  source.Load(args[0]);
             *
             *  var transform = new XmlTransformation(patch.OuterXml, false, null);
             *  transform.Apply(source);
             *
             *  source.Save(args[3]);
             * }
             * Console.WriteLine("- Finished successfully!\n");*/
            return((int)ExitCode.Success);
        }
Ejemplo n.º 2
0
        private void AssertCanTransform(string sourceXml, string targetXml)
        {
            var doc1 = new XmlDocument();

            doc1.LoadXml(sourceXml);

            // reordered xml but same values
            var doc2 = new XmlDocument();

            doc2.LoadXml(targetXml);

            var tree1 = new XTree(doc1);
            var tree2 = new XTree(doc2);

            XDiff.Diff(tree1, tree2);

            var writer      = new XdtDiffWriter();
            var patch       = writer.GetDiff(tree2);
            var transformed = Transform(doc1, patch);

            var transformedTree = new XTree(transformed);

            Assert.IsFalse(Enumerable.SequenceEqual(tree1.Root.Hash, tree2.Root.Hash));
            Assert.IsTrue(Enumerable.SequenceEqual(transformedTree.Root.Hash, tree2.Root.Hash));
        }
Ejemplo n.º 3
0
        private void diffToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                var source = new XmlDocument();
                source.LoadXml(fastColoredTextBoxSource.Text);
                var target = new XmlDocument();
                target.LoadXml(fastColoredTextBoxTarget.Text);

                var tree1 = new XTree(source);
                var tree2 = new XTree(target);


                XDiff.Diff(tree1, tree2);
                if (tree1.Root.Match == MatchType.Match && tree2.Root.Match == MatchType.Match && tree1.Root.Matching == tree2.Root)
                {
                    Message("Warning: No difference found!\n");
                }

                if (tree1.Root.Match == MatchType.NoMatch || tree2.Root.Match == MatchType.NoMatch)
                {
                    Message("Error: Root nodes must have the same name!\n");
                }

                var writer = new XdtDiffWriter();
                var patch  = writer.GetDiff(tree2);
                fastColoredTextBoxXDT.Text = PrettyXml(patch.OuterXml);
                tabControl1.SelectedIndex  = 2;
            }
            catch (Exception ex)
            {
                Message(ex.ToString());
            }
        }
Ejemplo n.º 4
0
        public void AttributeMarkedChanged()
        {
            var doc1 = new XmlDocument();

            doc1.LoadXml(@"
                <root>
                    <child1 name1='child1' type1='elem1'>child1</child1>
                    <child2 name1='child2' type1='elem2'>child2</child2>
                </root>"
                         );

            // reordered xml but same values
            var doc2 = new XmlDocument();

            doc2.LoadXml(@"
                <root>
                    <child1 type1='elem1' name1='DIFFERENT'>child1</child1>
                    <child2 name1='child2' type1='elem2'>child2</child2>
                </root>"
                         );

            var tree1 = new XTree(doc1);
            var tree2 = new XTree(doc2);

            XDiff.Diff(tree1, tree2);

            Assert.AreEqual(tree1.Root.Match, MatchType.Change);
            Assert.AreEqual(tree2.Root.Match, MatchType.Change);

            Assert.AreEqual(tree1.Root.Children.Length, 2);
            Assert.AreEqual(tree2.Root.Children.Length, 2);

            Assert.AreEqual(tree1.Root.Elements.Length, 2);
            Assert.AreEqual(tree2.Root.Elements.Length, 2);

            Assert.AreEqual(tree1.Root.Elements[0].Match, MatchType.Change);
            Assert.AreEqual(tree2.Root.Elements[0].Match, MatchType.Change);

            Assert.AreEqual(tree1.Root.Elements[1].Match, MatchType.Match);
            Assert.AreEqual(tree2.Root.Elements[1].Match, MatchType.Match);

            Assert.AreEqual(tree1.Root.Elements[0].Attributes.Length, 2);
            Assert.AreEqual(tree2.Root.Elements[0].Attributes.Length, 2);

            Assert.AreEqual(tree1.Root.Elements[0].Texts.Length, 1);
            Assert.AreEqual(tree2.Root.Elements[0].Texts.Length, 1);

            Assert.AreEqual(tree1.Root.Elements[0].Texts[0].Match, MatchType.Match);
            Assert.AreEqual(tree2.Root.Elements[0].Texts[0].Match, MatchType.Match);

            Assert.AreEqual(tree1.Root.Elements[0].Attributes[0].Match, MatchType.Change);
            Assert.AreEqual(tree2.Root.Elements[0].Attributes[0].Match, MatchType.Match);

            Assert.AreEqual(tree1.Root.Elements[0].Attributes[1].Match, MatchType.Match);
            Assert.AreEqual(tree2.Root.Elements[0].Attributes[1].Match, MatchType.Change);

            Assert.AreEqual(tree1.Root.Elements[0].Attributes[0].Matching, tree2.Root.Elements[0].Attributes[1]);
        }
Ejemplo n.º 5
0
        public static string GenerateXdt(string newConfig)
        {
            var baseConfig = GetBaseConfig();

            if (string.IsNullOrEmpty(baseConfig) || string.IsNullOrEmpty(newConfig))
            {
                return("");
            }

            var baseTree = BuildTree(baseConfig);
            var newTree  = BuildTree(newConfig);

            XDiff.Diff(baseTree, newTree);

            var patch = new XdtDiffWriter().GetDiff(newTree);

            return(patch.ToFormattedString());
        }
Ejemplo n.º 6
0
        private static List <String> CompileXmlChanges(int fileVersion, string origPath, string newPath)
        {
            XmlDocument origDoc = new XmlDocument();
            FileStream  origFs  = new FileStream(origPath, FileMode.Open);

            origDoc.Load(origFs);
            origFs.Dispose();

            XmlDocument newDoc = new XmlDocument();
            FileStream  newFs  = new FileStream(newPath, FileMode.Open);

            newDoc.Load(newFs);
            newFs.Dispose();

            var tree1 = new XTree(origDoc);
            var tree2 = new XTree(newDoc);

            XDiff.Diff(tree1, tree2);

            var res = new List <String>();

            List <string> missingFromNew = CompileXmlChangesForNode(Enumerable.Empty <string>().ToImmutableHashSet(), "", "", tree1.Root);

            if (missingFromNew.Any())
            {
                res.Add("Missing nodes:");
                res.AddRange(missingFromNew);
            }

            ImmutableHashSet <string> canIgnore =
                AddedFields.Where(kv => kv.Key >= fileVersion).SelectMany(kv => kv.Value).ToImmutableHashSet();

            List <string> extra = CompileXmlChangesForNode(canIgnore, "", "", tree2.Root);

            if (extra.Any())
            {
                res.Add("Additional nodes:");
                res.AddRange(extra);
            }

            return(res);
        }
Ejemplo n.º 7
0
        private XmlDocument GetPatch(string sourceXml, string targetXml)
        {
            var doc1 = new XmlDocument();

            doc1.LoadXml(sourceXml);

            // reordered xml but same values
            var doc2 = new XmlDocument();

            doc2.LoadXml(targetXml);

            var tree1 = new XTree(doc1);
            var tree2 = new XTree(doc2);

            XDiff.Diff(tree1, tree2);

            var writer = new XdtDiffWriter();

            return(writer.GetDiff(tree2));
        }