-
Notifications
You must be signed in to change notification settings - Fork 595
Expand file tree
/
Copy pathAttributeMutualExclusive.cs
More file actions
114 lines (94 loc) · 4.08 KB
/
AttributeMutualExclusive.cs
File metadata and controls
114 lines (94 loc) · 4.08 KB
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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using DocumentFormat.OpenXml.Framework;
namespace DocumentFormat.OpenXml.Validation.Semantic
{
/// <summary>
/// 1.16 only one of a group attributes can exist
/// </summary>
internal class AttributeMutualExclusive : SemanticConstraint
{
private readonly OpenXmlQualifiedName[] _attributes;
public AttributeMutualExclusive(OpenXmlQualifiedName attribute)
: this(new[] { attribute })
{
}
public AttributeMutualExclusive(OpenXmlQualifiedName attribute1, OpenXmlQualifiedName attribute2)
: this(new[] { attribute1, attribute2 })
{
}
public AttributeMutualExclusive(OpenXmlQualifiedName attribute1, OpenXmlQualifiedName attribute2, OpenXmlQualifiedName attribute3)
: this(new[] { attribute1, attribute2, attribute3 })
{
}
public AttributeMutualExclusive(OpenXmlQualifiedName attribute1, OpenXmlQualifiedName attribute2, OpenXmlQualifiedName attribute3, OpenXmlQualifiedName attribute4)
: this(new[] { attribute1, attribute2, attribute3, attribute4 })
{
}
public AttributeMutualExclusive(OpenXmlQualifiedName attribute1, OpenXmlQualifiedName attribute2, OpenXmlQualifiedName attribute3, OpenXmlQualifiedName attribute4, OpenXmlQualifiedName attribute5)
: this(new[] { attribute1, attribute2, attribute3, attribute4, attribute5 })
{
}
public AttributeMutualExclusive(OpenXmlQualifiedName attribute1, OpenXmlQualifiedName attribute2, OpenXmlQualifiedName attribute3, OpenXmlQualifiedName attribute4, OpenXmlQualifiedName attribute5, OpenXmlQualifiedName attribute6)
: this(new[] { attribute1, attribute2, attribute3, attribute4, attribute5, attribute6 })
{
}
private AttributeMutualExclusive(OpenXmlQualifiedName[] attributes)
: base(SemanticValidationLevel.Element)
{
_attributes = attributes;
}
public override ValidationErrorInfo? ValidateCore(ValidationContext context)
{
var element = context.Stack.Current?.Element;
if (element is null)
{
return null;
}
var attributesSb = StringBuilderPool.Acquire();
var existAttributeSb = StringBuilderPool.Acquire();
string? existAttribute2 = null;
foreach (var attribute in _attributes)
{
if (attributesSb.Length > 0)
{
attributesSb.Append(',');
}
attributesSb.Append(attribute);
if (!TryFindAttribute(element, attribute, out var found))
{
continue;
}
if (found.Value is not null)
{
if (existAttribute2 is not null)
{
if (existAttributeSb.Length > 0)
{
existAttributeSb.Append(',');
}
existAttributeSb.Append(existAttribute2);
}
existAttribute2 = found.ToString();
}
}
if (existAttributeSb.Length == 0)
{
StringBuilderPool.Release(attributesSb);
StringBuilderPool.Release(existAttributeSb);
return null;
}
return new ValidationErrorInfo()
{
Id = "Sem_AttributeMutualExclusive",
ErrorType = ValidationErrorType.Semantic,
Node = element,
Description = SR.Format(
ValidationResources.Sem_AttributeMutualExclusive,
StringBuilderPool.GetValueAndRelease(existAttributeSb),
existAttribute2,
StringBuilderPool.GetValueAndRelease(attributesSb)),
};
}
}
}