Skip to content

Commit eec8f84

Browse files
committed
added and fixed recomendations
1 parent 160931b commit eec8f84

File tree

4 files changed

+30
-23
lines changed

4 files changed

+30
-23
lines changed

app/src/main/java/hexlet/code/App.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
@Command(name = "gendiff", mixinStandardHelpOptions = true, version = "gendiff 1.1.1",
1010
description = "Compares two configuration files and shows a difference.")
11-
1211
class App implements Runnable {
1312

1413
@Option(names = { "-f", "--format" }, paramLabel = "format",
@@ -30,14 +29,7 @@ public void run() {
3029
} catch (IOException e) {
3130
System.err.println("Ошибка при чтении файла: " + e.getMessage());
3231
} catch (IllegalArgumentException e) {
33-
if (e.getMessage().contains("Unsupported file format")) {
34-
System.err.println("Ошибка: неподдерживаемый формат файла. Поддерживаемые форматы: "
35-
+ ".json, .yaml, .yml.");
36-
} else if (e.getMessage().contains("Invalid file path")) {
37-
System.err.println("Ошибка: неверный путь к файлу. Проверьте правильность пути.");
38-
} else {
39-
System.err.println("Ошибка: " + e.getMessage());
40-
}
32+
System.err.println("Ошибка: " + e.getMessage());
4133
} catch (Exception e) {
4234
System.err.println("Произошла ошибка: " + e.getMessage());
4335
}

app/src/main/java/hexlet/code/Differ.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,26 @@
99
import java.nio.file.Paths;
1010

1111
public class Differ {
12+
1213
public static String generate(String data1, String data2, String format) throws IOException {
1314

15+
if ((!Files.exists(Paths.get(data1))) || (!Files.exists(Paths.get(data2)))) {
16+
throw new IllegalArgumentException("Ошибка: файл не существует");
17+
}
18+
1419
String content1 = new String(Files.readAllBytes(Paths.get(data1)));
1520
String content2 = new String(Files.readAllBytes(Paths.get(data2)));
1621

17-
Map<String, Object> fileInfo1 = Parser.parseFromString(content1);
18-
Map<String, Object> fileInfo2 = Parser.parseFromString(content2);
22+
String fileName1 = Paths.get(data1).getFileName().toString();
23+
String fileName2 = Paths.get(data2).getFileName().toString();
24+
25+
if (!isSupportedFormat(data1) || !isSupportedFormat(data2)) {
26+
throw new IllegalArgumentException("Ошибка: неподдерживаемый формат файла. "
27+
+ "Поддерживаемые форматы: .json, .yaml, .yml.");
28+
}
29+
30+
Map<String, Object> fileInfo1 = Parser.parseFromString(content1, fileName1);
31+
Map<String, Object> fileInfo2 = Parser.parseFromString(content2, fileName2);
1932

2033
List<Map<String, Object>> diff = DifferCalculation.calculateDiff(fileInfo1, fileInfo2);
2134

@@ -25,6 +38,9 @@ public static String generate(String data1, String data2, String format) throws
2538
// без формата (по умолчанию "stylish")
2639
public static String generate(String data1, String data2) throws IOException {
2740
return generate(data1, data2, "stylish");
41+
}
2842

43+
public static boolean isSupportedFormat(String filePath) {
44+
return filePath.endsWith(".json") || filePath.endsWith(".yaml") || filePath.endsWith(".yml");
2945
}
3046
}

app/src/main/java/hexlet/code/DifferCalculation.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
package hexlet.code;
22

3-
import java.util.ArrayList;
4-
import java.util.HashMap;
5-
import java.util.List;
6-
import java.util.Map;
7-
import java.util.Objects;
8-
import java.util.Set;
9-
import java.util.TreeSet;
10-
3+
import java.util.*;
114

125
public class DifferCalculation {
136

app/src/main/java/hexlet/code/Parser.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,23 @@
77
import java.io.IOException;
88
import java.util.Map;
99

10-
1110
public class Parser {
1211

13-
public static Map<String, Object> parseFromString(String content) throws IOException {
12+
public static Map<String, Object> parseFromString(String content, String fileName) throws IOException {
1413
ObjectMapper objectMapper;
1514

16-
if (content.trim().startsWith("{") || content.trim().startsWith("[")) {
15+
// если JSON
16+
if (fileName.endsWith(".json")) {
1717
objectMapper = new ObjectMapper(); // JSON
18-
} else {
18+
}
19+
// если YAML
20+
else if (fileName.endsWith(".yaml") || fileName.endsWith(".yml")) {
1921
objectMapper = new ObjectMapper(new YAMLFactory()); // YAML
2022
}
23+
// Если формат не поддерживается
24+
else {
25+
throw new IllegalArgumentException("Ошибка: формат неясен");
26+
}
2127

2228
return objectMapper.readValue(content, new TypeReference<Map<String, Object>>() { });
2329
}

0 commit comments

Comments
 (0)