Exemple #1
0
 public void AddError(ValidationErrorInfo error)
 {
     if (error is not null && !CheckIfCancelled())
     {
         Stack.Current.AddError(error);
     }
 }
Exemple #2
0
 public void AddError(ValidationErrorInfo error)
 {
     if (error != null && !IsCancelled)
     {
         _errors.Add(error);
     }
 }
Exemple #3
0
 public void AddError(ValidationErrorInfo error)
 {
     if (error != null && !IsCancelled)
     {
         Stack.Current.AddError(error);
     }
 }
        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);
            }
        }
Exemple #5
0
        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);
            }
        }
 public ValidationInfo(OpenXmlPowerToolsDocument doc, ValidationErrorInfo err)
 {
     Document = doc;
     FileName = doc.FileName;
     Description = err.Description;
     ErrorType = err.ErrorType;
     Id = err.Id;
     Node = err.Node;
     Part = err.Part;
     XPath = err.Path.XPath;
     RelatedNode = err.RelatedNode;
     RelatedPart = err.RelatedPart;
 }
Exemple #7
0
        public void CreateError(string id, ValidationErrorType errorType, string description = null)
        {
            var current = Stack.Current;

            var error = new ValidationErrorInfo
            {
                Id          = id,
                Description = description,
                Part        = current.Part,
                ErrorType   = errorType,
                Node        = current.Element,
            };

            AddError(error);
        }
Exemple #8
0
 public DocumentError(ValidationErrorInfo err)
 {
     this.description = err.Description ?? "";
     this.pathXPath = err.Path != null ? err.Path.XPath : "";
     this.pathNamespacesDefinitions = err.Path != null ? err.Path.NamespacesDefinitions : null;
     this.id = err.Id ?? "";
     this.part = err.Part != null ? err.Part.ToString() : "";
     this.nodeInnerXml = err.Node != null ? err.Node.InnerXml : "";
     this.nodeOuterXml = err.Node != null ? err.Node.OuterXml : "";
     this.nodeInnerText = err.Node != null ? err.Node.InnerText : "";
     this.relatedNodeInnerXml = err.RelatedNode != null ? err.RelatedNode.InnerXml : "";
     this.relatedNodeInnerText = err.RelatedNode != null ? err.RelatedNode.InnerText : "";
     this.relatedNodeOuterXml = err.RelatedNode != null ? err.RelatedNode.OuterXml : "";
     this.relatedPart = err.RelatedPart != null ? err.RelatedPart.ToString() : "";
     this.relatedNode = err.RelatedNode != null ? err.RelatedNode.ToString() : "";
 }
Exemple #9
0
        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);
            }
        }
