public static GenerateForm BuilDuplicateJson(GenerateFormNoDupe noDupForm) { if (noDupForm == null || noDupForm.stringMap == null || noDupForm.checkboxMap == null) { return(null); } GenerateForm genForm = new GenerateForm(); foreach (KeyValuePair <string, Pair> entry in noDupForm.stringMap) { if (entry.Value.Count == 0) { genForm.stringMap[entry.Key] = entry.Value.Value; } else { for (int i = 0; i <= entry.Value.Count; i++) { genForm.stringMap[entry.Key + "_" + i] = entry.Value.Value; } } } genForm.checkboxMap = noDupForm.checkboxMap; return(genForm); }
public static void ParseJson(string filepath, GenerateForm genForm, CheckBoxAction checkBoxAction, TextFieldAction textFieldAction) { using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(filepath, true)) { IDictionary <String, BookmarkStart> bookmarkMap = new Dictionary <String, BookmarkStart>(); foreach (BookmarkStart bookmarkStart in wordprocessingDocument.MainDocumentPart.RootElement.Descendants <BookmarkStart>()) { bookmarkMap[bookmarkStart.Name] = bookmarkStart; } foreach (BookmarkStart bookmarkStart in bookmarkMap.Values) { Run bookmarkFieldCode = bookmarkStart.NextSibling <Run>(); if (bookmarkFieldCode != null) { FieldCode fcode = bookmarkFieldCode.GetFirstChild <FieldCode>(); if (fcode != null && FormTypes.isFormType(fcode.Text)) { if (FormTypes.FormCheckBox.Is(fcode.Text)) { Run checkboxRun = bookmarkStart.PreviousSibling <Run>(); FieldChar fieldChar = checkboxRun?.GetFirstChild <FieldChar>(); FormFieldData formFieldData = fieldChar?.GetFirstChild <FormFieldData>(); CheckBox checkbox = formFieldData?.GetFirstChild <CheckBox>(); //Note: docs say Checked should appear however type is DefaultCheckBoxFormFieldState //Checked checkboxChecked = checkbox?.GetFirstChild<Checked>(); DefaultCheckBoxFormFieldState checkboxChecked = checkbox?.GetFirstChild <DefaultCheckBoxFormFieldState>(); if (checkboxChecked != null) { PrintVerbose("" + (bool)checkboxChecked.Val); checkBoxAction(genForm, bookmarkStart.Name, checkboxChecked); } } else if (FormTypes.FormText.Is(fcode.Text)) { while (bookmarkFieldCode.NextSibling <Run>() != null) { Text bookmarkText = bookmarkFieldCode.GetFirstChild <Text>(); if (bookmarkText != null) { PrintVerbose(bookmarkText.Text); textFieldAction(genForm, bookmarkStart.Name, bookmarkText); } bookmarkFieldCode = bookmarkFieldCode.NextSibling <Run>(); } } } } } } }
public static void fillJson(string filepath, GenerateForm genForm) { CheckBoxAction checkBoxFill = (GenerateForm genForm, string key, DefaultCheckBoxFormFieldState checkboxChecked) => { checkboxChecked.Val = genForm.checkboxMap[key]; }; TextFieldAction textFieldFill = (GenerateForm genForm, string key, Text bookmarkText) => { bookmarkText.Text = genForm.stringMap[key]; }; ParseJson(filepath, genForm, checkBoxFill, textFieldFill); }
public static void GenerateJson(string filepath, GenerateForm genForm) { CheckBoxAction checkBoxGen = (GenerateForm genForm, string key, DefaultCheckBoxFormFieldState checkboxChecked) => { genForm.checkboxMap[key] = (bool)checkboxChecked.Val; }; TextFieldAction textFieldGen = (GenerateForm genForm, string key, Text bookmarkText) => { genForm.stringMap[key] = bookmarkText.Text; }; ParseJson(filepath, genForm, checkBoxGen, textFieldGen); }
public static GenerateFormNoDupe BuildNoDuplicateJson(GenerateForm genForm) { GenerateFormNoDupe noDupForm = new GenerateFormNoDupe(); foreach (KeyValuePair <string, string> entry in genForm.stringMap) { string key = entry.Key.Split('_')[0]; Pair valuePair; if (!noDupForm.stringMap.TryGetValue(key, out valuePair)) { noDupForm.stringMap[key] = new Pair(entry.Value, 0); } else { noDupForm.stringMap[key].Count++; } } noDupForm.checkboxMap = genForm.checkboxMap; return(noDupForm); }
static void Main(string[] args) { var verboseOption = new Option <bool>("--verbose"); verboseOption.AddAlias("-v"); var templateOption = new Option <string>("--template"); templateOption.AddAlias("t"); templateOption.IsRequired = true; var jsonOutputOption = new Option <string>("--json", getDefaultValue: () => "output.json"); jsonOutputOption.AddAlias("j"); var jsonInputOption = new Option <string>("--json"); jsonInputOption.AddAlias("j"); jsonInputOption.IsRequired = true; var genCommand = new Command("generate"); genCommand.AddAlias("gen"); genCommand.Add(templateOption); genCommand.Add(jsonOutputOption); genCommand.Add(verboseOption); genCommand.Handler = CommandHandler.Create <string, string, bool>((template, json, verbose) => { isVerbose = verbose; GenerateForm genForm = new GenerateForm(); GenerateJson(template, genForm); WriteJsonToFile(json, BuildNoDuplicateJson(genForm)); }); var fillCommand = new Command("fill"); fillCommand.Add(templateOption); fillCommand.Add(jsonInputOption); fillCommand.Add(verboseOption); fillCommand.Handler = CommandHandler.Create <string, string, bool>((template, json, verbose) => { isVerbose = verbose; if (String.IsNullOrEmpty(json)) { Console.WriteLine("Invalid input json file name."); return; } GenerateForm genForm = BuilDuplicateJson(ReadJsonFromFile <GenerateFormNoDupe>(json)); if (genForm == null || (genForm.stringMap.Count == 0 && genForm.checkboxMap.Count == 0)) { Console.WriteLine("exiting b\\c of invalid json."); return; } fillJson(template, genForm); }); var root = new RootCommand(""); root.Handler = CommandHandler.Create <bool>((verbose) => { isVerbose = verbose; }); root.Add(verboseOption); root.Add(genCommand); root.Add(fillCommand); root.InvokeAsync(args).Wait(); }