use of org.opendatakit.briefcase.util.BadFormDefinition in project briefcase by opendatakit.
the class BriefcaseFormDefinition method resolveAgainstBriefcaseDefn.
public static final BriefcaseFormDefinition resolveAgainstBriefcaseDefn(File tmpFormFile, boolean copyFile) throws BadFormDefinition {
if (!tmpFormFile.exists()) {
throw new BadFormDefinition("Form directory does not contain form");
}
// parse the temp file into a form definition...
boolean badForm = false;
JavaRosaParserWrapper newDefn;
File briefcaseFormDirectory;
File briefcaseFormFile;
try {
newDefn = new JavaRosaParserWrapper(tmpFormFile, readFile(tmpFormFile));
briefcaseFormDirectory = FileSystemUtils.getFormDirectory(newDefn.getFormName());
briefcaseFormFile = FileSystemUtils.getFormDefinitionFile(briefcaseFormDirectory);
} catch (ODKIncompleteSubmissionData e) {
log.warn("bad form definition", e);
try {
badForm = true;
newDefn = null;
briefcaseFormDirectory = FileSystemUtils.getFormDirectory("_badForm");
briefcaseFormFile = FileSystemUtils.getFormDefinitionFile(briefcaseFormDirectory);
} catch (FileSystemException ex) {
log.error("failed to establish storage location for bad form", e);
throw new BadFormDefinition(ex);
}
} catch (FileSystemException e) {
log.error("failed to establish storage location for form", e);
throw new BadFormDefinition(e);
}
boolean isIdentical = false;
boolean needsMediaUpdate = false;
File revised = new File(briefcaseFormFile.getParentFile(), briefcaseFormFile.getName() + ".revised");
String revisedXml = null;
JavaRosaParserWrapper revisedDefn = null;
// determine the most up-to-date existing definition...
JavaRosaParserWrapper existingDefn;
try {
if (revised.exists()) {
revisedXml = readFile(revised);
revisedDefn = new JavaRosaParserWrapper(revised, revisedXml);
}
if (!briefcaseFormFile.exists()) {
// Rename it to formFile and parse it.
if (copyFile) {
try {
FileUtils.copyFile(tmpFormFile, briefcaseFormFile);
} catch (IOException e) {
String msg = "Unable to copy form definition file into briefcase directory";
log.error(msg, e);
throw new BadFormDefinition(msg);
}
} else {
if (!tmpFormFile.renameTo(briefcaseFormFile)) {
// if cannot rename, try to copy instead (and mark for deletion)
try {
FileUtils.copyFile(tmpFormFile, briefcaseFormFile);
tmpFormFile.deleteOnExit();
} catch (IOException e) {
String msg = "Form directory does not contain form (can neither rename nor copy into briefcase directory)";
log.error(msg);
throw new BadFormDefinition(msg);
}
}
}
// weird if it does...
needsMediaUpdate = !revised.exists();
existingDefn = new JavaRosaParserWrapper(briefcaseFormFile, readFile(briefcaseFormFile));
} else {
// get the current existing definition...
String existingXml = readFile(briefcaseFormFile);
existingDefn = new JavaRosaParserWrapper(briefcaseFormFile, existingXml);
String existingTitle = existingDefn.getFormName();
// compare the two
DifferenceResult result;
if (badForm) {
// newDefn is considered identical to what we have locally...
result = DifferenceResult.XFORMS_IDENTICAL;
} else {
result = JavaRosaParserWrapper.compareXml(newDefn, existingXml, existingTitle, true);
}
if (result == DifferenceResult.XFORMS_DIFFERENT) {
if (revised.exists()) {
result = JavaRosaParserWrapper.compareXml(newDefn, revisedXml, revisedDefn.getFormName(), true);
if (result == DifferenceResult.XFORMS_DIFFERENT) {
throw new BadFormDefinition("Form definitions are incompatible.");
} else if (result != DifferenceResult.XFORMS_EARLIER_VERSION && result != DifferenceResult.XFORMS_MISSING_VERSION && result != DifferenceResult.XFORMS_IDENTICAL) {
if (copyFile) {
try {
FileUtils.copyFile(tmpFormFile, revised);
} catch (IOException e) {
String msg = "Unable to overwrite the '.revised' form definition file in briefcase storage";
log.error(msg, e);
throw new BadFormDefinition(msg);
}
} else {
if (!tmpFormFile.renameTo(revised)) {
// if cannot rename, try to copy instead (and mark for deletion)
try {
FileUtils.copyFile(tmpFormFile, revised);
tmpFormFile.deleteOnExit();
} catch (IOException e) {
String msg = "Form directory does not contain form (can neither rename nor copy into briefcase directory)";
log.error(msg, e);
throw new BadFormDefinition(msg);
}
}
}
needsMediaUpdate = true;
// and re-parse the new revised file (since we just updated it...)
revisedDefn = new JavaRosaParserWrapper(revised, readFile(revised));
} else if (result == DifferenceResult.XFORMS_IDENTICAL) {
// confirm that the media is up-to-date when the forms are
// identical
// allows briefcase to resume a form download when it failed
// during
// the early form-media-fetch phases.
isIdentical = true;
needsMediaUpdate = true;
}
} else {
throw new BadFormDefinition("Form definitions are incompatible.");
}
} else if (result != DifferenceResult.XFORMS_EARLIER_VERSION && result != DifferenceResult.XFORMS_MISSING_VERSION && result != DifferenceResult.XFORMS_IDENTICAL) {
if (!revised.exists()) {
// overwrite everything and re-parse the new file.
if (copyFile) {
try {
FileUtils.copyFile(tmpFormFile, briefcaseFormFile);
} catch (IOException e) {
String msg = "Unable to overwrite form definition file in briefcase storage";
log.error(msg, e);
throw new BadFormDefinition(msg);
}
} else {
if (!tmpFormFile.renameTo(briefcaseFormFile)) {
// if cannot rename, try to copy instead (and mark for deletion)
try {
FileUtils.copyFile(tmpFormFile, briefcaseFormFile);
tmpFormFile.deleteOnExit();
} catch (IOException e) {
String msg = "Form directory does not contain form (can neither rename nor copy into briefcase directory)";
log.error(msg, e);
throw new BadFormDefinition(msg);
}
}
}
needsMediaUpdate = true;
// and re-parse the new form file (since we just updated it...)
existingXml = readFile(briefcaseFormFile);
existingDefn = new JavaRosaParserWrapper(briefcaseFormFile, existingXml);
}
} else if (result == DifferenceResult.XFORMS_IDENTICAL) {
// if a revised form exists, we assume the media is up-to-date in that
// folder. Otherwise, confirm that the media is up-to-date when the
// forms are identical. This allows briefcase to resume a form
// download
// when it failed during the early form-media-fetch phases.
isIdentical = true;
needsMediaUpdate = !revised.exists();
}
}
} catch (ODKIncompleteSubmissionData e) {
throw new BadFormDefinition(e, e.getReason());
}
BriefcaseFormDefinition defn;
if (revised.exists()) {
defn = new BriefcaseFormDefinition(briefcaseFormDirectory, revisedDefn, revised, needsMediaUpdate);
} else {
defn = new BriefcaseFormDefinition(briefcaseFormDirectory, existingDefn, null, needsMediaUpdate);
}
if (!isIdentical && needsMediaUpdate) {
EventBus.publish(new UpdatedBriefcaseFormDefinitionEvent(defn));
}
return defn;
}
use of org.opendatakit.briefcase.util.BadFormDefinition in project briefcase by opendatakit.
the class BriefcaseFormDefinition method readFile.
private static final String readFile(File formDefinitionFile) throws BadFormDefinition {
StringBuilder xmlBuilder = new StringBuilder();
BufferedReader rdr = null;
try {
rdr = new BufferedReader(new InputStreamReader(new FileInputStream(formDefinitionFile), "UTF-8"));
String line = rdr.readLine();
while (line != null) {
xmlBuilder.append(line);
line = rdr.readLine();
}
} catch (FileNotFoundException e) {
throw new BadFormDefinition("Form not found");
} catch (IOException e) {
throw new BadFormDefinition("Unable to read form");
} finally {
if (rdr != null) {
try {
rdr.close();
} catch (IOException e) {
log.warn("failed to close reader", e);
}
}
}
String inputXml = xmlBuilder.toString();
return inputXml;
}
use of org.opendatakit.briefcase.util.BadFormDefinition in project briefcase by opendatakit.
the class OdkCollectFormDefinition method readFile.
private static final String readFile(File formDefinitionFile) throws BadFormDefinition {
StringBuilder xmlBuilder = new StringBuilder();
try (BufferedReader rdr = new BufferedReader(new InputStreamReader(new FileInputStream(formDefinitionFile), "UTF-8"))) {
String line = rdr.readLine();
while (line != null) {
xmlBuilder.append(line);
line = rdr.readLine();
}
} catch (FileNotFoundException e) {
throw new BadFormDefinition("Form not found");
} catch (IOException e) {
throw new BadFormDefinition("Unable to read form");
}
return xmlBuilder.toString();
}
Aggregations