Search in sources :

Example 1 with MediaFile

use of org.opendatakit.briefcase.util.ServerFetcher.MediaFile in project briefcase by opendatakit.

the class XmlManipulationUtils method parseFormManifestResponse.

public static final List<MediaFile> parseFormManifestResponse(boolean isOpenRosaResponse, Document doc) throws ParsingException {
    List<MediaFile> files = new ArrayList<>();
    if (!isOpenRosaResponse) {
        log.error("Manifest reply doesn't report an OpenRosa version -- bad server?");
        throw new ParsingException(BAD_NOT_OPENROSA_MANIFEST);
    }
    // Attempt OpenRosa 1.0 parsing
    Element manifestElement = doc.getRootElement();
    if (!manifestElement.getName().equals("manifest")) {
        log.error("Root element is not <manifest> -- was " + manifestElement.getName());
        throw new ParsingException(BAD_NOT_OPENROSA_MANIFEST);
    }
    String namespace = manifestElement.getNamespace();
    if (!isXformsManifestNamespacedElement(manifestElement)) {
        log.error("Root element Namespace is incorrect: " + namespace);
        throw new ParsingException(BAD_NOT_OPENROSA_MANIFEST);
    }
    int nElements = manifestElement.getChildCount();
    for (int i = 0; i < nElements; ++i) {
        if (manifestElement.getType(i) != Element.ELEMENT) {
            // e.g., whitespace (text)
            continue;
        }
        Element mediaFileElement = (Element) manifestElement.getElement(i);
        if (!isXformsManifestNamespacedElement(mediaFileElement)) {
            // someone else's extension?
            continue;
        }
        String name = mediaFileElement.getName();
        if (name.equalsIgnoreCase("mediaFile")) {
            String filename = null;
            String hash = null;
            String downloadUrl = null;
            // don't process descriptionUrl
            int childCount = mediaFileElement.getChildCount();
            for (int j = 0; j < childCount; ++j) {
                if (mediaFileElement.getType(j) != Element.ELEMENT) {
                    // e.g., whitespace (text)
                    continue;
                }
                Element child = mediaFileElement.getElement(j);
                if (!isXformsManifestNamespacedElement(child)) {
                    // someone else's extension?
                    continue;
                }
                String tag = child.getName();
                if (tag.equals("filename")) {
                    filename = XFormParser.getXMLText(child, true);
                    if (filename != null && filename.length() == 0) {
                        filename = null;
                    }
                } else if (tag.equals("hash")) {
                    hash = XFormParser.getXMLText(child, true);
                    if (hash != null && hash.length() == 0) {
                        hash = null;
                    }
                } else if (tag.equals("downloadUrl")) {
                    downloadUrl = XFormParser.getXMLText(child, true);
                    if (downloadUrl != null && downloadUrl.length() == 0) {
                        downloadUrl = null;
                    }
                }
            }
            if (filename == null || downloadUrl == null || hash == null) {
                log.error("Manifest entry " + Integer.toString(i) + " is missing one or more tags: filename, hash, or downloadUrl");
                throw new ParsingException(BAD_NOT_OPENROSA_MANIFEST);
            }
            files.add(new MediaFile(filename, hash, downloadUrl));
        }
    }
    return files;
}
Also used : MediaFile(org.opendatakit.briefcase.util.ServerFetcher.MediaFile) ParsingException(org.opendatakit.briefcase.model.ParsingException) Element(org.kxml2.kdom.Element) ArrayList(java.util.ArrayList)

Example 2 with MediaFile

use of org.opendatakit.briefcase.util.ServerFetcher.MediaFile in project briefcase by opendatakit.

the class XmlManipulationUtils method parseDownloadSubmissionResponse.

