예제 #1
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));
        }
예제 #2
0
        private void OpenFile(string filename, string nodePath, SharpRowReader rowReader)
        {
            if (!string.IsNullOrEmpty(nodePath))
            {
                var node = _nodes.GetNode(nodePath);
                if (node == null)
                {
                    _nodes.Add(new SharpNode
                    {
                        Path = nodePath,
                        Name = SharpNode.GetNodeName(nodePath)
                    });
                }
            }

            _stream = new FileStream(filename, FileMode.Open, FileAccess.Read);
            _rows   = new SharpRowStream
            {
                Nodes        = _nodes,
                StreamNode   = string.IsNullOrEmpty(nodePath) ? null : _nodes.GetNode(nodePath),
                StreamSource = rowReader(_stream, _nodes)
            };
        }
예제 #3
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);
        }
예제 #4
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);
        }
예제 #5
0
        public bool ParseRowLine(string line, out SharpRow result)
        {
            result = null;
            line   = line.Trim();
            if (string.IsNullOrEmpty(line))
            {
                return(false);
            }

            if (line.StartsWith("#!"))
            {
                var param = new SharpMetaParameterRow(line);
                if (param.Key.ToLower() == "package" && _nodes != null)
                {
                    _nodes.SetCurrentPackage(param.Value.ToString());
                }
                result = param;
                return(true);
            }

            if (line.StartsWith("#"))
            {
                result = new SharpMetaCommentRow {
                    Value = line.Substring(1)
                };
                return(true);
            }

            var metaNode = new SharpMetaNodeRow(line, _lastFullPath);

            if (line.StartsWith("$"))
            {
                var schemaNode = metaNode.CreateNode();

                if (metaNode.Index == -1)
                {
                    _nodes.Add(schemaNode);
                }
                else
                {
                    _nodes.Insert(schemaNode);
                }

                return(false);
            }

            bool isValueNode = metaNode.IsValueNode;

            if (!isValueNode)
            {
                _lastFullPath = metaNode.Path;
            }

            if (_nodes == null)
            {
                result = metaNode;
                return(true);
            }

            var node = _nodes.GetNode(metaNode.Path);

            if (node != null)
            {
                result = metaNode.CreateNodeRow(node);
                return(true);
            }

            node = metaNode.CreateNode();
            _nodes.Add(node);
            result = metaNode.CreateNodeRow(node);
            return(true);
        }
예제 #6
0
        private void AddObjectMember(string memberPath, string key, object value, SharpNode node, List <SharpNodeRow> rows)
        {
            var objectList = value as SharpObjectList;

            if (objectList != null)
            {
                if (objectList.HasOnlyValue)
                {
                    AddObjectMember(memberPath, "", objectList.GetValueList(), node, rows);
                    return;
                }
            }

            var memberList = value as IEnumerable <IDictionary <string, object> >;

            if (memberList != null)
            {
                var memberListNode = node ?? _nodes.GetNode(memberPath);
                if (memberListNode == null)
                {
                    memberListNode = new SharpNode
                    {
                        Path     = memberPath,
                        Name     = SharpNode.GetNodeName(memberPath),
                        NodeType = SharpNodeType.Repeated
                    };
                    _nodes.Add(memberListNode);
                }

                foreach (var item in memberList)
                {
                    AddObject(memberPath, item, rows);
                }

                return;
            }

            var memberObj = value as IDictionary <string, object>;

            if (memberObj != null)
            {
                AddObject(memberPath, memberObj, rows);
                return;
            }

            // Value Row
            if (!key.StartsWith("@") && !memberPath.EndsWith("/#"))
            {
                memberPath += "/#";
            }

            // Multiple Value Row
            SharpValueType memberValueType;

            if (SharpValue.TryGetValueList(value, out memberValueType))
            {
                var memberValueListNode = _nodes.GetNode(memberPath);
                if (memberValueListNode == null)
                {
                    memberValueListNode = new SharpNode
                    {
                        Path        = memberPath,
                        Name        = SharpNode.GetNodeName(memberPath),
                        IsValueNode = true,
                        ValueType   = memberValueType,
                        NodeType    = SharpNodeType.Repeated
                    };
                    _nodes.Add(memberValueListNode);
                }

                foreach (var val in ((IEnumerable)value))
                {
                    rows.Add(new SharpValueRow(memberValueListNode, val));
                }

                return;
            }

            // Single Value Row
            var memberValue = new SharpValue(value);

            var memberNode = _nodes.GetNode(memberPath);

            if (memberNode == null)
            {
                memberNode = new SharpNode()
                {
                    Path        = memberPath,
                    Name        = SharpNode.GetNodeName(memberPath),
                    IsValueNode = true,
                    ValueType   = memberValue.Type
                };
                _nodes.Add(memberNode);
            }

            rows.Add(new SharpValueRow(memberNode, memberValue.Value));
        }