public bool TryCollapseLeafNode(SharpNodeRow row) { List <SharpValueRow> columns = new List <SharpValueRow>(); SharpNodeRow prev = row; while (prev is SharpValueRow && prev.Node.IsSingleValueNode) { columns.Insert(0, prev as SharpValueRow); prev = prev.Prev; } if (columns.Count > 2) { string mapPath = string.Join(",", columns.Select(r => r.Node.Index)); var mapNode = _nodes.GetNode(mapPath) as SharpNodeMap; if (mapNode == null) { mapNode = new SharpNodeMap(columns); mapNode.Path = mapPath; mapNode.IsValueNode = true; mapNode.MapType = SharpMapType.Sequence; _nodes.Add(mapNode); } var mapRow = new SharpValueRow(mapNode, columns.Select(c => c.Values[0])); mapRow.Collection = this; mapRow.Next = row.Next; mapRow.Root = row.Root; if (prev != null) { mapRow.Prev = prev; prev.Next = mapRow; } else if (mapRow.Root != null) { mapRow.Root.First = mapRow; } return(true); } return(false); }
private void WriteValueRow(SharpValueRow row) { if (row.Node.IsSingleValueNode) { if (row.Node.PackageName.ToLower() != _lastPackage) { WritePackage(row.Node.PackageName); } while (!_nodeStack.IsEmpty && _nodeStack.Top != row.Node.Parent) { _nodeStack.Dequeue(); _writer.WriteEndElement(); } if (_nodeStack.IsEmpty) { WriteNodeRow(new SharpNodeRow(row.Node.Parent)); } if (row.Node.IsAttributeNode) { _writer.WriteAttributeString(row.Node.Name.TrimStart('@'), row.Values[0].ToString()); } else if (row.Node.IsExpandedValueNode) { _writer.WriteValue(row.Values[0] ?? ""); } else { var val = row.Values[0] == null ? "" : row.Values[0].ToString(); _writer.WriteElementString(row.Node.Name, val); } } else { foreach (var innerRow in row.Node.GetRows(row.Values)) { WriteRow(innerRow); } } }
private void WriteFixedRow(SharpValueRow row, SharpNodeMap mapNode) { var columns = new Dictionary <string, string>(); var rowMap = row.Node as SharpNodeMap; var mapType = rowMap.MapType; List <SharpValue> rowValues = new List <SharpValue>(); if (mapType == SharpMapType.Fixed) { rowValues = rowMap.ExpandRow((string)row.Values[0]).ToList(); } else if (mapType == SharpMapType.Sequence) { rowValues = rowMap.GetRowValues(row.Values).ToList(); } else if (mapType == SharpMapType.Variable) { if (row.Values.Count == 1 && row.Values[0] is string) { rowValues = rowMap.ExpandRow((string)row.Values[0]).ToList(); } else { rowValues = rowMap.GetRowValues((row.Values)).ToList(); } } int i = 0; foreach (var column in rowMap.Columns) { columns[column.Node.Name] = SharpValue.ToString(rowValues[i].Value, rowValues[i].Type); i++; } WriteLine(columns, mapNode); }
private void WriteValueRow(SharpValueRow row, SharpMapType mapType = SharpMapType.None, char delimiter = '\0') { if (row.Node.IsSingleValueNode) { return; } var mapNode = row.Node as SharpNodeMap; if (mapNode != null) { mapType = mapType == SharpMapType.None ? mapNode.MapType : mapType; delimiter = delimiter == '\0' ? mapNode.Delimiter : delimiter; if (mapType == SharpMapType.Fixed && row.Values.Count > 1) { mapType = SharpMapType.Sequence; } if (mapType == SharpMapType.Fixed) { _writer.WriteLine(row.Values[0]); } else if (mapType == SharpMapType.Variable) { if (row.Values.Count > 1) { StringBuilder line = new StringBuilder(); bool first = true; int index = 0; foreach (var col in mapNode.Columns) { var val = (index < row.Values.Count ? SharpValue.ToString(row.Values[index], col.ValueType) : col.DefaultValue.ToString()) ?? ""; index++; if (!first) { line.Append(delimiter); } first = false; line.Append(val); } _writer.WriteLine(line); } else { _writer.WriteLine(row.Values[0]); } } else if (mapType == SharpMapType.Sequence) { int index = 0; StringBuilder line = new StringBuilder(); foreach (var col in mapNode.Columns) { var val = (index < row.Values.Count ? SharpValue.ToString(row.Values[index], col.ValueType) : col.DefaultValue.ToString()) ?? ""; index++; if (val.Length == col.Width) { line.Append(val); } else if (val.Length > col.Width) { line.Append(col.AlignLeft ? val.Substring(0, col.Width) : val.Substring(val.Length - col.Width, col.Width)); } else { if (col.AlignLeft) { line.Append(val); line.Append(col.Padding, col.Width - val.Length); } else { line.Append(col.Padding, col.Width - val.Length); line.Append(val); } } } _writer.WriteLine(line.ToString()); } } }