简体   繁体   English

仅从每个Java文件的顶部删除多行注释

[英]Remove Multiline Comments only from Top of Every Java File

We once used borland starteam tool (one of the kind of revision/source code control system like mercurial) for our code management. 我们曾经使用borland starteam工具(一种类似Mercurial的修订/源代码控制系统之一)进行代码管理。 Whenever we commit the code, the tool itself puts a description of the commit at the top of the file. 每当我们提交代码时,工具本身都会在文件顶部放置对提交的描述。 So now we have many classes in the code where at the top of each file. 所以现在我们在每个文件顶部的代码中都有许多类。 For example: 例如:

/*This is some developer comment at the top of the file*/

/*
 * $Log:
 *  1   Client Name 1.0   07/11/2012 16:28:54  Umair Khalid did something
 *  2   Client Name 1.0   07/11/2012 16:28:54  Umair Khalid again did 
 *                                             something
 * $
 */

public class ABC
{
  /*This is just a variable*/
  int a = 0;
  public int method1()
  {
  }
}

Now i am planning to remove all this starteam type of the code which is present at the top of each file. 现在,我计划删除每个文件顶部显示的所有starteam类型代码。 But i dont want to remove any other comment from any file or any other copyright comment at the top. 但是我不想从任何文件或顶部的任何其他版权评论中删除任何其他评论。 I only want to remove that chunk that starts with $Log and ends with $. 我只想删除以$ Log开始并以$结尾的块。 I have looked at other questions as well related to this problem but this is a multiline comment. 我已经看过其他与此问题相关的问题,但这是多行注释。 Would regular expression be good option for this? 正则表达式将是一个很好的选择吗?

Is there any utility i can use rather then writing my own code to remove this? 有什么我可以使用的实用程序,而不是编写我自己的代码来删除的?

If regular expression is the only quick solution, then i am stuck in there. 如果正则表达式是唯一的快速解决方案,那么我就会陷入困境。

Any help would be appreciated. 任何帮助,将不胜感激。

If the format is exactly as you show, you could build a fragile little state machine that looks like this. 如果格式完全符合您的显示格式,则可以构建一个像这样的易碎小状态机。

Start with an enum to track the state: 从枚举开始跟踪状态:

enum ParseState
{
    Normal,
    MayBeInMultiLineComment,    //occurs after initial /*
    InMultilineComment,
}

and then add this code: 然后添加以下代码:

     public static void CommentStripper()
     {
         var text = @"/*This is some developer comment at the top of the file*/
/*
 * $Log:
 *  1   Client Name 1.0   07/11/2012 16:28:54  Umair Khalid did something
 *  2   Client Name 1.0   07/11/2012 16:28:54  Umair Khalid again did 
 *                                             something
 * $
 */

/*
    This is not a log entry
*/

public class ABC
{
  /*This is just a variable*/
  int a = 0;
  public int method1()
  {
  }
}";

    //this next line could be File.ReadAllLines to get the text from a file
    //or you could read from a stream, line by line.

    var lines = text.Split(new[] {"\r\n"}, StringSplitOptions.None);

    var buffer = new StringBuilder();
    ParseState parseState = ParseState.Normal;
    string lastLine = string.Empty;

    foreach (var line in lines)
    {
        if (parseState == ParseState.Normal)
        {
            if (line == "/*")
            {
                lastLine = line;
                parseState = ParseState.MayBeInMultiLineComment;
            }
            else
            {
                buffer.AppendLine(line);
            }
        }
        else if (parseState == ParseState.MayBeInMultiLineComment)
        {
            if (line == " * $Log:")
            {
                parseState = ParseState.InMultilineComment;
            }
            else
            {
                parseState = ParseState.Normal;
                buffer.AppendLine(lastLine);
                buffer.AppendLine(line);
            }
            lastLine = string.Empty;
        }
        else if (parseState == ParseState.InMultilineComment)
        {
            if (line == " */")
            {
                parseState = ParseState.Normal;
            }
        }

    }
    //you could do what you want with the string, I'm just going to write it out to the debugger console.
    Debug.Write(buffer.ToString());
}

Note the lastLine is used because you need to read-ahead one line to pick up whether a comment is a log entry or not (which is what the MayBeInMultiLineComment state tracks). 请注意,使用lastLine是因为您需要lastLine读一行以选择注释是否为日志条目( MayBeInMultiLineComment状态跟踪的内容)。

The output from that looks like: 该输出看起来像:

/*This is some developer comment at the top of the file*/


/*
    This is not a log entry
*/

public class ABC
{
  /*This is just a variable*/
  int a = 0;
  public int method1()
  {
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM