Пример #1
0
        /// <summary>
        /// Parse WITS record
        /// </summary>
        /// <param name="input">input data string</param>
        /// <returns>Returns deserialized WITS record</returns>
        public static WitsRecord Parse(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                throw new ArgumentNullException("input can't be null or empty");
            }

            input.Trim();
            if (input.Length < DataStartIndex)
            {
                throw new ParseException("Wits record length is less then identificator length");
            }

            try
            {
                WitsRecord word = new WitsRecord()
                {
                    GroupId = input.Substring(GroupIdStartIndex, GroupIdLength),
                    Id      = input.Substring(IdStartIndex, IdLength),
                    Data    = input.Substring(DataStartIndex, input.Length - DataStartIndex)
                };

                return(word);
            }
            catch (Exception ex)
            {
                throw new ParseException("WITS parse", ex);
            }
        }
Пример #2
0
        /// <summary>
        /// Parse WITS sentence from string
        /// </summary>
        /// <param name="input">String data</param>
        /// <returns>Return parsed sentence <see cref="WitsSentence"/>/></returns>
        public static WitsSentence Parse(string input)
        {
            try
            {
                if (string.IsNullOrEmpty(input))
                {
                    throw new Exception("input can't be null null");
                }
                input.Trim();
                string[] strWords = input.Split('\n');
                if (strWords.Length < INT_MinWordsInSentence)
                {
                    throw new Exception("WITS sentence has no records");
                }
                if (strWords.First().Trim() != SentenceBegining)
                {
                    throw new Exception(@"WITS sentence start symbols (&&) not found");
                }
                if (strWords.Where((s) => s == SentenceEnding).FirstOrDefault() == null)
                {
                    throw new Exception(@"WITS sentence ending not found (!!)");
                }

                var sentence = new WitsSentence();
                foreach (var strWord in strWords)
                {
                    if (strWord.StartsWith(SentenceBegining))
                    {
                        continue;
                    }

                    if (strWord.StartsWith(SentenceEnding))
                    {
                        break;
                    }

                    sentence.Records.Add(WitsRecord.Parse(strWord));
                }
                return(sentence);
            }
            catch (Exception ex)
            {
                throw new ParseException("Error while parsing WITS sentence", ex);
            }
        }