Skip to content

Commit 882b192

Browse files
committed
create empty projects for non-parseable project files
Fixes #49
1 parent 5fde7e3 commit 882b192

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

src/main/java/de/vogel612/helper/data/util/ProjectSerializer.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
import de.vogel612.helper.data.ResourceSet;
55
import org.jdom2.Document;
66
import org.jdom2.Element;
7+
import org.jdom2.JDOMException;
78

89
import java.io.IOException;
910
import java.nio.file.Path;
1011
import java.nio.file.Paths;
12+
import java.util.ArrayList;
1113
import java.util.List;
1214
import java.util.Objects;
1315
import java.util.Set;
@@ -68,14 +70,22 @@ private static Element getRoot(String name) {
6870
* @return A Project-instance that's equivalent to the Project instance originally serialized into the file
6971
*/
7072
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+
}
7989
return new Project(projectName, declaredResources);
8090
}
8191

src/main/java/de/vogel612/helper/data/util/Serialization.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,8 @@ public class Serialization {
3232
* @throws IllegalStateException
3333
* When the file could not be parsed into a document
3434
*/
35-
public static Document parseFile(final Path file) throws IOException {
36-
final Path xmlFile = file.getFileName();
37-
final SAXBuilder documentBuilder = new SAXBuilder();
38-
39-
try {
40-
return documentBuilder.build(file.toFile());
41-
} catch (JDOMException e) {
42-
throw new IllegalStateException("Unable to parse " + xmlFile, e);
43-
}
35+
public static Document parseFile(final Path file) throws IOException, JDOMException {
36+
return new SAXBuilder().build(file.toFile());
4437
}
4538

4639
/**

0 commit comments

Comments
 (0)