Exemplo n.º 1
0
        public void Export(string[] collectionNames, string outpath, ExportFormat exportFormatSelected)
        {
            if (!Directory.Exists(outpath))
            {
                throw new ArgumentException("output path doesn't exist", nameof(outpath));
            }

            IImportExportFormatter formatter = ImportExportFormatterFactory.Create(exportFormatSelected);

            if (formatter == null)
            {
                throw new ArgumentException("Can't find appropriate formatter for " + exportFormatSelected, nameof(exportFormatSelected));
            }

            IMagicDatabaseReadOnly magicDatabase = MagicDatabaseManager.ReadOnly;

            foreach (string collectionName in collectionNames)
            {
                ICardCollection cardcollection = magicDatabase.GetCollection(collectionName);
                IEnumerable <ICardInCollectionCount> cardsInCollection = magicDatabase.GetCardCollection(cardcollection);

                if (cardsInCollection == null)
                {
                    throw new ImportExportException("Can't find collection named {0}", collectionName);
                }

                string filePath = Path.Combine(outpath, collectionName + formatter.Extension);

                try
                {
                    using (StreamWriter sw = new StreamWriter(filePath, false))
                    {
                        sw.Write(formatter.ToFile(cardsInCollection));
                    }
                }
                catch (ImportExportException)
                {
                    if (File.Exists(filePath))
                    {
                        File.Delete(filePath);
                    }

                    throw;
                }
            }
        }
Exemplo n.º 2
0
        private IEnumerable <IImportExportCardCount> GetImport(string importFilePath)
        {
            if (!File.Exists(importFilePath))
            {
                throw new ArgumentException("import file doesn't exist", nameof(importFilePath));
            }

            IImportExportFormatter formatter = ImportExportFormatterFactory.CreateForFile(importFilePath);

            if (formatter == null)
            {
                throw new ArgumentException("Can't find appropriate formatter for " + importFilePath, nameof(importFilePath));
            }

            using (StreamReader sr = new StreamReader(importFilePath))
            {
                return(formatter.Parse(sr.ReadToEnd()));
            }
        }