Search in sources :

Example 1 with CannotFixXMLException

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);
    }
}
Also used : CannotFixXMLException(org.opendatakit.briefcase.model.CannotFixXMLException) FileSystemException(org.opendatakit.briefcase.model.FileSystemException) ParsingException(org.opendatakit.briefcase.model.ParsingException) IOException(java.io.IOException) File(java.io.File)

Example 2 with CannotFixXMLException

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;
}
Also used : CannotFixXMLException(org.opendatakit.briefcase.model.CannotFixXMLException) InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Document(org.kxml2.kdom.Document) FileInputStream(java.io.FileInputStream) FileSystemException(org.opendatakit.briefcase.model.FileSystemException) CannotFixXMLException(org.opendatakit.briefcase.model.CannotFixXMLException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) MetadataUpdateException(org.opendatakit.briefcase.model.MetadataUpdateException) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) ParsingException(org.opendatakit.briefcase.model.ParsingException) KXmlParser(org.kxml2.io.KXmlParser) FileSystemException(org.opendatakit.briefcase.model.FileSystemException) ParsingException(org.opendatakit.briefcase.model.ParsingException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) StorageLocation(org.opendatakit.briefcase.ui.StorageLocation) MediaFile(org.opendatakit.briefcase.util.ServerFetcher.MediaFile) File(java.io.File)

Example 3 with CannotFixXMLException

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");
    }
}
Also used : CannotFixXMLException(org.opendatakit.briefcase.model.CannotFixXMLException)

Aggregations

CannotFixXMLException (org.opendatakit.briefcase.model.CannotFixXMLException)3 File (java.io.File)2 IOException (java.io.IOException)2 FileSystemException (org.opendatakit.briefcase.model.FileSystemException)2 ParsingException (org.opendatakit.briefcase.model.ParsingException)2 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 MalformedURLException (java.net.MalformedURLException)1 KXmlParser (org.kxml2.io.KXmlParser)1 Document (org.kxml2.kdom.Document)1 MetadataUpdateException (org.opendatakit.briefcase.model.MetadataUpdateException)1 StorageLocation (org.opendatakit.briefcase.ui.StorageLocation)1 MediaFile (org.opendatakit.briefcase.util.ServerFetcher.MediaFile)1 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)1