Exemplo n.º 1
0
        /// <summary>
        /// Creates a new instance of PostAnalyzer, loads all detectors used
        /// in analyze process
        /// </summary>
        public PostAnalyzer(IConfigurationRoot configuration)
        {
            if (configuration != null)
            {
                codeDetectorTreshold = configuration.GetValue("threshold", 0.99f);
            }

            codeDetector = new CodeDetector();
        }
Exemplo n.º 2
0
        /// <summary>
        /// 置換処理
        /// </summary>
        /// <param name="filepath"></param>
        /// <param name="keyword"></param>
        private void TryReplace(string filepath, Encoding encoding, List <ReplaceParameter> keywords)
        {
            var replaceOcc = false;
            // 出力先
            var output = Path.GetTempFileName();

            if (encoding == null)
            {
                var detector = new CodeDetector();
                encoding = detector.Check(new FileInfo(filepath));
            }

            if (encoding != null)
            {
                using (var reader = new StreamReader(filepath, encoding, false, 1024 * 20))
                    using (var writer = new StreamWriter(output, false, encoding, 1024 * 20))
                    {
                        int           lineCnt = 0;
                        string        lnCd;
                        string        line;
                        StringBuilder newLine = new StringBuilder();
                        while ((line = reader.ReadLine(out lnCd)) != null)
                        {
                            lineCnt++;
                            bool findMatch = false;
                            int  startAt   = 0;

                            do
                            {
                                //置換対象キーワードを含んでいるか?
                                var hitKeyAndMatch =
                                    // 全てのキーワードについて検索
                                    keywords.Select(keyword => Tuple.Create(
                                                        keyword,
                                                        keyword.ReplaceFromPattern.Matches(line, startAt)
                                                        .Cast <Match>().Where(m => m.Success && m.Length != 0)
                                                        .OrderBy(m => m.Index)
                                                        .FirstOrDefault()
                                                        ))
                                    .Where(regres => regres.Item2 != null && regres.Item2.Success && regres.Item2.Length != 0)
                                    // 最初にヒットしたものを対象とする
                                    .OrderBy(regres => regres.Item2.Index)
                                    .FirstOrDefault();

                                if (hitKeyAndMatch != null)
                                {
                                    findMatch = true;

                                    var hitKey = hitKeyAndMatch.Item1;
                                    var match  = hitKeyAndMatch.Item2;

                                    replaceOcc = true;

                                    // ヒット位置より前の文字をそのままコピー
                                    var beforeText = line.Substring(0, match.Index);
                                    newLine.Append(beforeText);

                                    var bgnIdx = startAt;
                                    var endIdx = match.Index;
                                    if (bgnIdx != endIdx)
                                    {
                                        if (AddPlain != null)
                                        {
                                            AddPlain.Invoke(line.Substring(bgnIdx, endIdx - bgnIdx));
                                        }
                                    }

                                    int newLineLength = newLine.Length;

                                    // ヒット位置の文字を置換後の文字に変更
                                    foreach (ExtendReplaceTo rep in hitKey.ReplaceToPattern)
                                    {
                                        if (rep.Type == ReplaceToType.Plain)
                                        {
                                            newLine.Append(rep.Label);
                                        }
                                        else
                                        {
                                            Group group = rep.Type == ReplaceToType.GroupIndex ?
                                                          match.Groups[rep.Index] :
                                                          match.Groups[rep.Label];

                                            string value = group.Value;

                                            switch (rep.Change)
                                            {
                                            case ChangeCase.LowerHead:
                                                value = Char.ToLower(value[0]) + value.Substring(1);
                                                break;

                                            case ChangeCase.LowerAll:
                                                value = value.ToLower();
                                                break;

                                            case ChangeCase.UpperHead:
                                                value = Char.ToUpper(value[0]) + value.Substring(1);
                                                break;

                                            case ChangeCase.UpperAll:
                                                value = value.ToUpper();
                                                break;
                                            }
                                            newLine.Append(value);
                                        }
                                    }

                                    if (AddDiff != null)
                                    {
                                        AddDiff.Invoke(
                                            match.Groups[0].Value,
                                            newLine.ToString().Substring(newLineLength));
                                    }

                                    // ヒット位置より後の文字をそのままコピーし、再検索
                                    startAt = newLine.Length;
                                    newLine.Append(line.Substring(match.Index + match.Length));
                                    line = newLine.ToString();
                                    newLine.Clear();
                                }
                                else
                                {
                                    // どのパターンもヒットしていないなら打ち止め、次の行へ
                                    break;
                                }
                            } while (startAt < line.Length);

                            // startAt < line.Lengthなら、置換されなかった文字があるはずなので、通知
                            if (startAt < line.Length)
                            {
                                if (AddPlain != null)
                                {
                                    AddPlain.Invoke(line.Substring(startAt));
                                }
                            }

                            // 置換処理が行われたことを通知
                            if (findMatch)
                            {
                                if (Inform != null)
                                {
                                    Inform.Invoke(lineCnt, encoding, line);
                                }
                            }

                            writer.Write(line);
                            if (lnCd != null)
                            {
                                writer.Write(lnCd);
                                if (NewLine != null)
                                {
                                    NewLine.Invoke();
                                }
                            }
                        }
                    }

                if (replaceOcc)
                {
                    // 置換前のファイルの退避(DBへ)
                    db.Insert(
                        filepath,
                        Util.MakeHash(output),
                        (Action <string, string>) delegate(string src, string dist)
                    {
                        // ファイル置換
                        File.Delete(dist);
                        File.Move(src, dist);
                    },
                        output,
                        filepath);
                }
            }
        }