/*************************************************************************** Copyright (c) Lowell Stewart 2018-2019. Licensed under the Mozilla Public License. See LICENSE file in the project root for full license information. Published at https://github.com/opendocx/opendocx Developer: Lowell Stewart Email: lowell@opendocx.com Portions Copyright (c) Microsoft. All rights reserved. Portions Copyright (c) Eric White Inc. All rights reserved. ***************************************************************************/ using System; using System.Collections.Generic; using System.Dynamic; using System.Threading.Tasks; using System.IO; using System.Text.RegularExpressions; using System.Linq; using System.Text; using System.Xml.Linq; using OpenXmlPowerTools; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Validation; namespace OpenDocx { public class Validator { public ValidateResult ValidateDocument(string documentFile) { bool hasErrors = false; string errorList; WmlDocument doc = new WmlDocument(documentFile); // reads the document's bytes into memory byte[] byteArray = doc.DocumentByteArray; using (MemoryStream ms = new MemoryStream()) { ms.Write(byteArray, 0, byteArray.Length); // copy document file (binary) into memory stream using (WordprocessingDocument wDoc = WordprocessingDocument.Open(ms, true)) { OpenXmlValidator v = new OpenXmlValidator(); var valErrors = v.Validate(wDoc).Where(ve => !s_ExpectedErrors.Contains(ve.Description)); StringBuilder sb = new StringBuilder(); foreach (var item in valErrors.Select(r => r.Description).OrderBy(t => t).Distinct()) { hasErrors = true; sb.Append(item).Append(Environment.NewLine); } errorList = sb.ToString(); } } return new ValidateResult(hasErrors, errorList); } #pragma warning disable CS1998 public async Task ValidateDocumentAsync(dynamic input) { var documentFile = (string)input.documentFile; await Task.Yield(); return ValidateDocument(documentFile); } #pragma warning restore CS1998 private static List s_ExpectedErrors = new List() { "The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:evenHBand' attribute is not declared.", "The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:evenVBand' attribute is not declared.", "The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:firstColumn' attribute is not declared.", "The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:firstRow' attribute is not declared.", "The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:firstRowFirstColumn' attribute is not declared.", "The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:firstRowLastColumn' attribute is not declared.", "The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:lastColumn' attribute is not declared.", "The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:lastRow' attribute is not declared.", "The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:lastRowFirstColumn' attribute is not declared.", "The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:lastRowLastColumn' attribute is not declared.", "The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:noHBand' attribute is not declared.", "The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:noVBand' attribute is not declared.", "The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:oddHBand' attribute is not declared.", "The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:oddVBand' attribute is not declared.", "The attribute 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:name' has invalid value 'useWord2013TrackBottomHyphenation'. The Enumeration constraint failed.", "The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:allStyles' attribute is not declared.", "The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:alternateStyleNames' attribute is not declared.", "The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:clearFormatting' attribute is not declared.", "The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:customStyles' attribute is not declared.", "The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:directFormattingOnNumbering' attribute is not declared.", "The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:directFormattingOnParagraphs' attribute is not declared.", "The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:directFormattingOnRuns' attribute is not declared.", "The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:directFormattingOnTables' attribute is not declared.", "The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:headingStyles' attribute is not declared.", "The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:latentStyles' attribute is not declared.", "The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:numberingStyles' attribute is not declared.", "The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:stylesInUse' attribute is not declared.", "The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:tableStyles' attribute is not declared.", "The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:top3HeadingStyles' attribute is not declared.", "The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:visibleStyles' attribute is not declared.", "The 'http://schemas.microsoft.com/office/word/2012/wordml:restartNumberingAfterBreak' attribute is not declared.", }; } }