private void ValidatePart(OpenXmlPart part, ValidationContext context) { // if the part is not defined in the specified version, then do not validate the content. if (!part.IsInVersion(_cache.Version)) { return; } /******************* * DOM traversal is not performance bottleneck. * Is this the good way that we separate the schema validation and the semantics validation? *******************/ try { // Must be called before the call to PartRootElement { get; } bool partRootElementLoaded = part.IsRootElementLoaded; // Schema validation using (context.Stack.Push(part: part, element: part.PartRootElement)) { var lastErrorCount = context.Errors.Count; if (part.PartRootElement != null) { _schemaValidator.Validate(context); // TODO: Is this needed? It's set 8 lines up using (context.Stack.Push(element: part.PartRootElement)) { context.Events.OnPartValidationStarted(context); _semanticValidator.Validate(context); } } if (!partRootElementLoaded && context.Errors.Count == lastErrorCount) { // No new errors in this part. Release the DOM to GC memory. part.SetPartRootElementToNull(); } } } catch (System.Xml.XmlException e) { var errorInfo = new ValidationErrorInfo { ErrorType = ValidationErrorType.Schema, Id = "ExceptionError", Part = part, Description = SR.Format(ValidationResources.ExceptionError, e.Message), }; context.AddError(errorInfo); } }
private void ValidatePart(OpenXmlPart part, ValidationContext context) { // if the part is not defined in the specified version, then do not validate the content. if (!part.IsInVersion(_validationSettings.FileFormat)) { return; } /******************* * DOM traversal is not performance bottleneck. * Is this the good way that we separate the schema validation and the semantics validation? *******************/ try { // Must be called before the call to PartRootElement { get; } bool partRootElementLoaded = part.IsRootElementLoaded; // schema validation context.Part = part; context.Element = part.PartRootElement; var lastErrorCount = context.Errors.Count; if (part.PartRootElement != null) { _schemaValidator.Validate(context); context.Element = part.PartRootElement; _semanticValidator.ClearConstraintState(SemanticValidationLevel.PartOnly); _semanticValidator.Validate(context); } if (!partRootElementLoaded && context.Errors.Count == lastErrorCount) { // No new errors in this part. Release the DOM to GC memory. part.SetPartRootElementToNull(); } } catch (System.Xml.XmlException e) { var errorInfo = new ValidationErrorInfo { ErrorType = ValidationErrorType.Schema, Id = "ExceptionError", Part = part, Path = new XmlPath(part), Description = string.Format(CultureInfo.CurrentUICulture, ValidationResources.ExceptionError, e.Message) }; context.AddError(errorInfo); } }
private void ValidatePart(OpenXmlPart part, ValidationContext context) { // if the part is not defined in the specified version, then do not validate the content. if (!part.IsInVersion(_cache.Version)) { return; } try { // Must be called before the call to PartRootElement { get; } bool partRootElementLoaded = part.IsRootElementLoaded; // Schema validation using (context.Stack.Push(part: part, element: part.PartRootElement)) { var lastErrorCount = context.Errors.Count; if (part.PartRootElement is not null) { Validate(context); } if (!partRootElementLoaded && context.Errors.Count == lastErrorCount) { // No new errors in this part. Release the DOM to GC memory. part.UnloadRootElement(); } } } catch (System.Xml.XmlException e) { var errorInfo = new ValidationErrorInfo { ErrorType = ValidationErrorType.Schema, Id = "ExceptionError", Part = part, Description = SR.Format(ValidationResources.ExceptionError, e.Message), }; context.AddError(errorInfo); } }
private void ValidatePackageStructure(OpenXmlPackage document, ValidationContext context) { var documentName = document.GetType().Name; #pragma warning disable CS0618 // Type or member is obsolete var packageValidationSettings = new OpenXmlPackageValidationSettings(); #pragma warning restore CS0618 // Type or member is obsolete packageValidationSettings.EventHandler += OnPackageValidationError; document.Validate(packageValidationSettings, _validationSettings.FileFormat); #pragma warning disable CS0618 // Type or member is obsolete void OnPackageValidationError(Object sender, OpenXmlPackageValidationEventArgs e) #pragma warning restore CS0618 // Type or member is obsolete { var errorInfo = new ValidationErrorInfo { ErrorType = ValidationErrorType.Package, Id = "Pkg_" + e.MessageId }; string name; switch (errorInfo.Id) { case "Pkg_PartIsNotAllowed": Debug.Assert(e.SubPart != null); name = e.Part != null?GetPartNameAndUri(e.Part) : documentName; errorInfo.Description = String.Format(CultureInfo.CurrentUICulture, ValidationResources.Pkg_PartIsNotAllowed, name, GetPartNameAndUri(e.SubPart)); break; case "Pkg_RequiredPartDoNotExist": errorInfo.Description = string.Format(CultureInfo.CurrentUICulture, ValidationResources.Pkg_RequiredPartDoNotExist, e.PartClassName); break; case "Pkg_OnlyOnePartAllowed": name = e.Part != null?GetPartNameAndUri(e.Part) : documentName; errorInfo.Description = String.Format(CultureInfo.CurrentUICulture, ValidationResources.Pkg_OnlyOnePartAllowed, name, e.PartClassName); #if DEBUG Debug.Assert(e.SubPart != null); errorInfo.RelatedPart = e.SubPart; #endif break; case "Pkg_ExtendedPartIsOpenXmlPart": Debug.Assert(e.SubPart != null); errorInfo.Description = string.Format(CultureInfo.CurrentUICulture, ValidationResources.Pkg_ExtendedPartIsOpenXmlPart, GetPartUri(e.SubPart)); break; case "Pkg_DataPartReferenceIsNotAllowed": Debug.Assert(e.DataPartReferenceRelationship != null); name = e.Part != null?GetPartNameAndUri(e.Part) : documentName; errorInfo.Description = String.Format(CultureInfo.CurrentUICulture, ValidationResources.Pkg_PartIsNotAllowed, name, e.DataPartReferenceRelationship.Uri); break; case "Pkg_InvalidContentTypePart": // won't get this error. default: Debug.Assert(false, "Invalid package validation event."); break; } if (e.Part != null) { errorInfo.Part = e.Part; errorInfo.Path = new XmlPath(e.Part); } errorInfo.RelatedPart = e.SubPart; context.AddError(errorInfo); } }