Exemple #10
0
        private static void ValidatePackageStructure(OpenXmlPackage document, ValidationContext context)
        {
            var documentName = document.GetType().Name;

            var errors = new PackageValidator(document).Validate(context.FileFormat)
                         .Select(e =>
            {
                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 = SR.Format(ValidationResources.Pkg_PartIsNotAllowed, name, GetPartNameAndUri(e.SubPart));
                    break;

                case "Pkg_RequiredPartDoNotExist":
                    errorInfo.Description = SR.Format(ValidationResources.Pkg_RequiredPartDoNotExist, e.PartClassName);
                    break;

                case "Pkg_OnlyOnePartAllowed":
                    name = e.Part != null ? GetPartNameAndUri(e.Part) : documentName;
                    errorInfo.Description = SR.Format(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 = SR.Format(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 = SR.Format(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.RelatedPart = e.SubPart;

                return(errorInfo);
            });

            context.Errors.AddRange(errors);
        }
 internal ValidationErrorEventArgs(ValidationErrorInfo validatoinError)
 {
     this.ValidationErrorInfo = validatoinError;
 }
Exemple #12
0
 internal ValidationErrorEventArgs(ValidationErrorInfo validatoinError)
 {
     this.ValidationErrorInfo = validatoinError;
 }
Exemple #13
0
        private void OnPackageValidationError(Object sender, OpenXmlPackageValidationEventArgs e)
        {
            ValidationErrorInfo errorInfo = new ValidationErrorInfo();

            errorInfo.ErrorType = ValidationErrorType.Package;
            errorInfo.Id        = "Pkg_" + e.MessageId;

            string name;

            switch (errorInfo.Id)
            {
            case "Pkg_PartIsNotAllowed":
                Debug.Assert(e.SubPart != null);
                name = e.Part != null?GetPartNameAndUri(e.Part) : GetDocumentName(this.TargetDocument);

                errorInfo.Description = String.Format(CultureInfo.CurrentUICulture, ValidationResources.Pkg_PartIsNotAllowed, name, GetPartNameAndUri(e.SubPart));
                break;

            case "Pkg_RequiredPartDoNotExist":
                errorInfo.Description = string.Format(System.Globalization.CultureInfo.CurrentUICulture,
                                                      ValidationResources.Pkg_RequiredPartDoNotExist, e.PartClassName);
                break;

            case "Pkg_OnlyOnePartAllowed":
                name = e.Part != null?GetPartNameAndUri(e.Part) : GetDocumentName(this.TargetDocument);

                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(System.Globalization.CultureInfo.CurrentUICulture,
                                                      ValidationResources.Pkg_ExtendedPartIsOpenXmlPart, GetPartUri(e.SubPart));
                break;

            case "Pkg_DataPartReferenceIsNotAllowed":
                Debug.Assert(e.DataPartReferenceRelationship != null);
                name = e.Part != null?GetPartNameAndUri(e.Part) : GetDocumentName(this.TargetDocument);

                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;

            this.ValidationResult.AddError(errorInfo);
        }
 /// <summary>
 /// Used by validator to emit errors.
 /// </summary>
 /// <param name="error">The validation error.</param>
 internal void EmitError(ValidationErrorInfo error)
 {
     OnValidationError(new ValidationErrorEventArgs(error));
 }
 public OpenXmlValidationException(ValidationErrorInfo[] errorsInfo, string message) : base(message) { ValidationErrors = errorsInfo; }
 /// <summary>
 /// Used by validator to emit errors.
 /// </summary>
 /// <param name="error">The validation error.</param>
 internal void EmitError(ValidationErrorInfo error)
 {
     OnValidationError(new ValidationErrorEventArgs(error));
 }
        /// <summary>
        /// Validate the specified part.
        /// </summary>
        /// <param name="part">The OpenXmlPart to be validated.</param>
        internal void ValidatePart(OpenXmlPart part)
        {
            // if the part is not defined in the specified version, then do not validate the content.
            if ( ! part.IsInVersion(this.ValidationSettings.FileFormat))
            {
                return;
            }

            /*******************
             * DOM traverse is not performance bottleneck.
             * Is this the good way that we separate the schema validtion and the semantics validation?
             *******************/

            try
            {
                // Must be called before the call to PartRootElement { get; }
                bool partRootElementLoaded = part.IsRootElementLoaded;

                // schema validation
                this.ValidationContext.Part = part;
                this.ValidationContext.Element = part.PartRootElement;

                var lastErrorCount = this.ValidationResult.Errors.Count;

                if (part.PartRootElement != null)
                {
                    this.SchemaValidator.Validate(this.ValidationContext);

                    this.ValidationContext.Element = part.PartRootElement;
                    this.SemanticValidator.ClearConstraintState(SemanticValidationLevel.PartOnly);
                    this.SemanticValidator.Validate(this.ValidationContext);
                }

                if (!partRootElementLoaded &&
                    this.ValidationResult.Errors.Count == lastErrorCount)
                {
                    // No new errors in this part. Release the DOM to GC memary.
                    part.SetPartRootElementToNull();
                }
            }
            catch (System.Xml.XmlException e)
            {
                ValidationErrorInfo errorInfo = new ValidationErrorInfo();
                errorInfo.ErrorType = ValidationErrorType.Schema;
                errorInfo.Id = "ExceptionError";
                errorInfo.Part = part;
                errorInfo.Path = new XmlPath(part);
                errorInfo.Description = string.Format(System.Globalization.CultureInfo.CurrentUICulture,
                    ValidationResources.ExceptionError, e.Message);
                this.ValidationResult.AddError(errorInfo);
            }
        }
        private void OnPackageValidationError(Object sender, OpenXmlPackageValidationEventArgs e)
        {            
            ValidationErrorInfo errorInfo = new ValidationErrorInfo();
            errorInfo.ErrorType = ValidationErrorType.Package;
            errorInfo.Id = "Pkg_" + e.MessageId;

            string name;

            switch (errorInfo.Id)
            {
                case "Pkg_PartIsNotAllowed":
                    Debug.Assert(e.SubPart != null);
                    name = e.Part != null ? GetPartNameAndUri(e.Part) : GetDocumentName(this.TargetDocument);
                    errorInfo.Description = String.Format(CultureInfo.CurrentUICulture, ValidationResources.Pkg_PartIsNotAllowed, name, GetPartNameAndUri(e.SubPart));
                    break;

                case "Pkg_RequiredPartDoNotExist":
                    errorInfo.Description = string.Format(System.Globalization.CultureInfo.CurrentUICulture,
                                                ValidationResources.Pkg_RequiredPartDoNotExist, e.PartClassName);
                    break;

                case "Pkg_OnlyOnePartAllowed":
                    name = e.Part != null ? GetPartNameAndUri(e.Part) : GetDocumentName(this.TargetDocument);
                    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(System.Globalization.CultureInfo.CurrentUICulture,
                                                ValidationResources.Pkg_ExtendedPartIsOpenXmlPart, GetPartUri(e.SubPart));
                    break;

                case "Pkg_DataPartReferenceIsNotAllowed":
                    Debug.Assert(e.DataPartReferenceRelationship != null);
                    name = e.Part != null ? GetPartNameAndUri(e.Part) : GetDocumentName(this.TargetDocument);
                    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;

            this.ValidationResult.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);
            }
        }
        private void AssertValidationErrorCategory(string expected, ValidationErrorInfo targetErrorInfo)
        {
#if DEBUG
            Assert.Equal(expected, targetErrorInfo.ValidationErrorCategory);
#endif
        }
 internal void AddError(ValidationErrorInfo error)
 {
     this.Valid = false;
     this.Errors.Add(error);
 }
        internal static ValidationErrorInfo ComposeValidationError(this ValidationContext validationContext, ValidationErrorType errorType, OpenXmlElement element, OpenXmlElement child, 
                                                       string messageId, params string[] args)
        {
            ValidationErrorInfo errorInfo = new ValidationErrorInfo()
            {
                ErrorType = errorType,
                Part = validationContext.Part,
                Node = element,
                Id = messageId,
                RelatedNode = child,
                Description = string.Format(CultureInfo.CurrentUICulture, ValidationResources.ResourceManager.GetString(messageId), args)
            };

            return errorInfo;
        }
Exemple #23
0
 internal void AddError(ValidationErrorInfo error)
 {
     this.Valid = false;
     this.Errors.Add(error);
 }