forked from dotnet/Open-XML-SDK
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttributeAbsentConditionToNonValue.cs
More file actions
90 lines (76 loc) · 2.89 KB
/
AttributeAbsentConditionToNonValue.cs
File metadata and controls
90 lines (76 loc) · 2.89 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
// 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;
using System.Text;
namespace DocumentFormat.OpenXml.Validation.Semantic
{
/// <summary>
/// 1.15 attribute should be absent if another attribute not equals some value
/// </summary>
internal class AttributeAbsentConditionToNonValue : SemanticConstraint
{
private readonly OpenXmlQualifiedName _absentAttribute;
private readonly OpenXmlQualifiedName _conditionAttribute;
private readonly string[] _values;
public AttributeAbsentConditionToNonValue(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))
{
return null;
}
}
var sb = new StringBuilder();
sb.Append('\'').Append(_values[0]).Append('\'');
if (_values.Length > 1)
{
for (int i = 1; i < _values.Length - 1; i++)
{
sb.Append(", '").Append(_values[i]).Append('\'');
}
sb.Append(" and '").Append(_values[_values.Length - 1]).Append('\'');
}
string valueString = sb.ToString();
return new ValidationErrorInfo()
{
Id = "Sem_AttributeAbsentConditionToNonValue",
ErrorType = ValidationErrorType.Semantic,
Node = element,
Description = SR.Format(
ValidationResources.Sem_AttributeAbsentConditionToNonValue,
absentAttribute.Property.QName,
conditionAttribute.Property.QName,
valueString),
};
}
}
}