private static bool VerifyXmlEncoding(Context context, string encoding) { var encodings = Encoding.GetEncodings(); switch (encoding.ToLowerInvariant()) { case "utf8": case "utf-8": context.XmlEncoding = Encoding.UTF8; break; default: var match = encodings.FirstOrDefault(x => x.Name.Equals(encoding, StringComparison.OrdinalIgnoreCase)); if (match != null) { context.XmlEncoding = Encoding.GetEncoding(match.CodePage); } else { context.ExitCode = 102; context.AddError("Invalid encoding '" + encoding + "'."); } break; } return(CheckErrors(context)); }
private static bool VerifyWordTemplate(Context context, string filePath) { if (!File.Exists(filePath)) { context.AddError("File does not exist '" + filePath + "'"); context.ExitCode = 101; } return(CheckErrors(context)); }
private static bool VerifyFile(Context context, string filePath) { if (!File.Exists(filePath)) { context.AddError("File does not exist '" + filePath + "'"); context.ExitCode = 100; } context.Title = Path.GetFileNameWithoutExtension(filePath); return(CheckErrors(context)); }
private static List <ItemPath> ParseXmlArguments(Context context, string[] itemsPath) { var paths = new List <ItemPath>(itemsPath.Length); var chars = new char[] { ':', ';', }; foreach (var spec in itemsPath) { var path = new ItemPath(); if (spec.Count(c => c.Equals(':')) >= 3) { var parts = spec.Split(new char[] { ':', }, 4); ItemStyle style; if (Enum.TryParse <ItemStyle>(parts[0], out style)) { path.Style = style; } else { context.AddError("Invalid style in '" + spec + "'."); } path.Path = parts[1]; path.Attribute = string.IsNullOrWhiteSpace(parts[2]) ? null : parts[2]; path.Text = string.IsNullOrEmpty(parts[3]) ? null : parts[3]; } else { context.AddError("Not enough parameters in '" + spec + "'."); continue; } paths.Add(path); } return(paths); }
private static bool ParseXml(Context context, string rootPath, string[] itemsPath) { var collection = context.Xml.Root.XPathSelectElements(rootPath); if (collection == null) { context.AddError("Cound not find root element from path '" + rootPath + "'"); context.ExitCode = 105; return(false); } context.ExitCode = 106; List <ItemPath> paths; try { paths = ParseXmlArguments(context, itemsPath); } catch (Exception ex) { context.AddError("An error occured while parsing the arguments.", ex.Message); context.ExitCode = 108; return(CheckErrors(context)); } try { ReadXmlAndWriteWord(context, collection, paths); } catch (Exception ex) { context.AddError("An error occured while generating the document content.", ex.Message); context.ExitCode = 109; } return(CheckErrors(context)); }
private static bool CommitWordFile(Context context, string filePath) { context.WordPart.Document.Save(); context.Word.Close(); try { var bytes = context.WordStream.ToArray(); File.WriteAllBytes(filePath, bytes); } catch (Exception ex) { context.ExitCode = 107; context.AddError("Failed to save word document.", ex.Message); } return(CheckErrors(context)); }
private static bool CommitWordFile(Context context, string filePath) { context.WordPart.Document.Save(); context.Word.Close(); try { var bytes = context.WordStream.ToArray(); File.WriteAllBytes(filePath, bytes); } catch (Exception ex) { context.ExitCode = 107; context.AddError("Failed to save word document.", ex.Message); } return CheckErrors(context); }
private static bool PrepareWordFile(Context context, string filePath, string templateFilePath) { context.WordStream = new MemoryStream(); try { var templateBytes = File.ReadAllBytes(templateFilePath); context.WordStream.Write(templateBytes, 0, templateBytes.Length); context.WordStream.Seek(0L, SeekOrigin.Begin); context.Word = WordprocessingDocument.Open(context.WordStream, true); context.Word.ChangeDocumentType(WordprocessingDocumentType.Document); ////var mainPart = context.WordPart = context.Word.AddMainDocumentPart(); var mainPart = context.WordPart = context.Word.MainDocumentPart; var settings = mainPart.DocumentSettingsPart; ////var templateRelationship = new AttachedTemplate { Id = "relationId1" }; ////settings.Settings.Append(templateRelationship); var templateUri = new Uri(templateFilePath, UriKind.Absolute); mainPart.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate", templateUri, "relationId1"); context.WordPart.Document = new Document(); var body = context.WordPart.Document.AppendChild(new Body()); context.WordBody = body; var h1Properties = new ParagraphProperties(); h1Properties.Append(new ParagraphStyleId() { Val = "Heading1", }); var h1 = body.AppendChild(new Paragraph()); h1.Append(h1Properties); h1.AppendChild(new Run(new Text(context.Title ?? "Title 1"))); //body.AppendChild(h1); body.AppendChild(new Paragraph(new Run(new Text("Document generated on " + DateTime.Now.ToString("o") + ". ")))); } catch (Exception ex) { context.AddError("Failed to create in-memory Word document.", ex.Message); context.ExitCode = 104; } return(CheckErrors(context)); }
private static bool ReadXmlFile(Context context, string filePath) { try { using (var file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { var reader = new StreamReader(file, context.XmlEncoding); var doc = XDocument.Load(reader); context.Xml = doc; } } catch (Exception ex) { context.ExitCode = 103; context.AddError("Failed to read XML file", ex.Message); } return(CheckErrors(context)); }
private static bool VerifyWordTemplate(Context context, string filePath) { if (!File.Exists(filePath)) { context.AddError("File does not exist '" + filePath + "'"); context.ExitCode = 101; } return CheckErrors(context); }
private static bool VerifyXmlEncoding(Context context, string encoding) { var encodings = Encoding.GetEncodings(); switch (encoding.ToLowerInvariant()) { case "utf8": case "utf-8": context.XmlEncoding = Encoding.UTF8; break; default: var match = encodings.FirstOrDefault(x => x.Name.Equals(encoding, StringComparison.OrdinalIgnoreCase)); if (match != null) { context.XmlEncoding = Encoding.GetEncoding(match.CodePage); } else { context.ExitCode = 102; context.AddError("Invalid encoding '" + encoding + "'."); } break; } return CheckErrors(context); }
private static bool VerifyFile(Context context, string filePath) { if (!File.Exists(filePath)) { context.AddError("File does not exist '" + filePath + "'"); context.ExitCode = 100; } context.Title = Path.GetFileNameWithoutExtension(filePath); return CheckErrors(context); }
private static bool ReadXmlFile(Context context, string filePath) { try { using (var file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { var reader = new StreamReader(file, context.XmlEncoding); var doc = XDocument.Load(reader); context.Xml = doc; } } catch (Exception ex) { context.ExitCode = 103; context.AddError("Failed to read XML file", ex.Message); } return CheckErrors(context); }
private static bool PrepareWordFile(Context context, string filePath, string templateFilePath) { context.WordStream = new MemoryStream(); try { var templateBytes = File.ReadAllBytes(templateFilePath); context.WordStream.Write(templateBytes, 0, templateBytes.Length); context.WordStream.Seek(0L, SeekOrigin.Begin); context.Word = WordprocessingDocument.Open(context.WordStream, true); context.Word.ChangeDocumentType(WordprocessingDocumentType.Document); ////var mainPart = context.WordPart = context.Word.AddMainDocumentPart(); var mainPart = context.WordPart = context.Word.MainDocumentPart; var settings = mainPart.DocumentSettingsPart; ////var templateRelationship = new AttachedTemplate { Id = "relationId1" }; ////settings.Settings.Append(templateRelationship); var templateUri = new Uri(templateFilePath, UriKind.Absolute); mainPart.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate", templateUri, "relationId1"); context.WordPart.Document = new Document(); var body = context.WordPart.Document.AppendChild(new Body()); context.WordBody = body; var h1Properties = new ParagraphProperties(); h1Properties.Append(new ParagraphStyleId() { Val = "Heading1", }); var h1 = body.AppendChild(new Paragraph()); h1.Append(h1Properties); h1.AppendChild(new Run(new Text(context.Title ?? "Title 1"))); //body.AppendChild(h1); body.AppendChild(new Paragraph(new Run(new Text("Document generated on " + DateTime.Now.ToString("o") + ". ")))); } catch (Exception ex) { context.AddError("Failed to create in-memory Word document.", ex.Message); context.ExitCode = 104; } return CheckErrors(context); }
private static List<ItemPath> ParseXmlArguments(Context context, string[] itemsPath) { var paths = new List<ItemPath>(itemsPath.Length); var chars = new char[] { ':', ';', }; foreach (var spec in itemsPath) { var path = new ItemPath(); if (spec.Count(c => c.Equals(':')) >= 3) { var parts = spec.Split(new char[] { ':', }, 4); ItemStyle style; if (Enum.TryParse<ItemStyle>(parts[0], out style)) { path.Style = style; } else { context.AddError("Invalid style in '" + spec + "'."); } path.Path = parts[1]; path.Attribute = string.IsNullOrWhiteSpace(parts[2]) ? null : parts[2]; path.Text = string.IsNullOrEmpty(parts[3]) ? null : parts[3]; } else { context.AddError("Not enough parameters in '" + spec + "'."); continue; } paths.Add(path); } return paths; }
private static bool ParseXml(Context context, string rootPath, string[] itemsPath) { var collection = context.Xml.Root.XPathSelectElements(rootPath); if (collection == null) { context.AddError("Cound not find root element from path '" + rootPath + "'"); context.ExitCode = 105; return false; } context.ExitCode = 106; List<ItemPath> paths; try { paths = ParseXmlArguments(context, itemsPath); } catch (Exception ex) { context.AddError("An error occured while parsing the arguments.", ex.Message); context.ExitCode = 108; return CheckErrors(context); } try { ReadXmlAndWriteWord(context, collection, paths); } catch (Exception ex) { context.AddError("An error occured while generating the document content.", ex.Message); context.ExitCode = 109; } return CheckErrors(context); }