Skip to content

Commit

Permalink
Update Differ.java
Browse files Browse the repository at this point in the history
Rename methods
Optimize getFileExtension method
Add xml support files (with a small working workaround)
  • Loading branch information
rostex committed Nov 10, 2024
1 parent 0892f85 commit ca6a85e
Showing 1 changed file with 56 additions and 25 deletions.
81 changes: 56 additions & 25 deletions app/src/main/java/hexlet/code/Differ.java
Original file line number Diff line number Diff line change
@@ -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<Data> getDiff(Map<String, Object> content1, Map<String, Object> content2) {
Expand All @@ -31,11 +31,20 @@ public static List<Data> getDiff(Map<String, Object> content1, Map<String, Objec
sortedKeys.addAll(content1.keySet());
sortedKeys.addAll(content2.keySet());


for (var key : sortedKeys) {
var value1 = content1.get(key);
var value2 = content2.get(key);

// This is a small working workaround: if value is an empty string, we convert it to null.
// This happens because empty XML tags (like <key/>) 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)) {
Expand All @@ -49,24 +58,46 @@ public static List<Data> getDiff(Map<String, Object> content1, Map<String, Objec
return data;
}

public static Map getData(String filePath) throws Exception {
public static Map getDataFromFile(String filePath) throws UnsupportedFileFormatException, IOException {
String fileExtension = getFileExtension(filePath);
File file = new File(filePath);
YAMLMapper yamlMapper = new YAMLMapper();
ObjectMapper objectMapper = new ObjectMapper();
XmlMapper xmlMapper = new XmlMapper();

if (getFileFormat(filePath).equals("yml") || getFileFormat(filePath).equals("yaml")) {
return yamlMapper.readValue(file, Map.class);
} else if (getFileFormat(filePath).equals("json")) {
return objectMapper.readValue(file, Map.class);
} else {
throw new Exception(filePath + " format is not supported");
switch (fileExtension) {
case "yml":
case "yaml":
return yamlMapper.readValue(file, Map.class);
case "json":
return objectMapper.readValue(file, Map.class);
case "xml":
return xmlMapper.readValue(file, Map.class);
default:
throw new UnsupportedFileFormatException(filePath);
}

}

public static String getFileFormat(String filePath) {
int indexOfDoT = filePath.lastIndexOf(".");
return filePath.substring(indexOfDoT + 1);
public static String getFileExtension(String fileName) throws UnsupportedFileFormatException {
int indexOfDoT = fileName.lastIndexOf(".");
if (indexOfDoT == -1) {
throw new UnsupportedFileFormatException(fileName);
}

String extension = fileName.substring(indexOfDoT + 1);

boolean validExtension = false;
for (Extension ext : Extension.values()) {
if (ext.toString().equals(extension)) {
validExtension = true;
break;
}
}

if (!validExtension) {
throw new UnsupportedFileFormatException(fileName);
}
return extension;
}

private static boolean getEqualsData(Object value1, Object value2) {
Expand Down

0 comments on commit ca6a85e

Please sign in to comment.