-
Notifications
You must be signed in to change notification settings - Fork 595
Expand file tree
/
Copy pathAttributeAbsentConditionToValue.cs
More file actions
95 lines (81 loc) · 3.22 KB
/
AttributeAbsentConditionToValue.cs
File metadata and controls
95 lines (81 loc) · 3.22 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
// 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.14 attribute should be absent if another attribute equals some value
/// </summary>
internal class AttributeAbsentConditionToValue : SemanticConstraint
{
private readonly OpenXmlQualifiedName _absentAttribute;
private readonly OpenXmlQualifiedName _conditionAttribute;
private readonly string[] _values;
public AttributeAbsentConditionToValue(OpenXmlQualifiedName absentAttribute, OpenXmlQualifiedName conditionAttribute, params string[] values)
: base(SemanticValidationLevel.Element)
{
_absentAttribute = absentAttribute;
_conditionAttribute = conditionAttribute;
_values = values;
}
public override ValidationErrorInfo? ValidateCore(ValidationContext context)
{
var element = context.Stack.Current?.Element;
if (element is null)
{
return null;
}
if (!TryFindAttribute(element, _absentAttribute, out var absentAttribute))
{
return null;
}
if (absentAttribute.Value is null)
{
return null;
}
if (!TryFindAttribute(element, _conditionAttribute, out var conditionAttribute))
{
return null;
}
if (conditionAttribute.Value is null)
{
return null;
}
foreach (string value in _values)
{
if (AttributeValueEquals(conditionAttribute.Value, value, false))
{
var sb = StringBuilderPool.Acquire();
sb.Append('\'');
sb.Append(_values[0]);
sb.Append('\'');
if (_values.Length > 1)
{
for (int i = 1; i < _values.Length - 1; i++)
{
sb.Append(", '");
sb.Append(_values[i]);
sb.Append('\'');
}
sb.Append(" or '");
sb.Append(_values[_values.Length - 1]);
sb.Append('\'');
}
string valueString = StringBuilderPool.GetValueAndRelease(sb);
return new ValidationErrorInfo()
{
Id = "Sem_AttributeAbsentConditionToValue",
ErrorType = ValidationErrorType.Semantic,
Node = element,
Description = SR.Format(
ValidationResources.Sem_AttributeAbsentConditionToValue,
absentAttribute.Property.QName,
conditionAttribute.Property.QName,
valueString),
};
}
}
return null;
}
}
}