Advertisement
mahitsy

LogParser

Jan 19th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.96 KB | None | 0 0
  1. package my.company;
  2.  
  3. import java.math.BigInteger;
  4. import java.util.*;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7.  
  8. public class Main {
  9.  
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.  
  13.         String regex = "(\"([\\w\\W\\s\\S]+?)\")(: )(\\[\"([\\W\\w\\s\\S]+?)\"\\])";
  14.         Pattern pattern = Pattern.compile(regex);
  15.         Matcher matcher;
  16.  
  17.         HashMap<String, HashMap<String, List<String>>> result = new HashMap<>();
  18.         while (true) {
  19.             String input = scanner.nextLine().trim();
  20.             if (input.equals("END")) {
  21.                 break;
  22.             }
  23.  
  24.             String projectName = "";
  25.             String type = "";
  26.             String message = "";
  27.             matcher = pattern.matcher(input);
  28.  
  29.             while (matcher.find()) {
  30.                 switch (matcher.group(2)) {
  31.                     case "Project":
  32.                         projectName = matcher.group(5);
  33.                         break;
  34.                     case "Type":
  35.                         type = matcher.group(5);
  36.                         break;
  37.                     case "Message":
  38.                         message = matcher.group(5);
  39.                         break;
  40.                 }
  41.             }
  42.  
  43.             HashMap<String, List<String>> map;
  44.             List<String> list;
  45.             if (!result.containsKey(projectName)) {
  46.                 map = new HashMap<>();
  47.                 map.put("Critical", new ArrayList<String>());
  48.                 map.put("Warning", new ArrayList<String>());
  49.                 result.put(projectName, map);
  50.             } else {
  51.                 map = result.get(projectName);
  52.             }
  53.  
  54.             list = map.get(type);
  55.             list.add(message);
  56.         }
  57.  
  58.         scanner.close();
  59.         List<Map.Entry<String, HashMap<String, List<String>>>> sorted = new ArrayList<>();
  60.         for (Map.Entry<String, HashMap<String, List<String>>> entry : result
  61.                 .entrySet()) {
  62.             sorted.add(entry);
  63.         }
  64.  
  65.         sorted.sort(new Comparator<Map.Entry<String, HashMap<String, List<String>>>>() {
  66.  
  67.             @Override
  68.             public int compare(Map.Entry<String, HashMap<String, List<String>>> o1,
  69.                                Map.Entry<String, HashMap<String, List<String>>> o2) {
  70.                 int totalO1 = o1.getValue().get("Critical").size()
  71.                         + o1.getValue().get("Warning").size();
  72.                 int totalO2 = o2.getValue().get("Critical").size()
  73.                         + o2.getValue().get("Warning").size();
  74.  
  75.                 int cmp = Integer.compare(totalO2, totalO1);
  76.                 if (cmp == 0) {
  77.                     cmp = o1.getKey().compareTo(o2.getKey());
  78.                 }
  79.  
  80.                 return cmp;
  81.             }
  82.  
  83.         });
  84.  
  85.         Comparator<String> listSorter = new Comparator<String>() {
  86.  
  87.             @Override
  88.             public int compare(String o1, String o2) {
  89.                 int lengthO1 = o1.length();
  90.                 int lengthO2 = o2.length();
  91.  
  92.                 int cmp = Integer.compare(lengthO1, lengthO2);
  93.                 if (cmp == 0) {
  94.                     cmp = o1.compareTo(o2);
  95.                 }
  96.  
  97.                 return cmp;
  98.             }
  99.  
  100.         };
  101.  
  102.         StringBuilder bld = new StringBuilder();
  103.         for (int i = 0; i < sorted.size(); i++) {
  104.             Map.Entry<String, HashMap<String, List<String>>> entry = sorted
  105.                     .get(i);
  106.             List<String> criticalList = entry.getValue().get("Critical");
  107.             List<String> warningList = entry.getValue().get("Warning");
  108.  
  109.             int criticalCount = entry.getValue().get("Critical").size();
  110.             int warningCount = entry.getValue().get("Warning").size();
  111.             criticalList.sort(listSorter);
  112.             warningList.sort(listSorter);
  113.  
  114.             bld.append(String.format("%s:\n", entry.getKey()));
  115.             bld.append(String.format("Total Errors: %d\n", criticalCount
  116.                     + warningCount));
  117.             bld.append(String.format("Critical: %d\n", criticalCount));
  118.             bld.append(String.format("Warnings: %d\n", warningCount));
  119.             bld.append("Critical Messages:\n");
  120.             if (criticalCount == 0) {
  121.                 bld.append("--->None\n");
  122.             } else {
  123.                 for (int b = 0; b < criticalCount; b++) {
  124.                     bld.append(String.format("--->%s\n", criticalList.get(b)));
  125.                 }
  126.             }
  127.             bld.append("Warning Messages:\n");
  128.             if (warningCount == 0) {
  129.                 bld.append("--->None\n");
  130.             } else {
  131.                 for (int b = 0; b < warningCount; b++) {
  132.                     bld.append(String.format("--->%s\n", warningList.get(b)));
  133.                 }
  134.             }
  135.  
  136.             if (i != sorted.size() - 1) {
  137.                 bld.append("\n");
  138.             }
  139.         }
  140.  
  141.         System.out.println(bld.toString());
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement