Exemple #1
0
        private IEnumerable <Message> ReadFile(FileInfo file)
        {
            // whoops, no file, let's return an empty list
            if (file == null)
            {
                yield break;
            }

            // if no file, don't panic
            if (!file.Exists)
            {
                yield break;
            }


            // looks like we got the file, read it to the end
            // TODO: consider passing a stream to the serializer, in case the file is too big?
            bool skipFirstLine = _serializer.IncludeHeaders;

            using (StreamReader sr = file.OpenText())
            {
                string s = "";
                while ((s = sr.ReadLine()) != null)
                {
                    // some formatters support a header row, i.e. the CSV files, which means
                    // we need to skip the header
                    if (skipFirstLine)
                    {
                        skipFirstLine = false;
                        continue;
                    }
                    yield return(_serializer.Parse(s));
                }
            }
        }
        /// <summary>
        /// It parses the information in the parser context and builds the field.
        /// </summary>
        /// <param name="parserContext">
        /// It's the parser context.
        /// </param>
        /// <returns>
        /// The new field built with the information found in the parser context.
        /// </returns>
        public override Field Parse(ref ParserContext parserContext)
        {
            // If MinValue, at this moment the length hasn't been decoded.
            if (parserContext.DecodedLength == int.MinValue)
            {
                if (!_lengthManager.EnoughData(ref parserContext))
                {
                    // Insufficient data to parse length, return null.
                    return(null);
                }

                // Save length in parser context just in case field value
                // can't be parsed at this time (more data needed).
                parserContext.DecodedLength =
                    _lengthManager.ReadLength(ref parserContext);
            }

            if (parserContext.DataLength < _encoder.GetEncodedLength(
                    parserContext.DecodedLength))
            {
                // Insufficient data to parse field value, return null.
                return(null);
            }

            Message message = null;

            if (parserContext.DecodedLength > 0)
            {
                ParserContext innerMsgParserContext = new ParserContext(parserContext.DecodedLength);
                innerMsgParserContext.Write(
                    _encoder.Decode(ref parserContext, parserContext.DecodedLength));
                message = _messageFormatter.Parse(ref innerMsgParserContext);

                if (message == null)
                {
                    throw new MessagingException(SR.CantParseInnerMessage);
                }
            }

            // Create the new messaging component with parsing context data.
            InnerMessageField field = new InnerMessageField(FieldNumber, message);

            _lengthManager.ReadLengthTrailer(ref parserContext);

            return(field);
        }