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);
            }
        }
Beispiel #2
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);
            }
        }
Beispiel #3
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.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);
            }
        }
        /// <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);
            }
        }