aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core/Link/WixComplexReferenceSymbolExtensions.cs
blob: 1702d3ca0c923c105c549fed55336b09a0ca7f2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

namespace WixToolset.Core.Link
{
    using System;
    using WixToolset.Data.Symbols;

    internal static class WixComplexReferenceSymbolExtensions
    {
        /// <summary>
        /// Creates a shallow copy of the ComplexReference.
        /// </summary>
        /// <returns>A shallow copy of the ComplexReference.</returns>
        public static WixComplexReferenceSymbol Clone(this WixComplexReferenceSymbol source)
        {
            var clone = new WixComplexReferenceSymbol(source.SourceLineNumbers, source.Id);
            clone.ParentType = source.ParentType;
            clone.Parent = source.Parent;
            clone.ParentLanguage = source.ParentLanguage;
            clone.ChildType = source.ChildType;
            clone.Child = source.Child;
            clone.IsPrimary = source.IsPrimary;

            return clone;
        }

        /// <summary>
        /// Compares two complex references without considering the primary bit.
        /// </summary>
        /// <param name="obj">Complex reference to compare to.</param>
        /// <returns>Zero if the objects are equivalent, negative number if the provided object is less, positive if greater.</returns>
        public static int CompareToWithoutConsideringPrimary(this WixComplexReferenceSymbol symbol, WixComplexReferenceSymbol other)
        {
            var comparison = symbol.ChildType - other.ChildType;
            if (0 == comparison)
            {
                comparison = String.Compare(symbol.Child, other.Child, StringComparison.Ordinal);
                if (0 == comparison)
                {
                    comparison = symbol.ParentType - other.ParentType;
                    if (0 == comparison)
                    {
                        string thisParentLanguage = null == symbol.ParentLanguage ? String.Empty : symbol.ParentLanguage;
                        string otherParentLanguage = null == other.ParentLanguage ? String.Empty : other.ParentLanguage;
                        comparison = String.Compare(thisParentLanguage, otherParentLanguage, StringComparison.Ordinal);
                        if (0 == comparison)
                        {
                            comparison = String.Compare(symbol.Parent, other.Parent, StringComparison.Ordinal);
                        }
                    }
                }
            }

            return comparison;
        }

        /// <summary>
        /// Changes all of the parent references to point to the passed in parent reference.
        /// </summary>
        /// <param name="parent">New parent complex reference.</param>
        public static void Reparent(this WixComplexReferenceSymbol symbol, WixComplexReferenceSymbol parent)
        {
            symbol.Parent = parent.Parent;
            symbol.ParentLanguage = parent.ParentLanguage;
            symbol.ParentType = parent.ParentType;

            if (!symbol.IsPrimary)
            {
                symbol.IsPrimary = parent.IsPrimary;
            }
        }
    }
}