Пример #1
0
        public static Dictionary <string, TemplateFileItem> ParseFile(string fileName)
        {
            Dictionary <string, TemplateFileItem> itemList = new Dictionary <string, TemplateFileItem>();

            if (!File.Exists(fileName))
            {
                throw new Exception("Input file does not exist");
            }

            FileStream   fs       = File.Open(fileName, FileMode.Open);
            StreamReader sr       = new StreamReader(fs);
            Regex        rMatcher = new Regex(@"@\{[\w]+\}");

            while (!sr.EndOfStream)
            {
                string          jsLine = sr.ReadLine();
                MatchCollection mc     = rMatcher.Matches(jsLine);

                foreach (Match m in mc)
                {
                    if (itemList.ContainsKey(m.Value))
                    {
                        TemplateFileItem tfi = itemList[m.Value];
                        tfi.FilePositionList.Add(m.Index);
                    }
                    else
                    {
                        itemList.Add(m.Value, new TemplateFileItem(m.Value, m.Index));
                    }
                }
            }
            fs.Close();

            return(itemList);
        }
Пример #2
0
        private void btnGenerate_Click(object sender, EventArgs e)
        {
            Dictionary <string, TemplateFileItem> replaceDictionary = new Dictionary <string, TemplateFileItem>();

            foreach (Control ctrl in pnlReplacements.Controls)
            {
                Panel itemPanel = ctrl as Panel;
                if (itemPanel != null)
                {
                    TemplateFileItem tfi = itemPanel.Tag as TemplateFileItem;
                    if (tfi != null)
                    {
                        foreach (Control innerControl in itemPanel.Controls)
                        {
                            if (innerControl is CheckBox)
                            {
                                tfi.Enabled = (innerControl as CheckBox).Checked;
                            }
                            else if (innerControl is TextBox && innerControl.Tag is string && (innerControl.Tag as string) == "ReplaceText")
                            {
                                tfi.Replacement = (innerControl as TextBox).Text;
                            }
                        }
                        replaceDictionary.Add(tfi.Variable, tfi);
                    }
                }
            }
            try
            {
                TemplateFileParser.GenerateOutput(txtInputFile.Text, txtOutputFile.Text, replaceDictionary);
                MessageBox.Show("File successfully generated!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }