コード例 #1
0
ファイル: PFRDocumentReader.cs プロジェクト: Ran-QUAN/Alta
        public WEMDocument[] ReadDocuments(Stream textStream,
            Encoding outputEncoding, Encoding inputEncoding)
        {
            ParseContext context = new ParseContext(outputEncoding);

            using (StreamReader reader
                = new StreamReader(textStream, inputEncoding, false, 8192))
            {
                while (context.Status != ReadStatus.Done)
                {
                    context.ThrowIfError();

                    switch (context.Status)
                    {
                    case ReadStatus.Initial:
                        {
                            if (reader.EndOfStream)
                            {
                                context.Status = ReadStatus.Done;
                            }
                            else
                            {
                                context.Input((char)reader.Read());

                                switch (context.LastInput)
                                {
                                case '\r':
                                case '\n':
                                    break;

                                default:
                                    if (Char.IsDigit(context.LastInput))
                                    {
                                        context.Count++;
                                        context.Status = ReadStatus.ReadingDate;

                                        context.DocumentName
                                            = context.LastInput.ToString();
                                    }
                                    else
                                    {
                                        context.InvalidInput();
                                    }
                                    break;
                                }
                            }
                        }
                        break;

                    case ReadStatus.ReadingDate:
                        {
                            if (reader.EndOfStream)
                            {
                                context.IncompletedFormat();
                            }
                            else
                            {
                                context.Input((char)reader.Read());

                                if (context.Count == 8)
                                {
                                    if (context.LastInput == '-')
                                    {
                                        context.DocumentName
                                            += context.LastInput;

                                        context.Count = 0;
                                        context.Status = ReadStatus.ReadingPage;
                                    }
                                    else
                                    {
                                        context.InvalidInput();
                                    }
                                }
                                else if (Char.IsDigit(context.LastInput))
                                {
                                    context.Count++;
                                    context.DocumentName
                                        += context.LastInput;
                                }
                                else
                                {
                                    context.InvalidInput();
                                }
                            }
                        } break;

                    case ReadStatus.ReadingPage:
                        {
                            if (reader.EndOfStream)
                            {
                                context.IncompletedFormat();
                            }
                            else
                            {
                                context.Input((char)reader.Read());

                                if (context.Count == 2)
                                {
                                    if (context.LastInput == '-')
                                    {
                                        context.DocumentName
                                            += context.LastInput;

                                        context.Count = 0;
                                        context.Status
                                            = ReadStatus.ReadingDocumentNumber;
                                    }
                                    else
                                    {
                                        context.InvalidInput();
                                    }
                                }
                                else if (Char.IsDigit(context.LastInput))
                                {
                                    context.Count++;
                                    context.DocumentName += context.LastInput;
                                }
                                else
                                {
                                    context.InvalidInput();
                                }
                            }
                        } break;

                    case ReadStatus.ReadingDocumentNumber:
                        {
                            if (reader.EndOfStream)
                            {
                                context.IncompletedFormat();
                            }
                            else
                            {
                                context.Input((char)reader.Read());

                                if (context.Count == 3)
                                {
                                    if (context.LastInput == '-')
                                    {
                                        context.Count = 0;
                                        context.Status
                                            = ReadStatus.ReadingParagraphNumber;

                                        context.CreateDocument();
                                    }
                                    else
                                    {
                                        context.InvalidInput();
                                    }
                                }
                                else if (Char.IsDigit(context.LastInput))
                                {
                                    context.Count++;
                                    context.DocumentName += context.LastInput;
                                }
                                else
                                {
                                    context.InvalidInput();
                                }
                            }
                        } break;

                    case ReadStatus.ReadingParagraphNumber:
                        {
                            if (reader.EndOfStream)
                            {
                                context.IncompletedFormat();
                            }
                            else
                            {
                                context.Input((char)reader.Read());

                                if (context.Count == 3)
                                {
                                    if (context.LastInput == '/')
                                    {
                                        context.Count = 0;

                                        context.Skip(
                                            "m  ", ReadStatus.ReadingWord);
                                    }
                                    else
                                    {
                                        context.InvalidInput();
                                    }
                                }
                                else if (Char.IsDigit(context.LastInput))
                                {
                                    context.Count++;
                                }
                                else
                                {
                                    context.InvalidInput();
                                }
                            }
                        } break;

                    case ReadStatus.ReadingWord:
                        {
                            if (reader.EndOfStream)
                            {
                                context.Status = ReadStatus.Done;
                                context.EndSentence();
                            }
                            else
                            {
                                context.Input((char)reader.Read());

                                if (context.GotPeriod
                                    && !ParseContext.Periods.Contains(
                                    context.LastInput)
                                    && !ParseContext.RightParentheses.Contains(
                                    context.LastInput))
                                {
                                    context.EndSentence();
                                }

                                switch (context.LastInput)
                                {
                                case '\r':
                                    context.EndSentence();
                                    context.Skip("\n", ReadStatus.Initial);
                                    break;

                                case '[':
                                    context.PushEntity();
                                    break;

                                case ' ':
                                    break;

                                default:
                                    if (ParseContext.Periods.Contains(
                                        context.LastInput))
                                    {
                                        context.GotPeriod = true;
                                    }

                                    context.CreateWord();
                                    context.Status
                                        = ReadStatus.ReadingWordContent;
                                    break;
                                }
                            }
                        } break;

                    case ReadStatus.ReadingWordContent:
                        {
                            if (reader.EndOfStream)
                            {
                                context.IncompletedFormat();
                            }
                            else
                            {
                                context.Input((char)reader.Read());

                                if (context.LastInput == '/')
                                {
                                    context.Status = ReadStatus.ReadingTag;
                                }
                                else if (Char.IsWhiteSpace(context.LastInput)
                                    || Char.IsControl(context.LastInput))
                                {
                                    context.InvalidInput();
                                }
                            }
                        } break;

                    case ReadStatus.ReadingTag:
                        {
                            if (reader.EndOfStream)
                            {
                                context.IncompletedFormat();
                            }
                            else
                            {
                                context.Input((char)reader.Read());

                                if (context.LastInput == ']')
                                {
                                    context.EndWord();
                                    context.EndEntity();
                                }
                                else if (!Char.IsLetter(context.LastInput))
                                {
                                    context.EndWord();
                                    context.Skip(" ", ReadStatus.ReadingWord);
                                }
                            }
                        } break;

                    case ReadStatus.Skipping:
                        {
                            if (reader.EndOfStream)
                            {
                                context.IncompletedFormat();
                            }
                            else
                            {
                                for (int i = 0; i < context.SkipString.Length;
                                    i++)
                                {
                                    char c = (char)reader.Peek();

                                    if (c != context.SkipString[i] &&
                                        !Char.IsControl(c))
                                    {
                                        //context.InvalidInput();

                                        break;
                                    }

                                    context.Input((char)reader.Read());
                                }

                                context.SkipBack();
                            }
                        } break;
                    }
                }
            }

            return context.GetAllDocuments();
        }