private static INamingConvention GetNamingConvention(NamingConvention namingConvention) { INamingConvention target; switch (namingConvention) { case NamingConvention.Camel: target = new CamelCaseNamingConvention(); break; case NamingConvention.Hyphenated: target = new HyphenatedNamingConvention(); break; case NamingConvention.Pascal: target = new PascalCaseNamingConvention(); break; case NamingConvention.Underscored: target = new UnderscoredNamingConvention(); break; case NamingConvention.Null: target = new NullNamingConvention(); break; default: throw new NotImplementedException(namingConvention.ToString()); } return(target); }
static void Main(string[] args) { Microsoft.Office.Interop.Word.Application w = null; if (args.Length != 4) { Usage(); return; } /* Retrieve arguments */ var template = args[0]; var outFile = args[1]; var baseDirectory = args[2]; var list = args[3]; try { /* Create Word instance */ w = new Microsoft.Office.Interop.Word.Application(); /* Check files existence */ if (!System.IO.File.Exists(template)) { throw new System.IO.FileNotFoundException( "Template is not found: " + template); } if (!System.IO.File.Exists(list)) { throw new System.IO.FileNotFoundException( "File list is not found: " + list); } /* Read file list (Configuration) */ Console.WriteLine("Read file list '" + list + "'"); var convention = new UnderscoredNamingConvention(); var deserializer = new DeserializerBuilder() .WithNamingConvention(convention) .Build(); /* * Read base directory from configuration. If it is not * available, use the one from parameters */ var cfg = deserializer.Deserialize <Configuration>( System.IO.File.ReadAllText(list)); if (cfg.BaseDirectory == "") { cfg.BaseDirectory = baseDirectory; } if (!System.IO.Directory.Exists(cfg.BaseDirectory)) { throw new System.IO.DirectoryNotFoundException( "Base directory is not found: '" + cfg.BaseDirectory + "'"); } /* Reduce the load on Word */ w.Options.CheckGrammarWithSpelling = false; w.Options.CheckSpellingAsYouType = false; w.Options.CheckGrammarAsYouType = false; w.Options.AllowFastSave = true; w.Documents.Open(System.IO.Path.GetFullPath(template)); var doc = w.ActiveDocument; var range = doc.Range(); if (range.Find.Execute("<Insert>")) { w.Selection.SetRange(range.Start, range.End); foreach (var group in cfg.Groups) { processGroup(cfg, group, w.Selection); /* * Clearing undo cache is FAIRLY important to let Word * cope with very large files */ doc.UndoClear(); } } else { throw new Exception("<Insert> is not found"); } Console.WriteLine("Updating ToC..."); for (int i = 0; i < doc.TablesOfContents.Count; ++i) { doc.TablesOfContents[1 + i].Update(); } Console.WriteLine("Updating Page Count..."); range = doc.Range(); if (range.Find.Execute("<ListCount>")) { range.Text = doc.ComputeStatistics( WdStatistic.wdStatisticPages).ToString(); range.Select(); } Console.WriteLine("Writing to '" + System.IO.Path.GetFullPath(outFile) + "'..."); w.ActiveDocument.SaveAs2(System.IO.Path.GetFullPath(outFile)); Console.WriteLine("Done"); } catch (Exception ex) { Console.WriteLine("Error: " + ex.ToString()); } finally { if (w != null && w.Documents.Count != 0) { w.ActiveDocument.Close(false); } } }
public void TestUnderscored(string expected, string input) { var sut = new UnderscoredNamingConvention(); Assert.Equal(expected, sut.Apply(input)); }