Exemple #1
0
        private void _TraverseProperty(P.Property prop, Runner action)
        {
            if (prop == null)
            {
                return;
            }

            if (_callback != null)
            {
                _callback.Update(++_count, null, prop.ToString());
            }

            action(prop);

            Dictionary <string, object> childs = prop.GetChilds();

            foreach (string name in childs.Keys)
            {
                object sub = childs[name];
                if (sub is P.Property)
                {
                    _TraverseProperty(sub as P.Property, action);
                }
                else
                {
                    _TraverseObject(sub, action);
                }
            }
        }
        internal D.DifferenceNode _Compare(P.Property left, P.Property right)
        {
            D.DifferenceNode node = new D.DifferenceNode(left.ToString(), left, right);
            D.DifferenceNode sub;

            if ((left == null || right == null) && (left != right))
            {
                return(node);
            }

            // Do some by-name blacklisting to skips things like delta timestamps
            if (left is P.ValueProperty)
            {
                P.ValueProperty val_prop = left as P.ValueProperty;
                if (val_prop.Name != null)
                {
                    string name = val_prop.Name.ToString();
                    if (_blacklisted.Contains(name))
                    {
                        return(null);
                    }
                }
            }

            Dictionary <string, object> left_childs  = new Dictionary <string, object>(left.GetChilds());
            Dictionary <string, object> right_childs = new Dictionary <string, object>(right.GetChilds());

            while (left_childs.Count > 0)
            {
                var pair = left_childs.First();

                object left_sub = pair.Value;
                left_childs.Remove(pair.Key);

                object right_sub = null;
                if (right_childs.ContainsKey(pair.Key))
                {
                    right_sub = right_childs[pair.Key];
                    right_childs.Remove(pair.Key);
                }

                if (_blacklisted.Contains(pair.Key))
                {
                    continue;
                }

                if (right_sub == null && left_sub != null)
                {
                    sub = new D.DifferenceNode(null, left_sub, null);
                }
                else if (left_sub is P.Property)
                {
                    sub = _Compare(left_sub as P.Property, right_sub as P.Property);
                }
                else
                {
                    sub = _CompareObject(left_sub, right_sub);
                }
                if (sub != null)
                {
                    sub.Title = pair.Key;
                    node.Add(sub);
                }
            }

            foreach (var pair in right_childs)
            {
                if (!_blacklisted.Contains(pair.Key))
                {
                    node.Add(pair.Key, null, pair.Value);
                }
            }

            return((node.ChildCount > 0) ? node : null);
        }
        internal bool _CompareAll()
        {
            int total = _left_save.TotalElements + _right_save.TotalElements;

            Log.Info("Comparing a ~{0} elements ...", total);
            _cbStart(total, Translate._("Action.Compare.Progress"), "");

            D.DifferenceNode        node;
            D.DifferenceNode        sub;
            List <D.DifferenceNode> nodes = new List <D.DifferenceNode>();
            int differences = 0;

            Func <P.Properties, P.Properties> clone = (src) => {
                P.Properties dst = new P.Properties();
                foreach (P.Property prop in src)
                {
                    dst.Add(prop);
                }
                return(dst);
            };


            node = _Compare(_left_save.Header, _right_save.Header);
            if (node != null)
            {
                node.Title = "Header";
                _differences.Add(node);
                ++differences;
            }


            P.Properties left_objects  = clone(_left_save.Objects);
            P.Properties right_objects = clone(_right_save.Objects);
            while (left_objects.Count > 0)
            {
                P.Property left = left_objects[0];
                left_objects.RemoveAt(0);

                _cbUpdate(null, left.ToString());

                P.Property right = _FindMatchByPath(left, right_objects);
                if (right == null)
                {
                    sub = new D.DifferenceNode(left.ToString(), left, null);
                }
                else
                {
                    right_objects.Remove(right);
                    sub = _Compare(left, right);
                }
                if (sub != null)
                {
                    sub.Title = left.ToString();
                    nodes.Add(sub);
                }
            }
            foreach (P.Property right in right_objects)
            {
                nodes.Add(new D.DifferenceNode(right.ToString(), null, right));
            }
            if (nodes.Count > 0)
            {
                differences += nodes.Count;
                foreach (D.DifferenceNode child in nodes)
                {
                    AddDifference(child);
                }
                nodes.Clear();
            }


            P.Properties left_coll  = clone(_left_save.Collected);
            P.Properties right_coll = clone(_right_save.Collected);
            while (left_coll.Count > 0)
            {
                P.Property left = left_coll[0];
                left_coll.RemoveAt(0);

                _cbUpdate(null, left.ToString());

                P.Property right = _FindMatchByPath(left, right_coll);
                if (right == null)
                {
                    sub = new D.DifferenceNode(left.ToString(), left, null);
                }
                else
                {
                    right_coll.Remove(right);
                    sub = _Compare(left, right);
                }
                if (sub != null)
                {
                    sub.Title = left.ToString();
                    nodes.Add(sub);
                }
            }
            foreach (P.Property right in right_coll)
            {
                nodes.Add(new D.DifferenceNode(right.ToString(), null, right));
            }
            if (nodes.Count > 0)
            {
                differences += nodes.Count;
                foreach (D.DifferenceNode child in nodes)
                {
                    AddDifference(child);
                }
            }


            //if (_left_save.Missing != null && _left_save.Missing.Length > 0)
            //{
            //	//...
            //}


            _cbStop(Translate._("Action.Done"), "");
            Log.Info("... done comparing, differences={0}", differences);

            return(differences == 0);
        }