Caller Program :
String filePath = request.getParameter("fileName");
System.out.println(" File Path to filename..." +
filePath);
SopFinder sf = new SopFinder();
File f = new File(filePath);
if (!f.exists()){
System.out.println(" Not a valid project
path ....");
} else {
String sb = sf.findSOP(filePath);
sf.clearSOPList();
System.out.println(" parseed list....." + sb);
..............
.................
Main Program :
package com.vels.util;
/**
* Finout the SOPs in directory
* @author Velmruugan Pousel
* @date 10 november 2010
* @version 0.1
*/
import java.io.File;
import java.io.FileReader;
import java.io.LineNumberReader;
public class SopFinder {
File file = null;
FileReader freader = null;
LineNumberReader lnreader = null;
public static StringBuffer sopSB = new StringBuffer();
public String findSOP(String sFilePath){
readFile(sFilePath);
return sopSB.toString();
}
public StringBuffer readFile(String sFilePath) {
StringBuffer sb = new StringBuffer();
File oFile = new File(sFilePath);
// check if its subfolder or java class
if (oFile.isDirectory()) {
File[] aFiles = oFile.listFiles();
for (File oFileCur : aFiles) {
readFile(oFileCur.getAbsolutePath());
}
}
// restrict other file extensions to be parsed
// only java files
if (sFilePath.endsWith(".java")) {
sb = parseSOP(oFile);
sopSB.append(sb);
}
return sb;
}
/**
* method to parse the java class to findout the
* System.out .print usage
*/
public StringBuffer parseSOP(File file) {
StringBuffer sb = new StringBuffer();
try {
sb.append("\n \n").append("File Name :").append(
file.getAbsolutePath()).append("\n
\n");
freader = new FileReader(file);
lnreader = new LineNumberReader(freader);
String line = "";
while ((line = lnreader.readLine()) != null) {
int lineNum = lnreader.getLineNumber();
if (line.indexOf("System.out.println") > 0) {
sb.append("-------->Line
number: ").append(lineNum)
.append(": ").append
(line.trim()).append("\n");
}
}
// sb.append("\n \n");
} catch (Throwable e) {
e.printStackTrace();
}
return sb;
}
// to clear the output buffer after the result display
public void clearSOPList() {
sopSB = new StringBuffer();
}
}
No comments:
Post a Comment