public IEnumerable <SharpRow> InsertParentRows(IEnumerable <SharpRow> rows, bool skipFirstParentRow = false) { SharpNodeStack stack = new SharpNodeStack(); foreach (var row in rows) { var nodeRow = row as SharpNodeRow; if (nodeRow == null) { yield return(row); continue; } if (skipFirstParentRow) { stack.Enqueue(nodeRow.Node); skipFirstParentRow = false; yield return(row); continue; } if (stack.Top == nodeRow.Node.Parent) { if (!nodeRow.Node.IsValueNode) { stack.Enqueue(nodeRow.Node); } yield return(row); continue; } while (stack.Top != null && !stack.Top.HasDescendant(nodeRow.Node)) { stack.Dequeue(); } if (stack.Top == null || stack.Top.HasDescendant(nodeRow.Node)) { var nodes = nodeRow.Node.PathTo(stack.Top); foreach (var node in nodes) { stack.Enqueue(node); yield return(new SharpNodeRow(node)); } if (!nodeRow.Node.IsValueNode) { stack.Enqueue(nodeRow.Node); } yield return(row); } } }
private void WriteNodeRow(SharpNodeRow row) { if (row.Node.PackageName.ToLower() != _lastPackage) { WritePackage(row.Node.PackageName); } while (!_nodeStack.IsEmpty && _nodeStack.Top != row.Node && !_nodeStack.Top.HasDescendant(row.Node)) { _nodeStack.Dequeue(); _writer.WriteEndElement(); } if (_nodeStack.Top == row.Node) { _writer.WriteEndElement(); _writer.WriteStartElement(row.Node.Name); } else { if (!_nodeStack.IsEmpty && _nodeStack.Top != row.Node.Parent) { WriteNodeRow(new SharpNodeRow(row.Node.Parent)); } _nodeStack.Enqueue(row.Node); _writer.WriteStartElement(row.Node.Name); } }
public List <SharpNode> PathFrom(SharpNode ancestor) { if (Parent == null) { return(new List <SharpNode>()); } SharpNodeStack stack = new SharpNodeStack(); SharpNode start = Parent; if (IsValueNode) { var nodePath = Path; if (nodePath.EndsWith("/#")) { nodePath = nodePath.Substring(0, nodePath.Length - 2); } if (nodePath == Parent.Path) { start = Parent.Parent; } } if (start == null || ancestor == start || !ancestor.HasDescendant(start)) { return(new List <SharpNode>()); } stack.Enqueue(start); while (stack.Top != ancestor) { if (stack.Top.Parent == null || stack.Top == stack.Top.Parent) { break; } stack.Enqueue(stack.Top.Parent); } return(stack.ToList()); }