public static final SubmissionManifest parseDownloadSubmissionResponse(Document doc) throws ParsingException {
    // and parse the document...
    List<MediaFile> attachmentList = new ArrayList<>();
    Element rootSubmissionElement = null;
    String instanceID = null;
    // Attempt parsing
    Element submissionElement = doc.getRootElement();
    if (!submissionElement.getName().equals("submission")) {
        String msg = "Parsing downloadSubmission reply -- root element is not <submission> :" + submissionElement.getName();
        log.error(msg);
        throw new ParsingException(msg);
    }
    String namespace = submissionElement.getNamespace();
    if (!namespace.equalsIgnoreCase(NAMESPACE_OPENDATAKIT_ORG_SUBMISSIONS)) {
        String msg = "Parsing downloadSubmission reply -- root element namespace is incorrect:" + namespace;
        log.error(msg);
        throw new ParsingException(msg);
    }
    int nElements = submissionElement.getChildCount();
    for (int i = 0; i < nElements; ++i) {
        if (submissionElement.getType(i) != Element.ELEMENT) {
            // e.g., whitespace (text)
            continue;
        }
        Element subElement = (Element) submissionElement.getElement(i);
        namespace = subElement.getNamespace();
        if (!namespace.equalsIgnoreCase(NAMESPACE_OPENDATAKIT_ORG_SUBMISSIONS)) {
            // someone else's extension?
            continue;
        }
        String name = subElement.getName();
        if (name.equalsIgnoreCase("data")) {
            // find the root submission element and get its instanceID attribute
            int nIdElements = subElement.getChildCount();
            for (int j = 0; j < nIdElements; ++j) {
                if (subElement.getType(j) != Element.ELEMENT) {
                    // e.g., whitespace (text)
                    continue;
                }
                rootSubmissionElement = (Element) subElement.getElement(j);
                break;
            }
            if (rootSubmissionElement == null) {
                throw new ParsingException("no submission body found in submissionDownload response");
            }
            instanceID = rootSubmissionElement.getAttributeValue(null, "instanceID");
            if (instanceID == null) {
                throw new ParsingException("instanceID attribute value is null");
            }
        } else if (name.equalsIgnoreCase("mediaFile")) {
            int nIdElements = subElement.getChildCount();
            String filename = null;
            String hash = null;
            String downloadUrl = null;
            for (int j = 0; j < nIdElements; ++j) {
                if (subElement.getType(j) != Element.ELEMENT) {
                    // e.g., whitespace (text)
                    continue;
                }
                Element mediaSubElement = (Element) subElement.getElement(j);
                name = mediaSubElement.getName();
                if (name.equalsIgnoreCase("filename")) {
                    filename = XFormParser.getXMLText(mediaSubElement, true);
                } else if (name.equalsIgnoreCase("hash")) {
                    hash = XFormParser.getXMLText(mediaSubElement, true);
                } else if (name.equalsIgnoreCase("downloadUrl")) {
                    downloadUrl = XFormParser.getXMLText(mediaSubElement, true);
                }
            }
            attachmentList.add(new MediaFile(filename, hash, downloadUrl));
        } else {
            log.warn("Unrecognized tag inside submission: " + name);
        }
    }
    if (rootSubmissionElement == null) {
        throw new ParsingException("No submission body found");
    }
    if (instanceID == null) {
        throw new ParsingException("instanceID attribute value is null");
    }
    // write submission to a string
    StringWriter fo = new StringWriter();
    KXmlSerializer serializer = new KXmlSerializer();
    serializer.setOutput(fo);
    // setting the response content type emits the xml header.
    // just write the body here...
    // this has the xmlns of the submissions download, indicating that it
    // originated from a briefcase download. Might be useful for discriminating
    // real vs. recovered data?
    rootSubmissionElement.setPrefix(null, NAMESPACE_OPENDATAKIT_ORG_SUBMISSIONS);
    try {
        rootSubmissionElement.write(serializer);
        serializer.flush();
        serializer.endDocument();
        fo.close();
    } catch (IOException e) {
        String msg = "Unexpected IOException";
        log.error(msg, e);
        throw new ParsingException(msg + ": " + e.getMessage());
    }
    return new SubmissionManifest(instanceID, fo.toString(), attachmentList);
}
Also used : MediaFile(org.opendatakit.briefcase.util.ServerFetcher.MediaFile) StringWriter(java.io.StringWriter) Element(org.kxml2.kdom.Element) ParsingException(org.opendatakit.briefcase.model.ParsingException) ArrayList(java.util.ArrayList) SubmissionManifest(org.opendatakit.briefcase.util.ServerFetcher.SubmissionManifest) IOException(java.io.IOException) KXmlSerializer(org.kxml2.io.KXmlSerializer)

Aggregations

ArrayList (java.util.ArrayList)2 Element (org.kxml2.kdom.Element)2 ParsingException (org.opendatakit.briefcase.model.ParsingException)2 MediaFile (org.opendatakit.briefcase.util.ServerFetcher.MediaFile)2 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 KXmlSerializer (org.kxml2.io.KXmlSerializer)1 SubmissionManifest (org.opendatakit.briefcase.util.ServerFetcher.SubmissionManifest)1