chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//            Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
using System.Collections.Generic;
using System.Linq;
 
namespace OpenTap
{
    internal abstract class LicenseBase
    {
        internal abstract bool Matches(HashSet<string> licenses);
        internal abstract IEnumerable<LicenseBase> FindMatch(HashSet<string> licenses);
 
        internal static string FormatFriendly(string licenseString, bool humanReadable = true)
        {
            return PrintLicense(Prune(LicenseParser.ParseString(licenseString)), humanReadable);
        }
 
        private static LicenseBase Prune(LicenseBase real)
        {
            if (real is LicenseAll)
            {
                var all = (real as LicenseAll).Licenses.Select(Prune).ToArray();
 
                if (all.Any(l => l == null))
                    return null;
 
                all = all.Where(l => !(l is LicenseProcess)).ToArray();
 
                if (all.Length == 1)
                    return all.First();
                else if (all.Length == 0)
                    return null;
                else
                    return new LicenseAll(all);
            }
            else if (real is LicenseAny)
            {
                var all = (real as LicenseAny).Licenses.Select(Prune).Where(x => x != null).ToArray();
 
                if (all.OfType<LicenseProcess>().Any())
                    return all.OfType<LicenseProcess>().First();
 
                if (all.Length == 1)
                    return all.First();
                else if (all.Length == 0)
                    return null;
                else
                    return new LicenseAny(all);
            }
            else if (real is LicenseProcess)
            {
                if ((real as LicenseProcess).ProcessName.ToLowerInvariant().Contains("keysight.opentap"))
                    return real;
                else
                    return null;
            }
 
            return real;
        }
 
        private static string PrintLicense(LicenseBase license, bool humanReadable)
        {
            if (license is LicenseAll)
            {
                List<string> outputs = (license as LicenseAll).Licenses.Select(l =>
                    {
                        string output = PrintLicense(l, humanReadable);
                        if (l is LicenseAny && !string.IsNullOrEmpty(output))
                            output = "(" + output + ")";
                        return output;
                    }
                ).ToList();
 
                // If two licenses are required (&) and one of them is "secret" (not printed), then the other one should not be printed either.
                // For example: KS8000A&{BenchVue} should yield no output, as {BenchVue} is hidden and KS8000A will not work on its own.
                if (outputs.Any(s => string.IsNullOrEmpty(s))) 
                    return "";
                else
                    return string.Join(humanReadable ? " and " : "&", outputs);
            }
            else if (license is LicenseAny)
            {
                return string.Join(humanReadable ? " or " : "|", (license as LicenseAny).Licenses.Select(l =>
                    {
                        string output = PrintLicense(l, humanReadable);
                        if (l is LicenseAll && !string.IsNullOrEmpty(output))
                            output = "(" + output + ")";
                        return output;
                    }
                ).Where(x => x != ""));
            }
            else if (license is LicenseRequired)
            {
                string featureSeed = (license as LicenseRequired).FeatureSeed;
 
                if (humanReadable && featureSeed.Contains("-INT")) // hide -INT licenses only when printing to the user, when printing to an xml file we need to keep the int license
                    return "";
                else
                    return featureSeed;
            }
 
            return "";
        }
    }
 
    internal class LicenseRequired : LicenseBase
    {
        internal readonly string FeatureSeed;
 
        internal override bool Matches(HashSet<string> licenses)
        {
            return licenses.Contains(FeatureSeed);
        }
 
        internal override IEnumerable<LicenseBase> FindMatch(HashSet<string> licenses)
        {
            if (Matches(licenses))
                yield return this;
        }
 
        public LicenseRequired(string text)
        {
            FeatureSeed = text;
        }
    }
 
    internal class LicenseProcess : LicenseBase
    {
        internal readonly string ProcessName;
 
        internal override bool Matches(HashSet<string> licenses)
        {
            return ProcessName.ToLowerInvariant().Contains("keysight.opentap");
        }
 
        internal override IEnumerable<LicenseBase> FindMatch(HashSet<string> licenses)
        {
            if (Matches(licenses))
                yield return this;
        }
 
        public LicenseProcess(string processName)
        {
            ProcessName = processName;
        }
    }
 
    internal class LicenseAny : LicenseBase
    {
        internal readonly LicenseBase[] Licenses;
 
        internal override bool Matches(HashSet<string> licenses)
        {
            return Licenses.Any(l => l.Matches(licenses));
        }
 
        internal override IEnumerable<LicenseBase> FindMatch(HashSet<string> licenses)
        {
            var l = Licenses.FirstOrDefault(lic => lic.Matches(licenses));
 
            if (l != null)
                return l.FindMatch(licenses);
            else
                return Enumerable.Empty<LicenseBase>();
        }
 
        public LicenseAny(LicenseBase[] licenseBase)
        {
            Licenses = licenseBase;
        }
    }
 
    internal class LicenseAll : LicenseBase
    {
        internal readonly LicenseBase[] Licenses;
 
        internal override bool Matches(HashSet<string> licenses)
        {
            return Licenses.All(l => l.Matches(licenses));
        }
 
        internal override IEnumerable<LicenseBase> FindMatch(HashSet<string> licenses)
        {
            if (Matches(licenses))
                return Licenses.SelectMany(l => l.FindMatch(licenses));
            else
                return Enumerable.Empty<LicenseBase>();
        }
 
        public LicenseAll(LicenseBase[] licenseBase)
        {
            Licenses = licenseBase;
        }
    }
}