From ca6a85e41d6bcd081f11b79ffe72f93ee7f41749 Mon Sep 17 00:00:00 2001 From: rostex Date: Sun, 10 Nov 2024 13:52:39 +0300 Subject: [PATCH] Update Differ.java Rename methods Optimize getFileExtension method Add xml support files (with a small working workaround) --- app/src/main/java/hexlet/code/Differ.java | 81 ++++++++++++++++------- 1 file changed, 56 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/hexlet/code/Differ.java b/app/src/main/java/hexlet/code/Differ.java index 38e52fa..a8e6aef 100644 --- a/app/src/main/java/hexlet/code/Differ.java +++ b/app/src/main/java/hexlet/code/Differ.java @@ -1,28 +1,28 @@ package hexlet.code; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.xml.XmlMapper; import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; -import hexlet.code.formatter.Formatter; +import hexlet.code.formatter.Extension; +import hexlet.code.formatter.Format; +import hexlet.code.exceptions.UnsupportedFileFormatException; + import java.io.File; -import java.util.List; -import java.util.ArrayList; -import java.util.Map; -import java.util.Set; -import java.util.TreeSet; +import java.io.IOException; +import java.util.*; public class Differ { - - public static String generate(String filePath1, String filePath2, String format) throws Exception { - var content1 = getData(filePath1); - var content2 = getData(filePath2); + public static String generateDiff(String filePath1, String filePath2, Format formatType) throws Exception { + var content1 = getDataFromFile(filePath1); + var content2 = getDataFromFile(filePath2); var dataList = Differ.getDiff(content1, content2); - return Formatter.format(dataList, format); + return formatType.getFormat(dataList); } - public static String generate(String filePath1, String filePath2) throws Exception { - return generate(filePath1, filePath2, "stylish"); + public static String generateDiff(String filePath1, String filePath2) throws Exception { + return generateDiff(filePath1, filePath2, Format.STYLISH); } public static List getDiff(Map content1, Map content2) { @@ -31,11 +31,20 @@ public static List getDiff(Map content1, Map) are deserialized as empty strings, + // but we want to treat them as null in the diffing process. + if (value1 != null && value1.equals("")) { + value1 = null; + } + if (value2 != null && value2.equals("")) { + value2 = null; + } + if (!content1.containsKey(key)) { data.add(new Data(key, null, value2, Data.Status.ADDED)); } else if (!content2.containsKey(key)) { @@ -49,24 +58,46 @@ public static List getDiff(Map content1, Map