Example #1
0
        public Message Parse(string message)
        {
            // Validate if data has a valid message structure.
            if (!this.validator.Validate(message))
            {
                var exception = new ParserException("Invalid message.");
                exception.Data["Swift.Validator.Errors"] = string.Join(Environment.NewLine, this.validator.Errors);

                throw exception;
            }

            BasicHeader basicHeader = null;
            ApplicationHeader applicationHeader = null;
            UserHeader userHeader = null;
            TextBlock text = null;
            Trailer trailer = null;

            // Iterate through all blocks.
            // Instantiate the specific block when available.
            // Each instance will self validate.
            var iterator = new BlockIterator(message);
            foreach (var block in iterator)
            {
                switch (block.BlockId)
                {
                    case "1":
                        basicHeader = new BasicHeader(block.Value);
                        break;
                    case "2":
                        applicationHeader = new ApplicationHeader(block.Value);
                        break;
                    case "3":
                        userHeader = new UserHeader(block.Value);
                        break;
                    case "4":
                        text = new TextBlock(block.Value);
                        break;
                    case "5":
                        trailer = new Trailer(block.Value);
                        break;
                }
            }

            // A message is composed of these blocks.
            return new Message(basicHeader, applicationHeader, userHeader, text, trailer);
        }