package edu.hm.hafner.analysis.parser;

import java.util.Optional;
import java.util.stream.Stream;

import org.json.JSONException;
import org.json.JSONObject;

import edu.hm.hafner.analysis.Issue;
import edu.hm.hafner.analysis.ParsingException;
import edu.hm.hafner.analysis.ReaderFactory;
import edu.hm.hafner.analysis.Report;

/**
 * Parser for logs in JSON format.
 *
 * @author Jeremie Bresson
 */
public class JsonLogParser extends JsonBaseParser {
    private static final long serialVersionUID = 1349282064371959197L;

    @Override
    public boolean accepts(final ReaderFactory readerFactory) {
        String fileName = readerFactory.getFileName();
        return !fileName.endsWith(".xml") && !fileName.endsWith(".json");
    }

    @Override
    public Report parse(final ReaderFactory readerFactory) throws ParsingException {
        try (Stream<String> lines = readerFactory.readStream()) {
            Report report = new Report();
            lines.map(String::trim)
                .filter(line -> !line.isEmpty())
                .filter(line -> !line.startsWith("//"))
                .filter(line -> line.charAt(0) != '#')
                .map(line -> parseIssue(line, report))
                .filter(Optional::isPresent)
                .map(Optional::get)
                .forEach(report::add);
            return report;
        }
    }

    private Optional<Issue> parseIssue(final String line, final Report report) {
        try {
            JSONObject jsonIssue = new JSONObject(line);
            return convertToIssue(jsonIssue);
        }
        catch (JSONException e) {
            report.logException(e, "Could not parse line: «%s»", line);
            return Optional.empty();
        }
    }
}

@@ -984,32 +984,36 @@
                 
+    
 .filter(line -%3E 
@@ -1037,32 +1037,36 @@
                 
+   
+ 
 .filter(line -%3E 
@@ -1097,32 +1097,36 @@
                 
+    
 .filter(line -%3E 
@@ -1160,24 +1160,28 @@
             
+  
+  
 .map(line -%3E
@@ -1215,32 +1215,36 @@
                 
+    
 .filter(Optional
@@ -1268,24 +1268,28 @@
             
+ 
+   
 .map(Optiona
@@ -1296,16 +1296,20 @@
 l::get)%0A
+    
         

Fix indention.