我正在创建一个解析实用程序,以读取(重复的)Java源代码文件,并使用在JavaDoc块中某些位置插入的一些额外信息来重新输出该文件。
第一步是弄清楚每个JavaDoc块的开始和结束位置。下面是我为此编写的代码。
希望对您有所帮助。
这将获取Java源代码文件的路径,并输出每个找到的JavaDoc块的开始和结束行。
import java.io.File;
import java.io.IOException;
import java.util.regex.Pattern;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
import xbn.io.RTIOException;
/**
<P>Prints the start and end line-numbers for all JavaDoc blocks in a Java source-code file. The one and only parameter is the path to the file. This assumes that the JavaDoc open-comment (slash-asterisk-asterisk) is the first non-whitespace on its line. The end comment (asterisk-slash) may be anywhere on the line.</P>
java PrintJDBlocksStartStopLineNumsXmpl C:\java_code\PrintJDBlocksStartStopLineNumsXmpl.java
**/
public class PrintJDBlocksStartStopLineNumsXmpl {
/**
<P>The main function.</P>
**/
public static final void main(String[] as_1RqdJavaSourcePath) {
//Read command-line parameter
String sJPath = null;
try {
sJPath = as_1RqdJavaSourcePath[0];
} catch(ArrayIndexOutOfBoundsException aibx) {
throw new NullPointerException("Missing one-and-only required parameter: Path to java source-code file.");
}
System.out.println("Java source: " + sJPath);
//Establish line-iterator
LineIterator li = null;
try {
li = FileUtils.lineIterator(new File(sJPath)); //Throws npx if null
} catch(IOException iox) {
throw new RTIOException("PrintJDBlocksStartStopLinesXmpl", iox);
}
Pattern pTrmdJDBlockStart = Pattern.compile("^[\\t ]*/\\*\\*");
String sDD = "..";
int iLn = 1;
boolean bInJDBlock = false;
while(li.hasNext()) {
String sLn = li.nextLine();
if(!bInJDBlock) {
if(pTrmdJDBlockStart.matcher(sLn).matches()) {
bInJDBlock = true;
System.out.print(iLn + sDD);
}
} else if(sLn.indexOf("*/") != -1) {
bInJDBlock = false;
System.out.println(iLn);
}
iLn++;
}
if(bInJDBlock) {
throw new IllegalStateException("Reach end of file. JavaDoc not closed.");
}
}
/**
<P>Another one</P>
**/
private static final void oneMoreForGoodMeasure() {
}
}
输出:
[C:\java_code]java PrintJDBlocksStartStopLineNumsXmpl C:\java_code\PrintJDBlocksStartStopLineNumsXmpl.java
Java source: C:\java_code\PrintJDBlocksStartStopLineNumsXmpl.java
8..12
14..16
54..56
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句