|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using DocumentFormat.OpenXml.Validation; |
| 5 | + |
| 6 | +namespace DocumentFormat.OpenXml.Internal.SchemaValidation |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Validator for MarkupCompatibility feature - AlternateContent. |
| 10 | + /// </summary> |
| 11 | + /// <remarks> |
| 12 | + /// See Ecma376 "Part 5: Markup Compatibility and Extensibility" for reference. |
| 13 | + /// </remarks> |
| 14 | + internal class AlternateContentValidator |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// Validate ACB syntax - AlternateContent, Choice, Fallback and their attributes. |
| 18 | + /// </summary> |
| 19 | + /// <param name="validationContext"></param> |
| 20 | + internal static void Validate(ValidationContext validationContext) |
| 21 | + { |
| 22 | + AlternateContent acElement = (AlternateContent)validationContext.Element; |
| 23 | + |
| 24 | + // Validate MC attribute on AlternateContent |
| 25 | + ValidateMcAttributesOnAcb(validationContext, acElement); |
| 26 | + |
| 27 | + int status = 0; |
| 28 | + ValidationErrorInfo errorInfo; |
| 29 | + |
| 30 | + if (acElement.ChildElements.Count == 0) |
| 31 | + { |
| 32 | + // Rule: An AlternateContent element shall contain one or more Choice child elements |
| 33 | + errorInfo = validationContext.ComposeMcValidationError(acElement, "Sch_IncompleteContentExpectingComplex", ValidationResources.MC_ShallContainChoice); |
| 34 | + validationContext.EmitError(errorInfo); |
| 35 | + } |
| 36 | + |
| 37 | + OpenXmlElement child; |
| 38 | + |
| 39 | + child = acElement.GetFirstNonMiscElementChild(); |
| 40 | + |
| 41 | + while (child != null) |
| 42 | + { |
| 43 | + if (child is AlternateContent) |
| 44 | + { |
| 45 | + // Rule: An AlternateContent element shall not be the child of an AlternateContent element. |
| 46 | + errorInfo = validationContext.ComposeMcValidationError(acElement, "Sch_InvalidElementContentExpectingComplex", child.XmlQualifiedName.ToString(), ValidationResources.MC_ShallNotContainAlternateContent); |
| 47 | + validationContext.EmitError(errorInfo); |
| 48 | + } |
| 49 | + else |
| 50 | + { |
| 51 | + switch (status) |
| 52 | + { |
| 53 | + case 0: |
| 54 | + // expect a Choice |
| 55 | + if (child is AlternateContentChoice) |
| 56 | + { |
| 57 | + // validate the MC attributes on Choice |
| 58 | + ValidateMcAttributesOnAcb(validationContext, child); |
| 59 | + status = 1; |
| 60 | + } |
| 61 | + else |
| 62 | + { |
| 63 | + // Rule: An AlternateContent element shall contain one or more Choice child elements |
| 64 | + errorInfo = validationContext.ComposeMcValidationError(acElement, "Sch_IncompleteContentExpectingComplex", ValidationResources.MC_ShallContainChoice); |
| 65 | + validationContext.EmitError(errorInfo); |
| 66 | + |
| 67 | + if (child is AlternateContentFallback) |
| 68 | + { |
| 69 | + // validate the MC attributes on Fallback |
| 70 | + ValidateMcAttributesOnAcb(validationContext, child); |
| 71 | + } |
| 72 | + } |
| 73 | + break; |
| 74 | + |
| 75 | + case 1: |
| 76 | + // Already one Choice, expect Choice or Fallback |
| 77 | + if (child is AlternateContentChoice) |
| 78 | + { |
| 79 | + // validate the MC attributes on Choice |
| 80 | + ValidateMcAttributesOnAcb(validationContext, child); |
| 81 | + status = 1; |
| 82 | + } |
| 83 | + else if (child is AlternateContentFallback) |
| 84 | + { |
| 85 | + // validate the MC attributes on Fallback |
| 86 | + ValidateMcAttributesOnAcb(validationContext, child); |
| 87 | + status = 2; |
| 88 | + } |
| 89 | + else |
| 90 | + { |
| 91 | + errorInfo = validationContext.ComposeMcValidationError(acElement, "Sch_InvalidElementContentExpectingComplex", child.XmlQualifiedName.ToString(), ValidationResources.MC_ShallContainChoice); |
| 92 | + validationContext.EmitError(errorInfo); |
| 93 | + } |
| 94 | + break; |
| 95 | + |
| 96 | + case 2: |
| 97 | + // Already one Fallback. Can not have more than one Fallback |
| 98 | + errorInfo = validationContext.ComposeMcValidationError(acElement, "Sch_InvalidElementContentExpectingComplex", child.XmlQualifiedName.ToString(), ValidationResources.MC_ShallContainChoice); |
| 99 | + validationContext.EmitError(errorInfo); |
| 100 | + break; |
| 101 | + } |
| 102 | + } |
| 103 | + child = child.GetNextNonMiscElementSibling(); |
| 104 | + } |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + /// <summary> |
| 109 | + /// Validate attributes on AlternateContent, Choice and Fallback element. |
| 110 | + /// </summary> |
| 111 | + /// <param name="validationContext"></param> |
| 112 | + /// <param name="acElement">The element to be validated.</param> |
| 113 | + private static void ValidateMcAttributesOnAcb(ValidationContext validationContext, OpenXmlElement acElement) |
| 114 | + { |
| 115 | + ValidationErrorInfo errorInfo; |
| 116 | + |
| 117 | + // AlternateContent elements might include the attributes Ignorable, MustUnderstand, ProcessContent, PreserveElements, and PreserveAttributes |
| 118 | + // These attributes’ qualified names shall be prefixed when associated with an AlternateContent / Choice / Fallback element. |
| 119 | + // A markup consumer shall generate an error if it encounters an unprefixed attribute name associated with an AlternateContent element. |
| 120 | + if (acElement.ExtendedAttributes != null) |
| 121 | + { |
| 122 | + foreach (var exAttribute in acElement.ExtendedAttributes) |
| 123 | + { |
| 124 | + if (string.IsNullOrEmpty(exAttribute.Prefix)) |
| 125 | + { |
| 126 | + // error on any unprefixed attributes |
| 127 | + errorInfo = validationContext.ComposeMcValidationError(acElement, ValidationResources.MC_ErrorOnUnprefixedAttributeName, exAttribute.XmlQualifiedName.ToString()); |
| 128 | + validationContext.EmitError(errorInfo); |
| 129 | + } |
| 130 | + |
| 131 | + // Markup consumers shall generate an error if they encounter the xml:lang or xml:space attributes on an AlternateContent element. |
| 132 | + // Markup consumers shall generate an error if they encounter the xml:lang or xml:space attributes on a Choice element, regardless of whether the element is preceded by a selected Choice element. |
| 133 | + // Markup consumers shall generate an error if they encounter the xml:lang or xml:space attributes on a Fallback element, regardless of whether the element is preceded by a selected Choice element. |
| 134 | + if (IsXmlSpaceOrXmlLangAttribue(exAttribute)) |
| 135 | + { |
| 136 | + // report error. |
| 137 | + errorInfo = validationContext.ComposeMcValidationError(acElement, "MC_InvalidXmlAttribute", acElement.LocalName); |
| 138 | + validationContext.EmitError(errorInfo); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + // validate MC attribues (Ignorable, PreserveElements, etc.) of this element. |
| 144 | + CompatibilityRuleAttributesValidator.ValidateMcAttributes(validationContext); |
| 145 | + |
| 146 | + AlternateContentChoice choice = acElement as AlternateContentChoice; |
| 147 | + if (choice != null) |
| 148 | + { |
| 149 | + // All Choice elements shall have a Requires attribute whose value contains a whitespace-delimited list of namespace prefixes |
| 150 | + if (choice.Requires == null) |
| 151 | + { |
| 152 | + // report error |
| 153 | + errorInfo = validationContext.ComposeMcValidationError(acElement, "MC_MissedRequiresAttribute"); |
| 154 | + validationContext.EmitError(errorInfo); |
| 155 | + } |
| 156 | + else |
| 157 | + { |
| 158 | + var prefixes = new ListValue<StringValue>(); |
| 159 | + prefixes.InnerText = choice.Requires; |
| 160 | + foreach (var prefix in prefixes.Items) |
| 161 | + { |
| 162 | + var ignorableNamespace = choice.LookupNamespace(prefix); |
| 163 | + if (string.IsNullOrEmpty(ignorableNamespace)) |
| 164 | + { |
| 165 | + // report error, the prefix is not defined. |
| 166 | + errorInfo = validationContext.ComposeMcValidationError(choice, "MC_InvalidRequiresAttribute", choice.Requires); |
| 167 | + validationContext.EmitError(errorInfo); |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + /// <summary> |
| 175 | + /// Test whether the attribute is "xml:space" or "xml:lang". |
| 176 | + /// </summary> |
| 177 | + /// <param name="attribute">The attribute to be tested.</param> |
| 178 | + /// <returns>True if the attribute is "xml:space" or "xml:lang".</returns> |
| 179 | + internal static bool IsXmlSpaceOrXmlLangAttribue(OpenXmlAttribute attribute) |
| 180 | + { |
| 181 | + if ("http://www.w3.org/XML/1998/namespace" == attribute.NamespaceUri) |
| 182 | + { |
| 183 | + if (attribute.LocalName == "space" || attribute.LocalName == "lang") |
| 184 | + { |
| 185 | + return true; |
| 186 | + } |
| 187 | + } |
| 188 | + return false; |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments