Ejemplo n.º 1
0
        public void Write(SharpNodeMap mapNode, IEnumerable <SharpRow> rows)
        {
            if (mapNode.MapType == SharpMapType.Variable && IncludeHeader)
            {
                var header = string.Join(mapNode.Delimiter.ToString(), mapNode.Columns.Select(x => x.FieldName));
                _writer.WriteLine(header);
            }

            var mapPath = mapNode.Parent.Path;

            foreach (var row in rows)
            {
                var valueRow = row as SharpValueRow;
                if (valueRow != null && valueRow.Node.Parent.Path == mapPath)
                {
                    WriteFixedRow(valueRow, mapNode);
                    continue;
                }

                var nodeRow = row as SharpNodeRow;
                if (nodeRow != null && nodeRow.Node.Path == mapPath)
                {
                    WriteFixedRow(nodeRow, mapNode);
                }
            }
        }
Ejemplo n.º 2
0
        private void WriteFixedRow(SharpNodeRow row, SharpNodeMap mapNode)
        {
            var columns = new Dictionary <string, string>();

            var next = row.First;

            if (next.Node.IsValueNode && !next.Node.IsSingleValueNode)
            {
                if (next.Node.Parent.Path == mapNode.Parent.Path)
                {
                    return;
                }
            }

            while (next != null)
            {
                var vr = next as SharpValueRow;
                if (vr != null)
                {
                    columns[next.Node.Name] = SharpValue.ToString(vr.Values[0], vr.Node.ValueType);
                }

                next = next.Next;
            }

            WriteLine(columns, mapNode);
        }
Ejemplo n.º 3
0
		public FlatReader(Stream source, SharpNodeMap node, Encoding encoding = null, bool skipEmptyRows = false)
		{
			_encoding = encoding ?? Encoding.UTF8;
			_source = source;
			_node = node;
            SkipEmptyRows = skipEmptyRows;
		}
Ejemplo n.º 4
0
        public SharpMetaNodeMapRow(SharpNodeMap node)
        {
            Index   = node.Index;
            MapType = node.MapType;
            var columns = node.Columns;

            Members = columns.Select(x => x.Index).ToList();
            Columns = columns.Select(x => x.Width).ToList();
        }
Ejemplo n.º 5
0
 public static void SaveFormatFile(string filename, SharpNodeMap nodeMap, Encoding encoding = null)
 {
     using (var stream = new FileStream(filename, FileMode.Create, FileAccess.Write))
     {
         using (var writer = new StreamWriter(stream, encoding ?? Encoding.GetEncoding(1252)))
         {
             foreach (var col in nodeMap.Columns)
             {
                 var columnName = col.Node.Name;
                 writer.WriteLine(string.Format("{0},{1},C", columnName, col.Width));
             }
             writer.WriteLine("EOR,2,B");
         }
     }
 }
Ejemplo n.º 6
0
        public static SharpValueRow CreateRow(List <SharpValueRow> rows, SharpNodeCollection nodes)
        {
            if (rows.Count() == 1)
            {
                return(rows[0]);
            }

            List <object>    values  = new List <object>();
            List <SharpNode> columns = new List <SharpNode>();

            StringBuilder sb    = new StringBuilder();
            bool          first = true;

            foreach (var row in rows)
            {
                if (!first)
                {
                    sb.Append(',');
                }
                first = false;
                sb.Append(row.Node.Index);
                if (row.Values == null)
                {
                    values.Add(null);
                }
                else
                {
                    values.AddRange(row.Values);
                }

                columns.Add(row.Node);
            }

            string path = sb.ToString();

            var node = nodes.GetNode(path);

            if (node == null)
            {
                node = new SharpNodeMap(columns);
                nodes.Add(node);
            }

            return(new SharpValueRow(node, values));
        }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 8
0
        private void WriteLine(Dictionary <string, string> columns, SharpNodeMap mapNode)
        {
            StringBuilder line = new StringBuilder();

            foreach (var col in mapNode.Columns)
            {
                string val = "";

                if (columns.ContainsKey(col.Node.Name))
                {
                    val = SharpValue.ToString(columns[col.Node.Name], col.Node.ValueType);
                }
                else
                {
                    val = col.DefaultValue == null ? String.Empty : col.DefaultValue.ToString();
                }

                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());
        }
Ejemplo n.º 9
0
        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);
        }
