package edu.umd.cs.findbugs;


import java.util.*;

public class FindExamples {

	public static void main(String[] argv) throws Exception {
		if (argv.length < 1) {
			System.err.println("Usage: " + FindExamples.class.getName() + " <results file>");
			System.exit(1);
		}

		DetectorFactoryCollection.instance(); // load plugins

		for (int i = 0; i < argv.length; ++i)
			scan(argv[i]);
	}

	public static void scan(String filename) throws Exception {

		BugCollection bugCollection = new SortedBugCollection();
		bugCollection.readXML(filename, new Project());

		Iterator<BugInstance> i = bugCollection.iterator();
		while (i.hasNext()) {
			BugInstance bugInstance = i.next();
			String annotation = bugInstance.getAnnotationText();
			if (annotation.equals(""))
				continue;

			Set<String> contents = parseAnnotation(annotation);

			if (contents.contains("GOOD_EXAMPLE") || contents.contains("EXCELLENT_EXAMPLE"))
				dumpBug(bugInstance, filename);
		}

	}

	private static void dumpBug(BugInstance bugInstance, String filename) {
		System.out.println("In " + filename);
		System.out.println(bugInstance.getMessage());
		System.out.println("\t" + bugInstance.getAbbrev());
		SourceLineAnnotation srcLine = bugInstance.getPrimarySourceLineAnnotation();
		if (srcLine != null)
			System.out.println("\t" + srcLine.toString());
		System.out.println(bugInstance.getAnnotationText());
	}

	private static Set<String> parseAnnotation(String annotation) {
		HashSet<String> result = new HashSet<String>();
		StringTokenizer tok = new StringTokenizer(annotation, " \t\r\n\f");
		while (tok.hasMoreTokens())
			result.add(tok.nextToken());
		return result;
	}
}

// vim:ts=4

@@ -222,17 +222,53 @@
 Name() +
- 
+%0A%09%09%09%09%22 %5B-category %3Ccategory%3E%5D%22 +%0A%09%09%09%09
 %22 %3Cresul
@@ -360,16 +360,138 @@
 lugins%0A%0A
+%09%09int start = 0;%0A%09%09String category = null;%0A%09%09if (argv%5B0%5D.equals(%22-category%22)) %7B%0A%09%09%09category = argv%5B1%5D;%0A%09%09%09start = 2;%0A%09%09%7D%0A%0A
 %09%09for (i
@@ -497,17 +497,21 @@
 int i = 
-0
+start
 ; i %3C ar
@@ -541,16 +541,26 @@
 (argv%5Bi%5D
+, category
 );%0A%09%7D%0A%0A%09
@@ -598,16 +598,33 @@
 filename
+, String category
 ) throws
@@ -965,16 +965,101 @@
 tinue;%0A%0A
+%09%09%09if (category != null && !bugInstance.getAbbrev().equals(category))%0A%09%09%09%09continue;%0A%0A
 %09%09%09Set%3CS

Allow specification of category