Beispiel #1
0
        public ERB_Parser(string erb_path, Encoding encoding)
        {
            this.ErbPath = erb_path;
            if (!File.Exists(erb_path))
            {
                throw new FileNotFoundException();
            }
            FileInfo info = new FileInfo(erb_path);

            if (!File.Exists(Application.StartupPath + "\\Backup\\" + info.Name))
            {
                info.CopyTo(Application.StartupPath + "\\Backup\\" + info.Name);
            }
            using (FileStream ErbStream = info.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                //string plainPattern = @"(?<LeftBlank>\s*)(?<FunctionCode>PRINT[^(FORM)(FORMS) ]*)( (?<Text>[^;]+))?(?<RightComment>;.+)?";
                //string formPattern = @"(?<LeftBlank>\s*)(?<FunctionCode>PRINTFORM[^ S]*)( (?<Text>[^;]+))?(?<RightComment>;.+)?";
                //string formsPattern = @"(?<LeftBlank>\s*)(?<FunctionCode>PRINTFORMS[^ ]*)( (?<Text>[^;]+))?(?<RightComment>;.+)?";

                StreamReader reader = encoding != null ? new StreamReader(ErbStream, encoding) : new StreamReader(ErbStream, true);
                {
                    ErbEncoding = reader.CurrentEncoding;
                    int lineNo = -1;

                    string originalText = null;
                    string temp         = null;

                    Action ReadNext = null;
                    ReadNext = () =>
                    {
                        temp = reader.ReadLine();
                        lineNo++;
                        var original = IsOriginalLine(temp);
                        if (original.Success)
                        {
                            originalText = original.Groups[1].Value;
                            temp         = reader.ReadLine();
                            lineNo++;
                        }
                        else
                        {
                            originalText = null;
                        }
                        OriginalTexts.Add(lineNo, temp);
                    };

                    while (!reader.EndOfStream)
                    {
                        ReadNext();
                        var match = ParseLine(temp);

                        if (match.Success)
                        {
                            string code = match.Groups["FunctionCode"].Value;
                            if (Regex.IsMatch(code, "PRINTDATA[KD]?[LW]?"))//PRINTDATA문
                            {
                                int  printDataLine = lineNo;
                                int  listLine      = -1;
                                bool exit          = false;
                                while (!exit)
                                {
                                    ReadNext();
                                    match = ParseLine(temp);
                                    if (!match.Success)
                                    {
                                        continue;
                                    }
                                    switch (match.Groups["FunctionCode"].Value)
                                    {
                                    case "DATALIST":
                                    {
                                        listLine = lineNo;
                                        break;
                                    }

                                    case "DATA":
                                    {
                                        NonStringDictionary.Add(lineNo, Tuple.Create(match.Groups["Left"].Value, match.Groups["Right"].Value));
                                        StringDictionary.Add(lineNo, new LineInfo(match.Groups["Content"].Value, false, originalText, printDataLine, listLine));
                                        break;
                                    }

                                    case "DATAFORM":
                                    {
                                        NonStringDictionary.Add(lineNo, Tuple.Create(match.Groups["Left"].Value, match.Groups["Right"].Value));
                                        StringDictionary.Add(lineNo, new LineInfo(match.Groups["Content"].Value, true, originalText, printDataLine, listLine));
                                        break;
                                    }

                                    case "ENDLIST":
                                    {
                                        listLine = -1;
                                        break;
                                    }

                                    case "ENDDATA":
                                    {
                                        exit = true;
                                        break;
                                    }

                                    default:
                                    {
                                        throw new Exception("구문분석중 오류가 발생되었습니다.\r\n줄수:" + lineNo + "\r\n처리중인 문자열:" + temp);
                                    }
                                    }
                                }
                            }
                            else if (Regex.IsMatch(code, "PRINTBUTTON[C(LC)]"))
                            {
                                var buttonMatch = Regex.Match(match.Groups["Content"].Value, @"""(?<PrintText>[^""]*)(?<Right>"".+)");
                                NonStringDictionary.Add(lineNo, Tuple.Create(match.Groups["Left"].Value + "\"", buttonMatch.Groups["Right"].Value + match.Groups["Right"].Value));
                                StringDictionary.Add(lineNo, new LineInfo(buttonMatch.Groups["PrintText"].Value, false, false, originalText));
                            }
                            else
                            {//일반 PRINT문
                                NonStringDictionary.Add(lineNo, Tuple.Create(match.Groups["Left"].Value, match.Groups["Right"].Value));
                                StringDictionary.Add(lineNo, new LineInfo(match.Groups["Content"].Value, code.Contains("FORM"), code.Contains("FORMS"), originalText));
                            }
                        }
                    }
                }
            }
            return;
        }
Beispiel #2
0
        public ErbParser(string erb_path, Encoding readerEncoding, Encoding writerEncoding)
        {
            ErbPath         = erb_path;
            _writerEncoding = writerEncoding;

            if (!File.Exists(erb_path))
            {
                throw new FileNotFoundException();
            }

            var info = new FileInfo(erb_path);

            using (FileStream ErbStream = info.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                StreamReader reader = readerEncoding != null ? new StreamReader(ErbStream, readerEncoding) : new StreamReader(ErbStream, true);
                {
                    int lineNo = -1;

                    string originalText = null;
                    string temp         = null;

                    Match ParseNextLine()
                    {
                        temp = reader.ReadLine();
                        lineNo++;
                        var original = IsOriginalLine(temp);

                        if (original.Success)
                        {
                            originalText = original.Groups[1].Value;
                            temp         = reader.ReadLine();
                            lineNo++;
                        }
                        else
                        {
                            originalText = null;
                        }
                        OriginalTexts.Add(lineNo, temp);
                        return(ParseLine(temp));
                    }

                    while (!reader.EndOfStream)
                    {
                        var match = ParseNextLine();

                        if (match.Success)
                        {
                            string code = match.Groups["FunctionCode"].Value;
                            if (Regex.IsMatch(code, "PRINTDATA[KD]?[LW]?"))//PRINTDATA문
                            {
                                int  printDataLine = lineNo;
                                int  listLine      = -1;
                                bool exit          = false;
                                while (!exit)
                                {
                                    match = ParseNextLine();
                                    if (!match.Success)
                                    {
                                        continue;
                                    }
                                    switch (match.Groups["FunctionCode"].Value)
                                    {
                                    case "DATALIST":
                                    {
                                        listLine = lineNo;
                                        break;
                                    }

                                    case "DATA":
                                    {
                                        NonStringDictionary.Add(lineNo, Tuple.Create(match.Groups["Left"].Value, match.Groups["Right"].Value));
                                        StringDictionary.Add(lineNo, new LineInfo(match.Groups["Content"].Value, false, originalText, printDataLine, listLine));
                                        break;
                                    }

                                    case "DATAFORM":
                                    {
                                        NonStringDictionary.Add(lineNo, Tuple.Create(match.Groups["Left"].Value, match.Groups["Right"].Value));
                                        StringDictionary.Add(lineNo, new LineInfo(match.Groups["Content"].Value, true, originalText, printDataLine, listLine));
                                        break;
                                    }

                                    case "ENDLIST":
                                    {
                                        listLine = -1;
                                        break;
                                    }

                                    case "ENDDATA":
                                    {
                                        exit = true;
                                        break;
                                    }

                                    default:
                                    {
                                        throw new Exception("구문분석중 오류가 발생되었습니다.\r\n줄수:" + lineNo + "\r\n처리중인 문자열:" + temp);
                                    }
                                    }
                                }
                            }
                            else if (Regex.IsMatch(code, "PRINTBUTTON[C(LC)]"))
                            {
                                var buttonMatch = Regex.Match(match.Groups["Content"].Value, @"""(?<PrintText>[^""]*)(?<Right>"".+)");
                                NonStringDictionary.Add(lineNo, Tuple.Create(match.Groups["Left"].Value + "\"", buttonMatch.Groups["Right"].Value + match.Groups["Right"].Value));
                                StringDictionary.Add(lineNo, new LineInfo(buttonMatch.Groups["PrintText"].Value, false, false, originalText));
                            }
                            else
                            {
                                //일반 PRINT문
                                NonStringDictionary.Add(lineNo, Tuple.Create(match.Groups["Left"].Value, match.Groups["Right"].Value));
                                StringDictionary.Add(lineNo, new LineInfo(match.Groups["Content"].Value, code.Contains("FORM"), code.Contains("FORMS"), originalText));
                            }
                        }
                    }
                }
            }
            return;
        }