Example #1
0
        protected override void ParseAllLines(string[] allLines)
        {
            string location = this.GetType().Name + "." + MethodBase.GetCurrentMethod().Name;

            _logger.OpenSection(location);

            var lastLineType = LineTypeEnum.Nothing;

            foreach (var oneLine in allLines)
            {
                var result   = ParseLine(oneLine, lastLineType);
                var lineType = result.Item1;
                var contents = result.Item2;

                switch (lineType)
                {
                case LineTypeEnum.Nothing:
                case LineTypeEnum.Comment:
                    break;

                case LineTypeEnum.Kanji:
                case LineTypeEnum.Kana:
                case LineTypeEnum.Romaji:
                case LineTypeEnum.Translation:
                    var ex = new Exception("Line type '" + lineType + "' is not a valid line type for this parser.");
                    _logger.Error(ex);
                    throw ex;

                case LineTypeEnum.Source:
                    _logger.Info("Adding new source '" + contents + "'");
                    var source = new Domain.Source(contents);
                    AddNewSource(source);
                    break;

                case LineTypeEnum.Quote:
                    _logger.Info("Adding new quote '" + contents + "'");
                    var character = contents.Substring(0, contents.IndexOf("「"));
                    var quoteText = FeatherStrings.GetTextBetween(contents, "「", "」");

                    var quote = new Domain.Quote(character, quoteText);
                    AddNewQuote(quote);
                    break;

                case LineTypeEnum.JwpceWord:
                    var kanji       = contents.Substring(0, contents.IndexOf("【"));
                    var kana        = FeatherStrings.GetTextBetween(contents, "【", "】");
                    var translation = contents.Substring(contents.IndexOf("】") + 1);

                    var word = new Nightingale.Domain.Word(kanji, kana, translation);
                    AddNewWord(word);
                    break;
                }
                lastLineType = lineType;
            }

            _logger.CloseSection(location);
        }
Example #2
0
        protected override void ParseAllLines(string[] allLines)
        {
            string location = this.GetType().Name + "." + MethodBase.GetCurrentMethod().Name;

            _logger.OpenSection(location);

            var lastLineType = LineTypeEnum.Nothing;

            Exception ex;

            foreach (var oneLine in allLines)
            {
                var result   = ParseLine(oneLine, lastLineType);
                var lineType = result.Item1;
                var contents = result.Item2;

                switch (lineType)
                {
                case LineTypeEnum.JwpceWord:
                    ex = new Exception("Line type '" + lineType + "' is not a valid line type for this parser.");
                    _logger.Error(ex);
                    throw ex;
                    break;

                case LineTypeEnum.Nothing:
                case LineTypeEnum.Comment:
                case LineTypeEnum.Romaji:
                    break;

                case LineTypeEnum.Source:
                    _logger.Info("Adding new source '" + contents + "'");
                    var source = new Domain.Source(contents);
                    AddNewSource(source);
                    break;

                case LineTypeEnum.Quote:
                    _logger.Info("Adding new quote '" + contents + "'");
                    var character = contents.Substring(0, contents.IndexOf("「"));
                    var quoteText = FeatherStrings.GetTextBetween(contents, "「", "」");

                    var quote = new Domain.Quote(character, quoteText);
                    AddNewQuote(quote);
                    break;

                ///////////////////////////////

                case LineTypeEnum.Kanji:
                    if (!String.IsNullOrEmpty(_kanji) ||
                        !String.IsNullOrEmpty(_kana) ||
                        !String.IsNullOrEmpty(_translation))
                    {
                        ex = new Exception("Kanji is not valid. A word is currently being created. " +
                                           "_kanji = '" + _kanji + "', _kana = '" + _kana + "', _translation = '" + _translation + "'");
                        _logger.Error(ex);
                        throw ex;
                    }
                    _kanji = contents;
                    break;

                case LineTypeEnum.Kana:
                    if (String.IsNullOrEmpty(_kanji) ||
                        !String.IsNullOrEmpty(_kana) ||
                        !String.IsNullOrEmpty(_translation))
                    {
                        ex = new Exception("Kana is not valid. A word is currently being created. " +
                                           "_kanji = '" + _kanji + "', _kana = '" + _kana + "', _translation = '" + _translation + "'");
                        _logger.Error(ex);
                        throw ex;
                    }
                    _kana = contents;
                    break;

                case LineTypeEnum.Translation:
                    if (!String.IsNullOrEmpty(_kanji) &&
                        String.IsNullOrEmpty(_kana) &&
                        String.IsNullOrEmpty(_translation))
                    {
                        // e.g. "うだうだ \r\n idle, long-winded and meaningless"
                        _logger.Info("Word is kana-only.");

                        _kana        = _kanji; // actually _kanji contains the kana here...
                        _translation = contents;

                        var wordKanaOnly = new Nightingale.Domain.Word(_kanji, _kana, _translation);
                        AddNewWord(wordKanaOnly);

                        // Revert to "not currently inserting a word" mode
                        _kanji = _kana = _translation = null;
                        break;
                    }

                    if (String.IsNullOrEmpty(_kanji) ||
                        String.IsNullOrEmpty(_kana) ||
                        !String.IsNullOrEmpty(_translation))
                    {
                        ex = new Exception("Translation is not valid. Must be constructing a new word. " +
                                           "_kanji = '" + _kanji + "', _kana = '" + _kana + "', _translation = '" + _translation + "'");
                        _logger.Error(ex);
                        throw ex;
                    }
                    _translation = contents;

                    var word = new Nightingale.Domain.Word(_kanji, _kana, _translation);
                    AddNewWord(word);

                    // Revert to "not currently inserting a word" mode
                    _kanji = _kana = _translation = null;
                    break;

                    ///////////////////////////////
                }
                lastLineType = lineType;
            }

            _logger.CloseSection(location);
        }