-
Notifications
You must be signed in to change notification settings - Fork 588
Expand file tree
/
Copy pathCompatibilityRuleAttributesValidator.cs
More file actions
216 lines (195 loc) · 10.2 KB
/
CompatibilityRuleAttributesValidator.cs
File metadata and controls
216 lines (195 loc) · 10.2 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
namespace DocumentFormat.OpenXml.Validation.Schema
{
/// <summary>
/// Compatibility-Rule Attributes
/// </summary>
internal static class CompatibilityRuleAttributesValidator
{
/// <summary>
/// Validate compatibility rule attributes - Ignorable, ProcessContent, PreserveElements, PreserveAttributes, MustUnderstand.
/// </summary>
/// <param name="validationContext">The validation context.</param>
internal static void ValidateMcAttributes(ValidationContext validationContext)
{
var element = validationContext.Stack.Current?.Element;
if (element?.MCAttributes is null)
{
return;
}
HashSet<string>? ignorableNamespaces = null;
ValidationErrorInfo errorInfo;
if (element.MCAttributes is not null)
{
// validate Ignorable attribute
if (!string.IsNullOrEmpty(element.MCAttributes.Ignorable))
{
ignorableNamespaces = new HashSet<string>();
// rule: the prefix must already be defined.
var prefixes = new ListValue<StringValue>();
prefixes.InnerText = element.MCAttributes.Ignorable;
foreach (var prefix in prefixes.Items)
{
if (prefix.Value is not null)
{
var ignorableNamespace = element.LookupNamespace(prefix.Value);
if (ignorableNamespace.IsNullOrEmpty())
{
// error, the prefix is not defined.
errorInfo = validationContext.ComposeMcValidationError(element, "MC_InvalidIgnorableAttribute", element.MCAttributes.Ignorable);
validationContext.AddError(errorInfo);
}
else
{
ignorableNamespaces.Add(ignorableNamespace);
}
}
}
}
// validate PreserveAttributes attribute
if (!string.IsNullOrEmpty(element.MCAttributes.PreserveAttributes))
{
// The ProcessAttributes attribute value shall not reference any attribute name that does not belong to a namespace
// that is identified by the Ignorable attribute of the same element.
if (ignorableNamespaces is null)
{
// must have Ignorable on same element.
errorInfo = validationContext.ComposeMcValidationError(element, "MC_InvalidPreserveAttributesAttribute", element.MCAttributes.PreserveAttributes);
validationContext.AddError(errorInfo);
}
else
{
var errorQName = ValidateQNameList(element.MCAttributes.PreserveAttributes, ignorableNamespaces, validationContext);
if (!errorQName.IsNullOrEmpty())
{
errorInfo = validationContext.ComposeMcValidationError(element, "MC_InvalidPreserveAttributesAttribute", element.MCAttributes.PreserveAttributes);
validationContext.AddError(errorInfo);
}
}
}
// validate PreserveElements attribute
if (!string.IsNullOrEmpty(element.MCAttributes.PreserveElements))
{
// The ProcessAttributes attribute value shall not reference any attribute name that does not belong to a namespace
// that is identified by the Ignorable attribute of the same element.
if (ignorableNamespaces is null)
{
// must have Ignorable on same element.
errorInfo = validationContext.ComposeMcValidationError(element, "MC_InvalidPreserveElementsAttribute", element.MCAttributes.PreserveElements);
validationContext.AddError(errorInfo);
}
else
{
var errorQName = ValidateQNameList(element.MCAttributes.PreserveElements, ignorableNamespaces, validationContext);
if (!errorQName.IsNullOrEmpty())
{
errorInfo = validationContext.ComposeMcValidationError(element, "MC_InvalidPreserveElementsAttribute", element.MCAttributes.PreserveElements);
validationContext.AddError(errorInfo);
}
}
}
// validate ProcessContent attribute
if (!string.IsNullOrEmpty(element.MCAttributes.ProcessContent))
{
// The ProcessAttributes attribute value shall not reference any attribute name that does not belong to a namespace
// that is identified by the Ignorable attribute of the same element.
if (ignorableNamespaces is null)
{
// must have Ignorable on same element.
errorInfo = validationContext.ComposeMcValidationError(element, "MC_InvalidProcessContentAttribute", element.MCAttributes.ProcessContent);
validationContext.AddError(errorInfo);
}
else
{
var errorQName = ValidateQNameList(element.MCAttributes.ProcessContent, ignorableNamespaces, validationContext);
if (!errorQName.IsNullOrEmpty())
{
errorInfo = validationContext.ComposeMcValidationError(element, "MC_InvalidProcessContentAttribute", element.MCAttributes.ProcessContent);
validationContext.AddError(errorInfo);
}
}
foreach (var exAttribute in element.ExtendedAttributes)
{
// Markup consumers that encounter a non-ignored element that has an xml:lang or xml:space attribute and is also identified by a ProcessContent attribute value might generate an error.
if (AlternateContentValidator.IsXmlSpaceOrXmlLangAttribue(exAttribute))
{
// report error.
errorInfo = validationContext.ComposeMcValidationError(element, "MC_InvalidXmlAttributeWithProcessContent");
validationContext.AddError(errorInfo);
}
}
}
if (!string.IsNullOrEmpty(element.MCAttributes.MustUnderstand))
{
// TODO: MustUnderstand
// A markup consumer that does not understand these identified namespaces shall not continue to process the markup document
// rule: the prefix must already be defined.
var prefixes = new ListValue<StringValue>();
prefixes.InnerText = element.MCAttributes.MustUnderstand;
foreach (var prefix in prefixes.Items)
{
if (prefix.Value is not null)
{
var mustunderstandNamespace = element.LookupNamespace(prefix.Value);
if (string.IsNullOrEmpty(mustunderstandNamespace))
{
// report error, the prefix is not defined.
errorInfo = validationContext.ComposeMcValidationError(element, "MC_InvalidMustUnderstandAttribute", element.MCAttributes.MustUnderstand);
validationContext.AddError(errorInfo);
}
}
}
}
}
}
/// <summary>
/// Validate QName list in PreserveElements, PreserveAttributes, ProcessContent.
/// </summary>
/// <param name="qnameList">The QName list to be validated.</param>
/// <param name="ignorableNamespaces">The ignorable namespaces.</param>
/// <param name="validationContext"></param>
/// <returns>The QName that is not valid.</returns>
internal static string? ValidateQNameList(string? qnameList, HashSet<string> ignorableNamespaces, ValidationContext validationContext)
{
if (qnameList is null)
{
return null;
}
var element = validationContext.Stack.Current?.Element;
if (element is null)
{
return null;
}
var qnames = new ListValue<StringValue>();
qnames.InnerText = qnameList;
foreach (var qname in qnames.Items)
{
if (qname.Value is null)
{
continue;
}
// must be QName
var colonIndex = qname.Value.IndexOf(":", StringComparison.Ordinal);
if (colonIndex <= 0 || colonIndex == qname.Value.Length - 1 || qname.Value.IndexOf(":", colonIndex + 1, StringComparison.Ordinal) >= 0)
{
return qname;
}
// Prefix must be already defined.
var attributeNamesapce = element.LookupNamespace(qname.Value.Substring(0, colonIndex));
if (attributeNamesapce.IsNullOrEmpty())
{
return qname;
}
// The namespace must be identified by the Ignorable attribute at the same element.
if (!ignorableNamespaces.Contains(attributeNamesapce))
{
return qname;
}
}
return null;
}
}
}