예제 #1
0
        public static async Task Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("Usage:");
                Console.WriteLine("homework input.xml output.json");
                return;
            }

            var tokenSource = new CancellationTokenSource();
            var token       = tokenSource.Token;

            try
            {
                var sourceFileName = args[0];
                var targetFileName = args[1];

                var sourceStorage = new FileStorage(sourceFileName);
                var inputStream   = await sourceStorage.ReadStreamAsync(token);

                var inputConverter = new XmlFormatInputConverter();
                var document       = await inputConverter.ReadAsync(inputStream, token);

                var outputConverter = new JsonFormatOutputConverter();
                var outputStream    = await outputConverter.WriteAsync(document, token);

                var destinationStorage = new FileStorage(targetFileName);
                await destinationStorage.WriteStreamAsync(outputStream, token);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
                Console.WriteLine($"Conversion has failed. {e.InnerException?.Message}");
            }
        }
예제 #2
0
        public void TestRead()
        {
            var          converter = new XmlFormatInputConverter();
            const string input     = @"<?xml version=""1.0"" ?><document><title>Title</title><text>Text</text></document>";

            using var ms = new MemoryStream(Encoding.Default.GetBytes(input));
            var document = converter.ReadAsync(ms).Result;

            Assert.AreEqual("Title", document.Title);
            Assert.AreEqual("Text", document.Text);
        }
예제 #3
0
        public void TestWrite()
        {
            var converter = new XmlFormatOutputConverter();

            var document = new Document
            {
                Text  = "Text",
                Title = "Title"
            };

            var stream = converter.WriteAsync(document).Result;

            var reverseConverter = new XmlFormatInputConverter();
            var testDoc          = reverseConverter.ReadAsync(stream).Result;

            Assert.AreEqual(document.Title, testDoc.Title);
            Assert.AreEqual(document.Text, testDoc.Text);
        }