66
77import java .io .File ;
88import java .io .IOException ;
9+ import java .net .URISyntaxException ;
10+ import java .nio .file .Path ;
11+ import java .nio .file .Paths ;
912import java .util .Map ;
13+ import java .util .Objects ;
1014
1115public class Parser {
1216
17+ // Используется в тестах (ищет в resources/fixtures)
18+ public static Map <String , Object > parseFromResources (String fileName ) throws IOException {
19+ ObjectMapper objectMapper ;
20+
21+ if (fileName .endsWith (".json" )) {
22+ objectMapper = new ObjectMapper ();
23+ } else if (fileName .endsWith (".yaml" ) || fileName .endsWith (".yml" )) {
24+ objectMapper = new ObjectMapper (new YAMLFactory ());
25+ } else {
26+ throw new IllegalArgumentException ("Unsupported file format: " + fileName );
27+ }
28+
29+ Path path ;
30+ try {
31+ path = Paths .get (Objects .requireNonNull (
32+ Parser .class .getClassLoader ().getResource ("fixtures/" + fileName )).toURI ());
33+ } catch (URISyntaxException e ) {
34+ throw new RuntimeException ("Invalid file path: " + fileName , e );
35+ }
36+
37+ return objectMapper .readValue (path .toFile (), new TypeReference <Map <String , Object >>() {});
38+ }
39+
40+ // Используется из CLI — напрямую с файловой системы
1341 public static Map <String , Object > parse (String filePath ) throws IOException {
1442 ObjectMapper objectMapper ;
1543
1644 if (filePath .endsWith (".json" )) {
1745 objectMapper = new ObjectMapper ();
1846 } else if (filePath .endsWith (".yaml" ) || filePath .endsWith (".yml" )) {
19- objectMapper = new ObjectMapper (new YAMLFactory ()); // Используем YAML парсер
47+ objectMapper = new ObjectMapper (new YAMLFactory ());
2048 } else {
2149 throw new IllegalArgumentException ("Unsupported file format: " + filePath );
2250 }
2351
24- return objectMapper .readValue (new File ("src/main/resources/fixtures/" + filePath ),
25- new TypeReference <Map <String , Object >>() { });
52+ return objectMapper .readValue (new File (filePath ), new TypeReference <Map <String , Object >>() {});
2653 }
2754}
55+
56+
57+
58+
59+
60+ //package hexlet.code;
61+ //
62+ //import com.fasterxml.jackson.core.type.TypeReference;
63+ //import com.fasterxml.jackson.databind.ObjectMapper;
64+ //import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
65+ //
66+ //import java.io.File;
67+ //import java.io.IOException;
68+ //import java.net.URISyntaxException;
69+ //import java.nio.file.Path;
70+ //import java.nio.file.Paths;
71+ //import java.util.Map;
72+ //import java.util.Objects;
73+ //
74+ //public class Parser {
75+ //
76+ // public static Map<String, Object> parse(String filePath) throws IOException {
77+ // ObjectMapper objectMapper;
78+ //
79+ // if (filePath.endsWith(".json")) {
80+ // objectMapper = new ObjectMapper();
81+ // } else if (filePath.endsWith(".yaml") || filePath.endsWith(".yml")) {
82+ // objectMapper = new ObjectMapper(new YAMLFactory()); // Используем YAML парсер
83+ // } else {
84+ // throw new IllegalArgumentException("Unsupported file format: " + filePath);
85+ // }
86+ //
87+ // Path path;
88+ // try {
89+ // path = Paths.get(Objects.requireNonNull(
90+ // Parser.class.getClassLoader().getResource("fixtures/" + filePath)).toURI());
91+ // } catch (URISyntaxException e) {
92+ // throw new RuntimeException("Invalid file path: " + filePath, e);
93+ // }
94+ //
95+ // return objectMapper.readValue(path.toFile(), new TypeReference<Map<String, Object>>() {});
96+ // }
97+ //}
98+
99+
100+
101+ // return objectMapper.readValue(new File("src/main/resources/fixtures/" + filePath),
102+ // new TypeReference<Map<String, Object>>() { });
0 commit comments