package com.kiuwan.importer.parser;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import com.kiuwan.importer.beans.File;
import com.kiuwan.importer.beans.Rule;
import com.kiuwan.importer.beans.Violation;

public class InferReportParser implements ReportParser {
	
	private final String RULECODE_PREXIX = "CUS.INFER.JAVA.";
	
	
	Collection<Violation> defects = new ArrayList<Violation>();
	Map<String, Rule> rules = new HashMap<String, Rule>();
	

	@Override
	public Collection<Violation> getDefects() {
		return defects;
	}

	@Override
	public void parse(String inputFile) {
		
		JSONParser parser = new JSONParser();
		
		try {
			 
			Object obj = parser.parse(new FileReader(inputFile));
	 
			JSONArray bugs = (JSONArray) obj;
	 
			Iterator<JSONObject> iterator = bugs.iterator();
			while (iterator.hasNext()) {
				JSONObject bug = iterator.next();
				String file = (String) bug.get("file");
				Long line = (Long) bug.get("line");
				String bugType = (String) bug.get("bug_type");
				String procedure = (String) bug.get("procedure");
				String qualifier = (String) bug.get("qualifier");
				String bugMessage = procedure + ": " + qualifier;
				// JSONArray bug_trace = (JSONArray) bug.get("bug_trace");
					
				createDefect(file, bugType, line, bugMessage);
			}
	 
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
	}

	private void createDefect(String filePath, String defectName, Long line, String code) {
		
		String normalizedDefectName = defectName.toLowerCase();
		normalizedDefectName = normalizedDefectName.replace("/", ".");
		
		String ruleCode = RULECODE_PREXIX + normalizedDefectName;
		
		if (!rules.containsKey(ruleCode)) {
			rules.put(ruleCode, new Rule(ruleCode));
		}
		
		File file = new File(line.intValue(), filePath, code);
		defects.add(new Violation(file, rules.get(ruleCode)));
		
	}
	
}

@@ -651,17 +651,45 @@
 FER.
-JAVA.%22;%0A%09
+%22;%0A%09private String language = %22java%22;
 %0A%09%0A%09
@@ -804,17 +804,107 @@
 ule%3E();%0A
-
 %09
+%0A%09public InferReportParser(String language) %7B%0A%09%09this.language = language.toUpperCase();%0A%09%7D
 %0A%0A%09@Over
@@ -2166,16 +2166,38 @@
 PREXIX +
+ this.language + %22.%22 +
  normali

Support for the -language parameter