Esempio n. 1
0
        /// <summary>
        /// Validates the specified element.
        /// </summary>
        /// <param name="openXmlElement">The target OpenXmlElement.</param>
        /// <returns>A set of validation erros.</returns>
        /// <exception cref="ArgumentNullException">Thrown when the "openXmlElement" parameter is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">Thrown when the "openXmlElement" is type of OpenXmlUnknownElement, OpenXmlMiscNode, AlternateContent, AlternateContentChoice or AlternateContentFallback.</exception>
        /// <exception cref="InvalidOperationException">Thrown when the "openXmlElement" is not defined in the specified FileFormat.</exception>
        public IEnumerable <ValidationErrorInfo> Validate(OpenXmlElement openXmlElement)
        {
            if (openXmlElement == null)
            {
                throw new ArgumentNullException(nameof(openXmlElement));
            }

            if (openXmlElement is OpenXmlUnknownElement)
            {
                throw new ArgumentOutOfRangeException(nameof(openXmlElement), ExceptionMessages.CannotValidateUnknownElement);
            }

            if (openXmlElement is OpenXmlMiscNode)
            {
                throw new ArgumentOutOfRangeException(nameof(openXmlElement), ExceptionMessages.CannotValidateMiscNode);
            }

            if (openXmlElement is AlternateContent ||
                openXmlElement is AlternateContentChoice ||
                openXmlElement is AlternateContentFallback)
            {
                throw new ArgumentOutOfRangeException(nameof(openXmlElement), ExceptionMessages.CannotValidateAcbElement);
            }

            if (!openXmlElement.IsInVersion(FileFormat))
            {
                switch (FileFormat)
                {
                case FileFormatVersions.Office2007:
                    throw new InvalidOperationException(ExceptionMessages.ElementIsNotInOffice2007);

                case FileFormatVersions.Office2010:
                    throw new InvalidOperationException(ExceptionMessages.ElementIsNotInOffice2010);

                case FileFormatVersions.Office2013:
                    throw new InvalidOperationException(ExceptionMessages.ElementIsNotInOffice2013);
                }
            }

            // TODO: if the FileFormat is Office2007, and the element is only in Office2010 and O15.
            // then this method should throw exceptions.
            var validationContext = new ValidationContext
            {
                FileFormat        = FileFormat,
                MaxNumberOfErrors = _settings.MaxNumberOfErrors,
                Element           = openXmlElement
            };

            SchemaValidator.Validate(validationContext);

            validationContext.Element = openXmlElement;
            FullSemanticValidator.Validate(validationContext);

            return(validationContext.Errors);
        }
Esempio n. 2
0
        /// <summary>
        /// Validates the specified element.
        /// </summary>
        /// <param name="openXmlElement">The target OpenXmlElement.</param>
        /// <returns>A set of validation errors.</returns>
        /// <exception cref="ArgumentNullException">Thrown when the "openXmlElement" parameter is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">Thrown when the "openXmlElement" is type of OpenXmlUnknownElement, OpenXmlMiscNode, AlternateContent, AlternateContentChoice or AlternateContentFallback.</exception>
        /// <exception cref="InvalidOperationException">Thrown when the "openXmlElement" is not defined in the specified FileFormat.</exception>
        public IEnumerable <ValidationErrorInfo> Validate(OpenXmlElement openXmlElement)
        {
            if (openXmlElement == null)
            {
                throw new ArgumentNullException(nameof(openXmlElement));
            }

            if (openXmlElement is OpenXmlUnknownElement)
            {
                throw new ArgumentOutOfRangeException(nameof(openXmlElement), ExceptionMessages.CannotValidateUnknownElement);
            }

            if (openXmlElement is OpenXmlMiscNode)
            {
                throw new ArgumentOutOfRangeException(nameof(openXmlElement), ExceptionMessages.CannotValidateMiscNode);
            }

            if (openXmlElement is AlternateContent ||
                openXmlElement is AlternateContentChoice ||
                openXmlElement is AlternateContentFallback)
            {
                throw new ArgumentOutOfRangeException(nameof(openXmlElement), ExceptionMessages.CannotValidateAcbElement);
            }

            FileFormat.ThrowIfNotInVersion(openXmlElement);

            var validationContext = new ValidationContext
            {
                FileFormat        = FileFormat,
                MaxNumberOfErrors = _settings.MaxNumberOfErrors,
                Element           = openXmlElement,
            };

            SchemaValidator.Validate(validationContext);

            validationContext.Element = openXmlElement;
            FullSemanticValidator.Validate(validationContext);

            return(validationContext.Errors);
        }