Exemplo n.º 1
0
        private IEnumerable <DiffContent> ChildsChanges(RealNode node)
        {
            var childs = Zip(node.Childs, Childs)
                         .Select(x => x.Result.CompareWith(x.Source))
                         .Where(x => x.IsChanged);

            return(childs);
        }
Exemplo n.º 2
0
        private IEnumerable <DiffContent> ValueChanges(RealNode node)
        {
            var pair = Pair.Create(node.Value, node.DefaultAction, Value, DefaultAction);

            return(pair.Apply((source, res, action) =>
            {
                return source != res && !string.IsNullOrEmpty(source)
                                                ? new DiffValue[] { new DiffValue(action, source) }
                                                : new DiffValue[0];
            }));
        }
Exemplo n.º 3
0
        private IEnumerable <DiffContent> AttributeChanges(RealNode node)
        {
            var pair = Pair.Create(node.Attributes, node.DefaultAction, Attributes, DefaultAction);

            return(pair.Apply((source, res, action) =>
            {
                return source
                .Where(x => !res.ContainsKey(x.Key) || res[x.Key].ToString() != x.Value.ToString())
                .Select(x => new DiffAttribute(action, x.Value));
            }));
        }
Exemplo n.º 4
0
        public DiffNode CompareWith(RealNode node)
        {
            //swap nodes to be sure that raw element with default action = Added comes first
            //since we are looking for a comparison against a current version
            if (DefaultAction == DiffAction.Removed)
            {
                return(node.CompareWith(this));
            }

            var contentChanges = AttributeChanges(node)
                                 .Concat(ValueChanges(node))
                                 .Concat(ChildsChanges(node));

            return(new DiffNode(Raw, contentChanges));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Compare <paramref name="resultElement"/> with a <paramref name="sourceElement"/>
        /// Comparison is made only for XElement, XAttribute or XText elements
        /// All other xml elements such as XComment, CDATA etc would be ignored
        /// </summary>
        /// <param name="sourceElement">Source element</param>
        /// <param name="resultElement">Result element</param>
        /// <returns>
        /// Return value is build upon <paramref name="resultElement"/>
        /// In case of different root tags InvalidOperationException is throws
        /// </returns>
        public DiffNode Compare(XElement sourceElement, XElement resultElement)
        {
            if (sourceElement == null)
            {
                throw new ArgumentNullException(nameof(sourceElement));
            }
            if (resultElement == null)
            {
                throw new ArgumentNullException(nameof(resultElement));
            }
            if (sourceElement.Name != resultElement.Name)
            {
                throw new InvalidOperationException("Root elements are different");
            }

            RealNode parsedSource = Parse(sourceElement, DiffAction.Removed);
            RealNode parsedResult = Parse(resultElement, DiffAction.Added);

            return(parsedResult.CompareWith(parsedSource));
        }
Exemplo n.º 6
0
 public DiffNode CompareWith(RealNode node)
 {
     return(new DiffNode(node.DefaultAction, node.Raw));
 }