/// <summary> /// Method to read a child or a sibling of the node. /// </summary> /// <param name="xmlTextReader">The XML text reader.</param> /// <param name="currentDepth">The current depth on the XML tree.</param> /// <param name="node">The XML node.</param> /// <param name="nodeNameAttribute">The node attribute name.</param> private static void ReadChildOrSibling(XmlTextReader xmlTextReader, int currentDepth, NppXmlNode node, string nodeNameAttribute) { var nodeName = xmlTextReader.Name; if (!string.IsNullOrEmpty(nodeNameAttribute)) { var attr = xmlTextReader.GetAttribute(nodeNameAttribute); if (attr != null) { nodeName = attr; } } node.Name = nodeName; while (xmlTextReader.Read()) { // It's a sibling. if (currentDepth == xmlTextReader.Depth) { if (xmlTextReader.NodeType != XmlNodeType.Element || !xmlTextReader.IsStartElement()) { return; } var sibling = new NppXmlNode(xmlTextReader.Name, new NppXmlNodePosition(xmlTextReader), node.Parent); node.Parent.AddNode(sibling); // Can be a single node. sibling.EndPosition = new NppXmlNodePosition(xmlTextReader, true); ReadChildOrSibling(xmlTextReader, xmlTextReader.Depth, sibling, nodeNameAttribute); sibling.EndPosition = new NppXmlNodePosition(xmlTextReader, true); return; } if (xmlTextReader.NodeType != XmlNodeType.Element || !xmlTextReader.IsStartElement()) { continue; } // It's a child. var child = new NppXmlNode(xmlTextReader.Name, new NppXmlNodePosition(xmlTextReader), node); node.AddNode(child); // Can be a single node. child.EndPosition = new NppXmlNodePosition(xmlTextReader, true); ReadChildOrSibling(xmlTextReader, xmlTextReader.Depth, child, nodeNameAttribute); child.EndPosition = new NppXmlNodePosition(xmlTextReader, true); } }
/// <summary> /// The default constructor for the class. /// </summary> /// <param name="name">The node name.</param> /// <param name="startPosition">The node start position.</param> /// <param name="parent">The parent node.</param> internal NppXmlNode(string name, NppXmlNodePosition startPosition, NppXmlNode parent) { this.Name = name; this.StartPosition = startPosition; this.ChildNodes = new List<NppXmlNode>(); this.Parent = parent; this.Id = nodeId; nodeId++; }
/// <summary> /// The default constructor for the class. /// </summary> /// <param name="name">The node name.</param> /// <param name="startPosition">The node start position.</param> /// <param name="parent">The parent node.</param> private NppXmlNode(string name, NppXmlNodePosition startPosition, NppXmlNode parent) { Name = name; StartPosition = startPosition; Parent = parent; Id = _nodeId; _childNodes = new List <NppXmlNode>(); _nodeId++; }
/// <summary> /// Method to try parse the XML. /// </summary> /// <param name="xml">The XMl as string.</param> /// <param name="nodeNameAttribute"> /// The attribute, that will be used as node name. If no one of that attribute, tag name /// will be used /// </param> /// <param name="logger">The logger.</param> /// <param name="nppXmlNode">The Notepad++ XmlNode.</param> /// <returns>True if parse successfully, false otherwise.</returns> public static bool TryParse(string xml, string nodeNameAttribute, ILogger logger, out NppXmlNode nppXmlNode) { nppXmlNode = null; _nodeId = 1; try { using (var stringReader = new StringReader(xml)) { using (var xmlTextReader = new XmlTextReader(stringReader)) { while (xmlTextReader.Read()) { if (xmlTextReader.NodeType != XmlNodeType.Element || !xmlTextReader.IsStartElement()) { continue; } nppXmlNode = new NppXmlNode(xmlTextReader.Name, new NppXmlNodePosition(xmlTextReader)); ReadChildOrSibling(xmlTextReader, xmlTextReader.Depth, nppXmlNode, nodeNameAttribute); } if (null == nppXmlNode) { throw new ArgumentException("nppXmlNode"); } nppXmlNode.EndPosition = new NppXmlNodePosition(xmlTextReader, true); return(true); } } } catch (Exception exception) { logger.Warning(exception, exception.Message); nppXmlNode = null; return(false); } }
/// <summary> /// Background worker method to do the user interface updated. /// </summary> /// <param name="sender">The background worker object.</param> /// <param name="e">The event arguments.</param> private void BackgroundWorker_UpdateUserInterfaceDoWork(object sender, DoWorkEventArgs e) { this._rootNode = null; if (!this.ButtonToggle.InvokeRequired) { this.ButtonToggle.Invoke(new EnableToggleButtonDelegate(EnableToggleButton), false); } else { EnableToggleButton(false); } if (this.treeView.InvokeRequired) { this.treeView.Invoke(new SetTreeviewVisibilityDelegate(SetTreeviewVisibility), false); } else { SetTreeviewVisibility(false); } if (this.LabelStatus.InvokeRequired) { this.LabelStatus.Invoke(new SetStatusLabelTextDelegate(SetStatusLabelText), "Parsing the document"); } else { SetStatusLabelText("Parsing the document"); } // Do validations. if (!NppXmlNode.TryParse(GetDocumentText(PluginBase.GetCurrentScintilla()), out this._rootNode)) { if (this.LabelStatus.InvokeRequired) { this.LabelStatus.Invoke(new SetStatusLabelTextDelegate(SetStatusLabelText), "Document is not valid."); } else { SetStatusLabelText("Document is not valid."); } this._workerIsRunning = false; return; } if (this.treeView.InvokeRequired) { this.treeView.Invoke(new ClearNodesDelegate(ClearNodes)); } else { ClearNodes(); } if (this.treeView.InvokeRequired) { this.treeView.Invoke(new AddNodeToTreeViewDelegate(AddNodeToTreeView), GenerateTreeNode(this._rootNode)); } else { this.treeView.Nodes.Add(GenerateTreeNode(this._rootNode)); } // Add the rest of the nodes. var treeNode = this.treeView.Nodes[0]; AddNode(this._rootNode, treeNode); // Finish up the operation. if (this.treeView.InvokeRequired) { this.treeView.Invoke(new ExpandTreeViewDelegate(ExpandTreeView)); } else { ExpandTreeView(); } if (this.LabelStatus.InvokeRequired) { this.LabelStatus.Invoke(new SetStatusLabelTextDelegate(SetStatusLabelText), string.Empty); } else { SetStatusLabelText(string.Empty); } if (!this.ButtonToggle.InvokeRequired) { this.ButtonToggle.Invoke(new EnableToggleButtonDelegate(EnableToggleButton), true); } else { EnableToggleButton(true); } this._workerIsRunning = false; }
/// <summary> /// Method to add a npp xml node to a tree node. /// </summary> /// <param name="inXmlNode">The npp xml node.</param> /// <param name="inTreeNode">The tree node.</param> private void AddNode(NppXmlNode inXmlNode, TreeNode inTreeNode) { if (!inXmlNode.HasChildNodes) { return; } for (var i = 0; i < inXmlNode.ChildNodes.Count; i++) { var xNode = inXmlNode.ChildNodes[i]; if (this.treeView.InvokeRequired) { this.treeView.Invoke(new AddNodeToNodeDelegate(AddNodeToNode), GenerateTreeNode(xNode), inTreeNode); } else { AddNodeToNode(GenerateTreeNode(xNode), inTreeNode); } AddNode(xNode, inTreeNode.Nodes[i]); } }
/// <summary> /// Method to generate a treenode from a npp tree node. /// </summary> /// <param name="node">The npp tree node.</param> /// <returns>The tree node.</returns> private static TreeNode GenerateTreeNode(NppXmlNode node) { return new TreeNode { ToolTipText = $"{node.Name}", Tag = new TextBoundary(node.StartPosition, node.EndPosition, node.Id), Text = node.Name, // The name is the key. Name = node.Id.ToString() }; }
private void AddNode(NppXmlNode node) { _childNodes.Add(node); }
/// <summary> /// Method to try parse the XML. /// </summary> /// <param name="xml">The XMl as string.</param> /// <param name="logger">The logger.</param> /// <param name="nppXmlNode">The Notepad++ XmlNode.</param> /// <returns>True if parse successfully, false otherwise.</returns> public static bool TryParse(string xml, ILogger logger, out NppXmlNode nppXmlNode) { return(TryParse(xml, null, logger, out nppXmlNode)); }
/// <summary> /// Method to read a child or a sibling of the node. /// </summary> /// <param name="xmlTextReader">The XML text reader.</param> /// <param name="currentDepth">The current depth on the XML tree.</param> /// <param name="node">The XML node</param> private static void ReadChildOrSibling(XmlTextReader xmlTextReader, int currentDepth, NppXmlNode node) { while (xmlTextReader.Read()) { // It's a sibling. if (currentDepth == xmlTextReader.Depth) { if (xmlTextReader.NodeType != XmlNodeType.Element || !xmlTextReader.IsStartElement()) { return; } var sibling = new NppXmlNode(xmlTextReader.Name, new NppXmlNodePosition(xmlTextReader), node.Parent); node.Parent.ChildNodes.Add(sibling); // Can be a single node. sibling.EndPosition = new NppXmlNodePosition(xmlTextReader, true); ReadChildOrSibling(xmlTextReader, xmlTextReader.Depth, sibling); sibling.EndPosition = new NppXmlNodePosition(xmlTextReader, true); return; } if (xmlTextReader.NodeType != XmlNodeType.Element || !xmlTextReader.IsStartElement()) { continue; } // It's a child. var child = new NppXmlNode(xmlTextReader.Name, new NppXmlNodePosition(xmlTextReader), node); node.ChildNodes.Add(child); // Can be a single node. child.EndPosition = new NppXmlNodePosition(xmlTextReader, true); ReadChildOrSibling(xmlTextReader, xmlTextReader.Depth, child); child.EndPosition = new NppXmlNodePosition(xmlTextReader, true); } }
/// <summary> /// Method to try parse the XML. /// </summary> /// <param name="xml">The XMl as string.</param> /// <param name="nppXmlNode">The Notepad++ XmlNode.</param> /// <returns>True if parse successfully, false otherwise.</returns> public static bool TryParse(string xml, out NppXmlNode nppXmlNode) { nppXmlNode = null; nodeId = 1; try { using (var stringReader = new StringReader(xml)) { using (var xmlTextReader = new XmlTextReader(stringReader)) { while (xmlTextReader.Read()) { if (xmlTextReader.NodeType != XmlNodeType.Element || !xmlTextReader.IsStartElement()) { continue; } nppXmlNode = new NppXmlNode(xmlTextReader.Name, new NppXmlNodePosition(xmlTextReader)); ReadChildOrSibling(xmlTextReader, xmlTextReader.Depth, nppXmlNode); } if (null == nppXmlNode) { throw new ArgumentException("nppXmlNode"); } nppXmlNode.EndPosition = new NppXmlNodePosition(xmlTextReader, true); return true; } } } catch { nppXmlNode = null; return false; } }