public Tree(T value, params Tree <T>[] childern) { foreach (Tree <T> child in childern) { root.AddChild(child.root); } }
private XmpNode NewNode(string ns, string name) { Dictionary <string, XmpNode> ns_nodes = null; if (!nodes.ContainsKey(ns)) { ns_nodes = new Dictionary <string, XmpNode>(); nodes.Add(ns, ns_nodes); } else { ns_nodes = nodes[ns]; } if (ns_nodes.ContainsKey(name)) { foreach (XmpNode child_node in NodeTree.Children) { if (child_node.Namespace == ns && child_node.Name == name) { NodeTree.RemoveChild(child_node); break; } } ns_nodes.Remove(name); } XmpNode node = new XmpNode(ns, name); ns_nodes.Add(name, node); NodeTree.AddChild(node); return(node); }
public NodeTree Parse(string input) { var nodeTree = new NodeTree(securitySubstitutions, aliasSubstitutions); var treeDepth = 0; if (string.IsNullOrWhiteSpace(input)) { nodeTree.AddChild(new TextNode(string.Empty)); return(nodeTree); } var reader = new Reader(input, tags); var current = (Node)nodeTree; if (!reader.TryRead(out var tagResult)) { current.AddChild(new TextNode(input)); } else { do { if (!string.IsNullOrEmpty(tagResult.Text)) { current.AddChild(new TextNode(tagResult.Text)); } // no parsing inside CodeTag var isInsideCodeTag = (current as TagNode)?.Tag is CodeTag; var resultIsClosingCodeTag = tagResult.Tag is CodeTag && tagResult.TagType == TagType.Close; if (isInsideCodeTag && !resultIsClosingCodeTag && !string.IsNullOrEmpty(tagResult.Match) && tagResult.Tag != null) { current.AddChild(new TextNode(tagResult.Match)); continue; } switch (tagResult.TagType) { case TagType.NoResult: continue; case TagType.Open: var tagNode = new TagNode(tagResult.Tag, current, tagResult.AttributeValue); current.AddChild(tagNode); if (!tagResult.Tag.RequiresClosing) { continue; } current = tagNode; treeDepth++; if (treeDepth > TreeMaxDepth) { throw new BbParserException(); } break; default: if (tagResult.TagType == TagType.Close && current != nodeTree) { var currentTagNode = (TagNode)current; if (currentTagNode.Tag.Name != tagResult.Tag.Name) { do { current = current.ParentNode; treeDepth--; } while (current != nodeTree && currentTagNode.Tag.Name != tagResult.Tag.Name); } else { current = current.ParentNode; treeDepth--; } } break; } } while (reader.TryRead(out tagResult)); } return(nodeTree); }