-
Notifications
You must be signed in to change notification settings - Fork 595
Expand file tree
/
Copy pathAttributeValueConditionToAnother.cs
More file actions
125 lines (107 loc) · 4.43 KB
/
AttributeValueConditionToAnother.cs
File metadata and controls
125 lines (107 loc) · 4.43 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
115
116
117
118
119
120
121
122
123
124
125
// 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.19 attribute should be of some value if another attribute is of some value
/// </summary>
internal class AttributeValueConditionToAnother : SemanticConstraint
{
private readonly OpenXmlQualifiedName _attribute;
private readonly OpenXmlQualifiedName _conditionAttribute;
private readonly string[] _values;
private readonly string[] _otherValues;
public AttributeValueConditionToAnother(OpenXmlQualifiedName attribute, OpenXmlQualifiedName conditionAttribute, string[] values, string[] otherValues)
: base(SemanticValidationLevel.Element)
{
_attribute = attribute;
_conditionAttribute = conditionAttribute;
_values = values;
_otherValues = otherValues;
}
public override ValidationErrorInfo? ValidateCore(ValidationContext context)
{
var element = context.Stack.Current?.Element;
if (element is null)
{
return null;
}
if (!TryFindAttribute(element, _attribute, out var attribute))
{
return null;
}
if (attribute.Value is null)
{
return null;
}
foreach (string value in _values)
{
if (AttributeValueEquals(attribute.Value, value, false))
{
return null;
}
}
if (!TryFindAttribute(element, _conditionAttribute, out var conditionAttribute))
{
return null;
}
if (conditionAttribute.Value is null)
{
return null;
}
foreach (string value in _otherValues)
{
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 attributeValueString = StringBuilderPool.GetValueAndRelease(sb);
var otherSb = StringBuilderPool.Acquire();
otherSb.Append('\'');
otherSb.Append(_otherValues[0]);
otherSb.Append('\'');
if (_otherValues.Length > 1)
{
for (int i = 1; i < _otherValues.Length - 1; i++)
{
otherSb.Append(", '");
otherSb.Append(_otherValues[i]);
otherSb.Append('\'');
}
otherSb.Append(" or '");
otherSb.Append(_otherValues[_otherValues.Length - 1]);
otherSb.Append('\'');
}
string otherAttributeValueString = StringBuilderPool.GetValueAndRelease(otherSb);
return new ValidationErrorInfo()
{
Id = "Sem_AttributeValueConditionToAnother",
ErrorType = ValidationErrorType.Semantic,
Node = element,
Description = SR.Format(
ValidationResources.Sem_AttributeValueConditionToAnother,
attribute.Property.QName, attributeValueString,
conditionAttribute.Property.QName, otherAttributeValueString,
attribute.Property.QName, attribute.Value),
};
}
}
return null;
}
}
}