/// <summary> /// Tags the list. /// </summary> /// <param name="source">The source.</param> /// <returns></returns> public static MatchTagCollection TagList(string source) { MatchTagCollection list = new MatchTagCollection(); for (int i = 0; i < source.Length; i++) { if (source[i] == '<') { int length = GetTagLength(source, i); // check if tag is too short or comment // min lenght for a tag is 3 <x> // comments start <!-- if (length >= 3 && (source[i + 1] == '/' || source[i + 1] == '!' || (source[i + 1] >= 'A' && source[i + 1] <= 'Z') || (source[i + 1] >= 'a' && source[i + 1] <= 'z'))) { MatchTag tag = new MatchTag(source, i, length); list.Add(tag); i = i + length - 1; } } } return list; }
/// <summary> /// Tags the list. /// </summary> /// <param name="source">The source.</param> /// <returns></returns> public static MatchTagCollection TagList(string source) { MatchTagCollection list = new MatchTagCollection(); for (int i = 0; i < source.Length; i++) { if (source[i] == '<') { int length = GetTagLength(source, i); // check if tag is too short or comment // min lenght for a tag is 3 <x> // comments start <!-- if (length >= 3 && (source[i + 1] == '/' || source[i + 1] == '!' || (source[i + 1] >= 'A' && source[i + 1] <= 'Z') || (source[i + 1] >= 'a' && source[i + 1] <= 'z'))) { MatchTag tag = new MatchTag(source, i, length); list.Add(tag); i = i + length - 1; } } } return(list); }
/// <summary> /// Builds the profile. /// </summary> /// <param name="source">The source.</param> /// <returns>the profile</returns> private Profile BuildProfile(string source) { Profile build = new Profile(); if (source == null || source.Length == 0) { return(build); } MatchTagCollection tags = HtmlString.TagList(source); build.tags = new MatchTagCollection(); build.tagMap = string.Empty; for (int i = 0; i < tags.Count; i++) { MatchTag tag = tags[i]; char tagStart = char.ToUpper(tag.TagName[0]); // Test if special tag -- used for regex searchs // these tags are only copied into the tagMap no position index is stored if (tagStart == 'Z') { build.tagMap += tag.TagName.Substring(1); } if (_sectionTemplate.Tags.IndexOf(tagStart) != -1 && tag.TagName != "br") { if (tagStart == 'T') { if (char.ToUpper(tag.TagName[1]) != 'A') { tagStart = char.ToUpper(tag.TagName[1]); } } if (tag.IsClose) { build.tagMap += tagStart; } else { build.tagMap += char.ToLower(tagStart); } build.tags.Add(tag); } } return(build); }
/// <summary> /// Strips the unwanted HTML tags. /// </summary> /// <param name="source">The source.</param> /// <returns>stripped source</returns> private string StripTags(string source) { string stripped = string.Empty; source = HtmlString.NewLines(source); MatchTagCollection tags = HtmlString.TagList(source); for (int i = 0; i < tags.Count; i++) { MatchTag tag = tags[i]; if (_template.Tags.IndexOf(char.ToUpper(tag.TagName[0])) != -1) { stripped += source.Substring(tag.Index, tag.Length); } int start = tag.Index + tag.Length; if (start < source.Length) { int end; if (i + 1 < tags.Count) { tag = tags[i + 1]; end = tag.Index; } else { end = source.Length; } stripped += source.Substring(start, end - start); } } return(stripped); }
/// <summary> /// Gets the sections. /// </summary> /// <param name="source">The source.</param> /// <returns></returns> private Sections GetSections(string source) { source = StripTags(source); Sections data = new Sections(); data.dataFields = new ArrayList(); data.minFields = 0; data.optionalData = false; data.dataTags = 0; MatchTagCollection tags = HtmlString.TagList(source); bool isOptionalTag = false; bool zTag = false; for (int i = 0; i < tags.Count; i++) { MatchTag tag = tags[i]; // check if tag + data is optional / regex zTag = false; if (char.ToUpper(tag.TagName[0]) == 'Z') { zTag = true; isOptionalTag = true; if (tag.IsClose) { isOptionalTag = false; } //i++; //if (i < tags.Count) // tag = tags[i]; //else // break; } // Check if tag is one of interest if (_template.Tags.IndexOf(char.ToUpper(tag.TagName[0])) != -1) { DataField section; if (!zTag) { // Add tag to array of fields section = new DataField(); section.optional = isOptionalTag; section.htmlTag = tag; section.hasData = false; section.source = tag.FullTag; if (section.source.IndexOf("<#") != -1 || section.source.IndexOf("<*") != -1) { section.hasData = true; if (isOptionalTag) { data.optionalData = true; } section.dataElements = GetElements(section.source); data.dataTags += section.dataElements.Count; } data.dataFields.Add(section); if (!isOptionalTag) { data.minFields++; } } // Add data between this tag and the next to field array int start = tag.Index + tag.Length; int end; if (i + 1 < tags.Count) { tag = tags[i + 1]; zTag = false; if (char.ToUpper(tag.TagName[0]) == 'Z') { zTag = true; isOptionalTag = true; if (tag.IsClose) { isOptionalTag = false; } } // start = tag.Index + tag.Length; // i++; // if (i + 1 < tags.Count) // tag = tags[i + 1]; // else // break; //} end = tag.Index; } else { end = source.Length; } if (!zTag) { section = new DataField(); section.optional = isOptionalTag; section.htmlTag = null; section.source = HtmlString.Decode(source.Substring(start, end - start)); section.hasData = false; if (section.source.IndexOf("<#") != -1 || section.source.IndexOf("<*") != -1) { section.hasData = true; if (isOptionalTag) { data.optionalData = true; } section.dataElements = GetElements(section.source); data.dataTags += section.dataElements.Count; } data.dataFields.Add(section); if (!isOptionalTag) { data.minFields++; } } } } return(data); }