Exemplo n.º 1
0
        public void SaveFlatFile(string rootNodePath, string filename, string formatfile = null, Encoding encoding = null)
        {
            if (formatfile == null)
            {
                var fi = new FileInfo(filename);
                formatfile = filename.Substring(0, filename.Length - fi.Extension.Length) + ".fmt";
            }

            var rows = _rows.GetRows();

            SharpNodeMap nodeMap;

            if (!_mapSets.TryGetValue(rootNodePath, out nodeMap))
            {
                var rootNode = _nodes.GetNode(rootNodePath);
                var maps     = _nodes.GetNodes().Where(n => n is SharpNodeMap && n.Parent == rootNode).ToList();
                if (maps.Count > 1)
                {
                    throw new Exception(string.Format("Could not save flat file format: Multiple column mappings found for '{0}'", rootNodePath));
                }

                nodeMap = maps.FirstOrDefault() as SharpNodeMap;
            }

            if (nodeMap == null)
            {
                throw new Exception(string.Format("Could not save flat file format: No column mapping not found for '{0}'", rootNodePath));
            }

            FormatWriter.SaveFormatFile(formatfile, nodeMap, encoding ?? _defaultEncoding);

            using (var dest = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite))
            {
                using (var writer = new FlatWriter(dest, encoding ?? _defaultEncoding))
                {
                    writer.Write(nodeMap, rows);
                }
            }
        }