Skip to content

Instantly share code, notes, and snippets.

@presidentbeef
Created January 18, 2011 23:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save presidentbeef/785402 to your computer and use it in GitHub Desktop.
Save presidentbeef/785402 to your computer and use it in GitHub Desktop.
Java to parse output from Brakeman
/* Parse tab-separated output from Brakeman
* Use:
* brakeman -o example.tabs
* java BrakemanScanner example.tabs
*/
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.io.RandomAccessFile;
public class BrakemanScanner {
private static Pattern pattern = Pattern.compile("^([^\t]+?)\t(\\d+)\t([\\w\\s]+?)\t(\\w+)\t([\\w\\s]+?)\t(High|Medium|Weak)", Pattern.MULTILINE);
public static void main(String[] argv) {
new BrakemanScanner().scan(argv[0]);
}
public void scan(String filename) {
System.err.println("Pattern: " + this.pattern);
Matcher m = this.pattern.matcher(readFile(filename));
while(m.find()) {
System.out.println("File: " + m.group(1));
System.out.println("Line number: " + m.group(2));
System.out.println("Type: " + m.group(3));
System.out.println("Category: " + m.group(4));
System.out.println("Message: " + m.group(5));
System.out.println("Confidence: " + m.group(6));
System.out.println("----------------------------");
}
}
public String readFile(String filename) {
try {
RandomAccessFile f = new RandomAccessFile(filename, "r");
byte[] b = new byte[(int)f.length()];
f.readFully(b);
System.err.println("Read file of length " + b.length);
return new String(b);
}
catch(Exception e) {
System.err.println(e);
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment