Created
January 18, 2011 23:44
-
-
Save presidentbeef/785402 to your computer and use it in GitHub Desktop.
Java to parse output from Brakeman
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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