public override void AddTag(Tag child) { var childTagType = child.GetTagType(); if (childTagType == TagType.head || childTagType == TagType.body) { Children.Add(child); child.SetFather(this); } else { throw new Exception("Invalid tag: " + childTagType + " with: " + Type); } }
public override void AddTag(Tag child) { var childTagType = child.GetTagType(); if (childTagType != TagType.head && childTagType != TagType.body && childTagType != TagType.html) { Children.Add(child); child.SetFather(this); } else { throw new Exception("Invalid tag "); } }
public override void AddTag(Tag child) { var childTagType = child.GetTagType(); try { if (childTagType != TagType.head && childTagType != TagType.body && childTagType != TagType.html && childTagType != TagType.title) { Children.Add(child); child.SetFather(this); } } catch (Exception) { throw new Exception("Invalid tag "); } }
public static void ParseFile(string FileLocation) { string text = System.IO.File.ReadAllText(FileLocation); text = Regex.Replace(text, @"\t|\n|\r", ""); string TagName = ""; for (int index = 0; index < text.Length; index++) { int tempIndex = index + 1; //Obtain name of tag if (text[index] == '<' && text[index + 1] != '/') { while (text[tempIndex] != '>' && (text[tempIndex] != ' ' || text[tempIndex + 1] == ' ') && text[tempIndex] != '/') { TagName += text[tempIndex]; tempIndex++; } index = tempIndex; if (text[index] == ' ') { TagName = TagName.Trim(); index++; } if (Root == null) { //html tag Root = CreateTag(TagName); CurrentTag = Root; } else if (text[index] != '/') { //normal tag ParseTag(TagName); } else { //self-closing tag CurrentTag.AddTag(CreateTag(TagName)); index = text.IndexOf('>', index + 1); } if (text[index - 1] == ' ' && text[index] != '>') { var attributes = new StringBuilder(); while (text[index] != '>') { attributes.Append(text[index]); index++; } attributes.Replace("=\"", "*"); attributes.Replace(" ", ""); attributes.Length--; var attribute = attributes.ToString().Split('"'); foreach (var tempAtt in attribute) { var keyValue = tempAtt.Split('*'); CurrentTag.AddAttribute(keyValue[0], keyValue[1]); } } } else if (text[index] == '<' && text[index + 1] == '/') { index += CurrentTag.GetTagType().ToString().Length + 2; CurrentTag = CurrentTag.GetFather(); if (ParrentTag != null) { ParrentTag = ParrentTag.GetFather(); } } else { string content = ""; tempIndex = index; while (text[tempIndex] != '<') { content += text[tempIndex]; tempIndex++; } index = tempIndex - 1; Element temp = new Element(); temp.SetContent(content); CurrentTag.AddElement(temp); } TagName = ""; } Console.WriteLine("Render of the Tree:"); Console.WriteLine(Root.CreateString(0)); Root.PrintTag(); }