/platform/analysis-impl/src/com/intellij/codeInspection/ex/JsonSingleFileInspectionsReportConverter.java

http://github.com/JetBrains/intellij-community · Java · 96 lines · 82 code · 10 blank · 4 comment · 10 complexity · 2fc308df2936bb9bfebba5a166f4898b MD5 · raw file

  1. // Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
  2. package com.intellij.codeInspection.ex;
  3. import com.google.gson.Gson;
  4. import com.google.gson.GsonBuilder;
  5. import com.google.gson.stream.JsonWriter;
  6. import com.intellij.codeInspection.InspectionsResultUtil;
  7. import com.intellij.openapi.util.JDOMUtil;
  8. import com.intellij.openapi.util.io.FileUtil;
  9. import com.intellij.openapi.vfs.CharsetToolkit;
  10. import com.intellij.util.containers.ContainerUtil;
  11. import org.jdom.Document;
  12. import org.jdom.Element;
  13. import org.jdom.JDOMException;
  14. import org.jetbrains.annotations.NotNull;
  15. import org.jetbrains.annotations.Nullable;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.io.Writer;
  19. import java.nio.file.Files;
  20. import java.nio.file.Paths;
  21. import java.util.HashSet;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Set;
  25. /**
  26. * @author yole
  27. */
  28. public class JsonSingleFileInspectionsReportConverter extends JsonInspectionsReportConverter {
  29. @Override
  30. public String getFormatName() {
  31. return "json-single-file";
  32. }
  33. @Override
  34. public void convert(@NotNull String rawDataDirectoryPath,
  35. @Nullable String outputPath,
  36. @NotNull Map<String, Tools> tools,
  37. @NotNull List<? extends File> inspectionsResults) throws ConversionException {
  38. if (outputPath == null) {
  39. throw new ConversionException("Output path isn't specified");
  40. }
  41. Gson gson = new GsonBuilder().setPrettyPrinting().create();
  42. try {
  43. try (Writer writer = Files.newBufferedWriter(Paths.get(outputPath), CharsetToolkit.UTF8_CHARSET);
  44. JsonWriter jsonWriter = gson.newJsonWriter(writer)) {
  45. jsonWriter.beginObject();
  46. File patches = ContainerUtil.find(inspectionsResults,
  47. (file) -> FileUtil.getNameWithoutExtension(file).equals("fixes"));
  48. if (patches != null) {
  49. Element patchesElement = JDOMUtil.loadDocument(patches).getRootElement();
  50. jsonWriter.name("patch").value(patchesElement.getTextTrim());
  51. jsonWriter.name("patchedFiles");
  52. jsonWriter.beginArray();
  53. for (Element patchedFile : patchesElement.getChildren("patchedFile")) {
  54. jsonWriter.beginObject();
  55. jsonWriter.name("path").value(patchedFile.getAttributeValue("path"));
  56. jsonWriter.name("content").value(patchedFile.getTextTrim());
  57. jsonWriter.endObject();
  58. }
  59. jsonWriter.endArray();
  60. }
  61. jsonWriter.name(PROBLEMS);
  62. jsonWriter.beginArray();
  63. Set<String> seenProblemIds = new HashSet<>();
  64. for (File result : inspectionsResults) {
  65. if (!FileUtil.getNameWithoutExtension(result).equals(InspectionsResultUtil.DESCRIPTIONS)) {
  66. seenProblemIds.add(FileUtil.getNameWithoutExtension(result));
  67. Document doc = JDOMUtil.loadDocument(result);
  68. for (Element problem : doc.getRootElement().getChildren(PROBLEM)) {
  69. convertProblem(jsonWriter, problem);
  70. }
  71. }
  72. }
  73. jsonWriter.endArray();
  74. File descriptionsFile = ContainerUtil.find(inspectionsResults,
  75. (file) -> FileUtil.getNameWithoutExtension(file).equals(InspectionsResultUtil.DESCRIPTIONS));
  76. if (descriptionsFile != null) {
  77. convertDescriptionsContents(jsonWriter, JDOMUtil.loadDocument(descriptionsFile), (id) -> seenProblemIds.contains(id));
  78. }
  79. jsonWriter.endObject();
  80. }
  81. }
  82. catch (IOException | JDOMException e) {
  83. throw new ConversionException("Cannot convert file: " + e.getMessage());
  84. }
  85. }
  86. }