|
4 | 4 | import de.vogel612.helper.data.ResourceSet; |
5 | 5 | import org.jdom2.Document; |
6 | 6 | import org.jdom2.Element; |
| 7 | +import org.jdom2.JDOMException; |
7 | 8 |
|
8 | 9 | import java.io.IOException; |
9 | 10 | import java.nio.file.Path; |
10 | 11 | import java.nio.file.Paths; |
| 12 | +import java.util.ArrayList; |
11 | 13 | import java.util.List; |
12 | 14 | import java.util.Objects; |
13 | 15 | import java.util.Set; |
@@ -68,14 +70,22 @@ private static Element getRoot(String name) { |
68 | 70 | * @return A Project-instance that's equivalent to the Project instance originally serialized into the file |
69 | 71 | */ |
70 | 72 | public static Project deserialize(Path file) throws IOException { |
71 | | - Document projectDocument = Serialization.parseFile(file); |
72 | | - Element root = projectDocument.getRootElement(); |
73 | | - final String projectName = root.getAttribute("name").getValue(); |
74 | | - List<ResourceSet> declaredResources = root.getChildren().stream() |
75 | | - .map(el -> ProjectSerializer.deserializeResourceSet(el, file)) |
76 | | - .filter(Objects::nonNull) |
77 | | - .collect(Collectors.toList()); |
78 | | - |
| 73 | + String projectName; |
| 74 | + List<ResourceSet> declaredResources; |
| 75 | + try { |
| 76 | + Document projectDocument = Serialization.parseFile(file); |
| 77 | + Element root = projectDocument.getRootElement(); |
| 78 | + projectName = root.getAttribute("name").getValue(); |
| 79 | + declaredResources = root.getChildren().stream() |
| 80 | + .map(el -> ProjectSerializer.deserializeResourceSet(el, file)) |
| 81 | + .filter(Objects::nonNull) |
| 82 | + .collect(Collectors.toList()); |
| 83 | + } catch (JDOMException e) { |
| 84 | + // probably an empty file |
| 85 | + // log a warning |
| 86 | + projectName = file.getFileName().toString().replace(".thp", ""); |
| 87 | + declaredResources = new ArrayList<>(); |
| 88 | + } |
79 | 89 | return new Project(projectName, declaredResources); |
80 | 90 | } |
81 | 91 |
|
|
0 commit comments