Ejemplo n.º 10
0
        public static SharpNodeMap LoadHeaderLine(string line, char delimiter, SharpNodeCollection nodes, string path, Dictionary <string, string> aliases = null)
        {
            SharpNode rootNode = nodes.GetNode(path);

            if (rootNode == null)
            {
                rootNode = new SharpNode
                {
                    Path = path,
                    Name = SharpNode.GetNodeName(path),
                };
                nodes.Add(rootNode);
            }

            SharpNodeMap result = new SharpNodeMap();

            result.MapType = SharpMapType.Variable;

            var columns = line.Split(delimiter).Select(x => x.Trim().Trim('"')).ToList();

            foreach (var column in columns)
            {
                var colname = column;
                if (aliases != null)
                {
                    if (aliases.ContainsKey(colname))
                    {
                        colname = aliases[colname];
                    }
                }
                var nodePath = string.Format("{0}/{1}/#", path, colname);
                var colNode  = nodes.GetNode(nodePath);
                if (colNode == null)
                {
                    colNode = new SharpNode
                    {
                        Path        = nodePath,
                        Name        = colname,
                        IsValueNode = true,
                        IsLeafNode  = true
                    };
                    nodes.Add(colNode);
                }

                result.Columns.Add(new SharpNodeMapColumn(colNode)
                {
                    FieldName = column
                });
            }

            string mapPath = string.Join(",", result.Columns.Select(c => c.Node.Index));

            var mapNode = new SharpNodeMap
            {
                Path        = mapPath,
                IsValueNode = true,
                MapType     = SharpMapType.Variable,
                Delimiter   = delimiter,
            };

            mapNode.SetColumns(result.Columns);
            nodes.Add(mapNode);

            if (nodes.GetNode(mapPath) == null)
            {
                nodes.Add(mapNode);
            }

            return(mapNode);
        }
Ejemplo n.º 11
0
        public SharpNodeMap CreateColumnNodes(SharpNodeCollection nodes, string path, bool alwaysUpdateColumns = true)
        {
            SharpNode rootNode = nodes.GetNode(path);

            if (rootNode == null)
            {
                rootNode = new SharpNode
                {
                    Path = path,
                    Name = SharpNode.GetNodeName(path),
                };
                nodes.Add(rootNode);
            }

            using (var reader = new StreamReader(_source, _encoding))
            {
                int position = 0;
                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();
                    if (line == null)
                    {
                        continue;
                    }

                    line = line.Trim();
                    if (string.IsNullOrEmpty(line))
                    {
                        continue;
                    }

                    var parts = line.Split(',').Select(x => x.Trim()).ToList();

                    string fieldName  = parts[0];
                    string columnName = fieldName;
                    if (_names.ContainsKey(columnName))
                    {
                        columnName = _names[columnName];
                    }

                    if (columnName == "EOR")
                    {
                        continue;
                    }

                    string columnPath = string.Format("{0}/{1}/#", rootNode.Path, columnName);
                    int    columnWidth;
                    int.TryParse(parts[1], out columnWidth);

                    var columnNode = nodes.GetNode(columnPath);

                    if (columnNode == null)
                    {
                        columnNode = new SharpNode
                        {
                            Path        = columnPath,
                            Name        = columnName,
                            IsValueNode = true,
                            ValueType   = _types.ContainsKey(columnName) ? _types[columnName] : SharpValueType.None,
                        };

                        nodes.Add(columnNode);
                    }

                    var column = new SharpNodeMapColumn(columnNode, columnWidth, position);
                    column.FieldName = fieldName;

                    if (_allowTrim.ContainsKey(columnName))
                    {
                        column.AllowTrim = _allowTrim[columnName];
                    }

                    if (_alignLeft.ContainsKey(columnName))
                    {
                        column.AlignLeft = _alignLeft[columnName];
                    }

                    if (_format.ContainsKey(columnName))
                    {
                        column.Format = _format[columnName];
                    }

                    if (_padding.ContainsKey(columnName))
                    {
                        column.Padding = _padding[columnName];
                    }

                    position += columnWidth;
                    _columns.Add(column);
                }
            }

            string mapPath = string.Join(",", _columns.Select(c => c.Node.Index));

            var mapNode = new SharpNodeMap
            {
                Path        = mapPath,
                IsValueNode = true
            };

            mapNode.SetColumns(_columns);
            mapNode.MapType = SharpMapType.Fixed;

            if (nodes.GetNode(mapPath) == null)
            {
                nodes.Add(mapNode);
            }
            else
            {
                mapNode.Parent = nodes.GetNode(mapPath).Parent;
            }
            return(mapNode);
        }