use of org.opendatakit.briefcase.model.CannotFixXMLException in project briefcase by opendatakit.
the class BadXMLFixer method fixBadXML.
public static Document fixBadXML(File xmlFile) throws CannotFixXMLException {
log.info("Trying to fix the submission {} ", xmlFile.getAbsolutePath());
try {
String originalXML = FileUtils.readFileToString(xmlFile, ENCODING);
String fixedXML = fixXML(originalXML);
File tempFile = File.createTempFile(xmlFile.getName(), ".fixed.xml");
FileUtils.writeStringToFile(tempFile, fixedXML, ENCODING);
return XmlManipulationUtils.parseXml(tempFile);
} catch (IOException | ParsingException | FileSystemException e) {
log.error("Cannot fix xml", e);
throw new CannotFixXMLException("Cannot fix xml", e);
}
}
use of org.opendatakit.briefcase.model.CannotFixXMLException in project briefcase by opendatakit.
the class XmlManipulationUtils method parseXml.
public static Document parseXml(File submission) throws ParsingException, FileSystemException {
// parse the xml document...
Document doc = null;
try {
InputStream is = null;
InputStreamReader isr = null;
try {
is = new FileInputStream(submission);
isr = new InputStreamReader(is, UTF_8);
Document tempDoc = new Document();
KXmlParser parser = new KXmlParser();
parser.setInput(isr);
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
tempDoc.parse(parser);
isr.close();
doc = tempDoc;
} finally {
if (isr != null) {
try {
isr.close();
} catch (Exception e) {
// no-op
}
}
if (is != null) {
try {
is.close();
} catch (Exception e) {
// no-op
}
}
}
} catch (XmlPullParserException e) {
try {
return BadXMLFixer.fixBadXML(submission);
} catch (CannotFixXMLException e1) {
File debugFileLocation = new File(new StorageLocation().getBriefcaseFolder(), "debug");
try {
if (!debugFileLocation.exists()) {
FileUtils.forceMkdir(debugFileLocation);
}
long checksum = FileUtils.checksumCRC32(submission);
File debugFile = new File(debugFileLocation, "submission-" + checksum + ".xml");
FileUtils.copyFile(submission, debugFile);
} catch (IOException e2) {
throw new RuntimeException(e2);
}
throw new ParsingException("Failed during parsing of submission Xml: " + e.toString());
}
} catch (IOException e) {
throw new FileSystemException("Failed while reading submission xml: " + e.toString());
}
return doc;
}
use of org.opendatakit.briefcase.model.CannotFixXMLException in project briefcase by opendatakit.
the class BadXMLFixer method fixXML.
protected static String fixXML(String originalXML) throws CannotFixXMLException {
// try to find the name of the root element, that is the formId
int startIndex = XML_HEADER.length() + 1;
int endIndex = originalXML.indexOf(" ", startIndex);
String formId = originalXML.substring(startIndex, endIndex);
log.warn("Trying to fix a submission of the form: " + formId);
// try to see if the last part is a part of the form id
int idClosingTagIndex = originalXML.lastIndexOf("</");
if (idClosingTagIndex == -1) {
throw new CannotFixXMLException("Cannot find a single closing tag in this file!");
}
String lastPart = originalXML.substring(idClosingTagIndex + 2);
if (lastPart.equals("") || (formId + ">").startsWith(lastPart)) {
// this is the easy case just fill in the rest of the id.
return originalXML.substring(0, idClosingTagIndex) + "</" + formId + ">";
} else if (("meta></" + formId + ">").startsWith(lastPart)) {
// that means that perhaps the entire closing tag is missing, let's give it a try
return originalXML.substring(0, idClosingTagIndex) + "</meta></" + formId + ">";
} else {
throw new CannotFixXMLException("Cannot understand where this file was truncated");
}
}
Aggregations