Skip to content
This repository has been archived by the owner on Apr 16, 2022. It is now read-only.

Commit

Permalink
Merge pull request #884 from lognaturel/issue-874
Browse files Browse the repository at this point in the history
  • Loading branch information
lognaturel authored Oct 28, 2020
2 parents 98fb98e + 04216f8 commit fbd9fd3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
4 changes: 1 addition & 3 deletions src/org/opendatakit/briefcase/export/ExportToCsv.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import org.bushe.swing.event.EventBus;
import org.opendatakit.briefcase.model.FormStatus;
import org.opendatakit.briefcase.model.form.FormMetadata;
Expand Down Expand Up @@ -143,14 +142,13 @@ private static ExportOutcome export(FormMetadataPort formMetadataPort, FormMetad
}

private static SubmissionExportErrorCallback buildParsingErrorCallback(Path errorsDir) {
AtomicInteger errorSeq = new AtomicInteger(1);
// Remove errors from a previous export attempt
if (exists(errorsDir))
deleteRecursive(errorsDir);
return (path, message) -> {
if (!exists(errorsDir))
createDirectories(errorsDir);
copy(path, errorsDir.resolve("failed_submission_" + errorSeq.getAndIncrement() + ".xml"));
copy(path, errorsDir.resolve("failed_submission_" + path.getParent().getFileName() + ".xml"));
log.warn("A submission has been excluded from the export output due to some problem ({}). If you didn't expect this, please ask for support at https://forum.getodk.org/c/support", message);
};
}
Expand Down
29 changes: 12 additions & 17 deletions src/org/opendatakit/briefcase/export/SubmissionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,12 @@ public static List<Path> getListOfSubmissionFiles(FormMetadata formMetadata, For
.forEach(instanceDir -> {
Path submissionFile = instanceDir.resolve("submission.xml");
try {
Optional<OffsetDateTime> submissionDate = readSubmissionDate(submissionFile, onParsingError);
Optional<OffsetDateTime> submissionDate = readSubmissionDate(submissionFile);
paths.add(Pair.of(submissionFile, submissionDate.orElse(OffsetDateTime.of(1970, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC))));
} catch (Throwable t) {
log.error("Can't read submission date", t);
log.error("Parse error attempting to read instance date", t);
EventBus.publish(ExportEvent.failureSubmission(formDef, instanceDir.getFileName().toString(), t));
onParsingError.accept(submissionFile, "Parse error attempting to read instance date");
}
});
return paths.parallelStream()
Expand Down Expand Up @@ -152,32 +153,26 @@ public static Optional<Submission> parseSubmission(Path path, boolean isEncrypte
});
}

private static Optional<OffsetDateTime> readSubmissionDate(Path path, SubmissionExportErrorCallback onParsingError) {
private static Optional<OffsetDateTime> readSubmissionDate(Path path) throws XMLStreamException {
try (InputStream is = Files.newInputStream(path);
InputStreamReader isr = new InputStreamReader(is, UTF_8)) {
return parseAttribute(path, isr, "submissionDate", onParsingError)
return parseAttribute(isr, "submissionDate")
.map(Iso8601Helpers::parseDateTime);
} catch (IOException e) {
throw new CryptoException("Can't decrypt file", e);
}
}

private static Optional<String> parseAttribute(Path submission, Reader ioReader, String attributeName, SubmissionExportErrorCallback onParsingError) {
try {
XMLStreamReader reader = xmlInputFactory.createXMLStreamReader(ioReader);
while (reader.hasNext())
if (reader.next() == START_ELEMENT)
for (int i = 0, c = reader.getAttributeCount(); i < c; ++i)
if (reader.getAttributeLocalName(i).equals(attributeName))
return Optional.of(reader.getAttributeValue(i));
} catch (XMLStreamException e) {
log.error("Can't parse submission", e);
onParsingError.accept(submission, "parsing error");
}
private static Optional<String> parseAttribute(Reader ioReader, String attributeName) throws XMLStreamException {
XMLStreamReader reader = xmlInputFactory.createXMLStreamReader(ioReader);
while (reader.hasNext())
if (reader.next() == START_ELEMENT)
for (int i = 0, c = reader.getAttributeCount(); i < c; ++i)
if (reader.getAttributeLocalName(i).equals(attributeName))
return Optional.of(reader.getAttributeValue(i));
return Optional.empty();
}


private static Optional<Submission> decrypt(Submission submission, SubmissionExportErrorCallback onError) {
List<Path> mediaPaths = submission.getMediaPaths();

Expand Down

0 comments on commit fbd9fd3

Please sign in to comment.