예제 #1
0
        TemplateSection FindTemplateSection(string sectionName)
        {
            TemplateSection rc = null;

            int i = 0;

            while (rc == null && i < templateSections.Count())
            {
                if (templateSections[i].SectionName == sectionName)
                {
                    rc = templateSections[i];
                }
                i++;
            }

            return(rc);
        }
예제 #2
0
        TemplateSection AddTemplateSection(string sectionName)
        {
            // Check to see if the section already exists - if it does, we add to it
            TemplateSection section = FindTemplateSection(sectionName);

            if (section == null)
            {
                section = new TemplateSection {
                    SectionName     = sectionName,
                    SectionTemplate = ""
                };

                templateSections.Add(section);
            }

            return(section);
        }
예제 #3
0
        public Error LoadTemplate(string templateText, TemplateProperties properties = null)
        {
            var error = new Error();

            int nestCount = 0;

            bool   bAddToPreamble  = true;
            string sectionTemplate = "";

            TemplateSection section = null;

            if (properties != null)
            {
                TemplateProperties = properties;
            }

            List <string> sections = TemplateProperties.GetTemplateSections();

            string[] templateLines = templateText.Replace("\r\n", "\n").Trim().Split('\n');

            // When we read a template, we assume that all templates are consecutive and that there
            // is no html between sections.
            // There can be 'pre-amble' html before the first section and post-amble after the last section.
            foreach (string templateLine in templateLines)
            {
                if (!string.IsNullOrEmpty(templateLine.Trim()))
                {
                    if (section != null)
                    {
                        // Inside a section, so check for end of section
                        sectionTemplate += templateLine + "\r\n";
                        if (templateLine.IndexOf("<tr") != -1)
                        {
                            nestCount++;
                        }
                        if (templateLine.IndexOf("</tr>") != -1)
                        {
                            nestCount--;
                            if (nestCount == 0)
                            {
                                section.SetTemplateText(sectionTemplate);
                                sectionTemplate = "";
                                section         = null;
                            }
                        }
                    }
                    else
                    {
                        // Not in a section, so look for a start of section
                        int foundIdx = -1;
                        for (int i = 0; i < sections.Count() && foundIdx == -1; i++)
                        {
                            if (templateLine.IndexOf(sections[i]) != -1)
                            {
                                foundIdx = i;
                            }
                        }

                        if (foundIdx != -1)
                        {
                            // Found a section start
                            nestCount = 1;
                            string sectionName = sections[foundIdx];

                            section         = AddTemplateSection(sectionName);
                            sectionTemplate = templateLine + "\r\n";
                            bAddToPreamble  = false;
                        }
                        else
                        {
                            // Section start not found, so add to pre/post amble content
                            if (bAddToPreamble)
                            {
                                preAmble += templateLine + "\r\n";
                            }
                            else
                            {
                                postAmble += templateLine + "\r\n";
                            }
                        }
                    }
                }
            }
            return(error);
        }