Search in sources :

Example 6 with Element

use of org.kxml2.kdom.Element in project collect by opendatakit.

the class FileUtils method parseXML.

public static HashMap<String, String> parseXML(File xmlFile) {
    final HashMap<String, String> fields = new HashMap<String, String>();
    final InputStream is;
    try {
        is = new FileInputStream(xmlFile);
    } catch (FileNotFoundException e1) {
        Timber.d(e1);
        throw new IllegalStateException(e1);
    }
    InputStreamReader isr;
    try {
        isr = new InputStreamReader(is, "UTF-8");
    } catch (UnsupportedEncodingException uee) {
        Timber.w(uee, "Trying default encoding as UTF 8 encoding unavailable");
        isr = new InputStreamReader(is);
    }
    final Document doc;
    try {
        doc = XFormParser.getXMLDocument(isr);
    } catch (IOException e) {
        Timber.e(e, "Unable to parse XML document %s", xmlFile.getAbsolutePath());
        throw new IllegalStateException("Unable to parse XML document", e);
    } finally {
        try {
            isr.close();
        } catch (IOException e) {
            Timber.w("%s error closing from reader", xmlFile.getAbsolutePath());
        }
    }
    final String xforms = "http://www.w3.org/2002/xforms";
    final String html = doc.getRootElement().getNamespace();
    final Element head = doc.getRootElement().getElement(html, "head");
    final Element title = head.getElement(html, "title");
    if (title != null) {
        fields.put(TITLE, XFormParser.getXMLText(title, true));
    }
    final Element model = getChildElement(head, "model");
    Element cur = getChildElement(model, "instance");
    final int idx = cur.getChildCount();
    int i;
    for (i = 0; i < idx; ++i) {
        if (cur.isText(i)) {
            continue;
        }
        if (cur.getType(i) == Node.ELEMENT) {
            break;
        }
    }
    if (i < idx) {
        // this is the first data element
        cur = cur.getElement(i);
        final String id = cur.getAttributeValue(null, "id");
        final String version = cur.getAttributeValue(null, "version");
        final String uiVersion = cur.getAttributeValue(null, "uiVersion");
        if (uiVersion != null) {
            // pre-OpenRosa 1.0 variant of spec
            Timber.e("Obsolete use of uiVersion -- IGNORED -- only using version: %s", version);
        }
        fields.put(FORMID, (id == null) ? cur.getNamespace() : id);
        fields.put(VERSION, (version == null) ? null : version);
    } else {
        throw new IllegalStateException(xmlFile.getAbsolutePath() + " could not be parsed");
    }
    try {
        final Element submission = model.getElement(xforms, "submission");
        final String base64RsaPublicKey = submission.getAttributeValue(null, "base64RsaPublicKey");
        final String autoDelete = submission.getAttributeValue(null, "auto-delete");
        final String autoSend = submission.getAttributeValue(null, "auto-send");
        fields.put(SUBMISSIONURI, submission.getAttributeValue(null, "action"));
        fields.put(BASE64_RSA_PUBLIC_KEY, (base64RsaPublicKey == null || base64RsaPublicKey.trim().length() == 0) ? null : base64RsaPublicKey.trim());
        fields.put(AUTO_DELETE, autoDelete);
        fields.put(AUTO_SEND, autoSend);
    } catch (Exception e) {
        Timber.i("XML file %s does not have a submission element", xmlFile.getAbsolutePath());
    // and that's totally fine.
    }
    return fields;
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Element(org.kxml2.kdom.Element) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Document(org.kxml2.kdom.Document) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 7 with Element

use of org.kxml2.kdom.Element in project collect by opendatakit.

the class DownloadFormListTask method downloadMediaFileList.

private List<MediaFile> downloadMediaFileList(String manifestUrl) {
    if (manifestUrl == null) {
        return null;
    }
    // get shared HttpContext so that authentication and cookies are retained.
    HttpContext localContext = Collect.getInstance().getHttpContext();
    HttpClient httpclient = WebUtils.createHttpClient(WebUtils.CONNECTION_TIMEOUT);
    DocumentFetchResult result = WebUtils.getXmlDocument(manifestUrl, localContext, httpclient);
    if (result.errorMessage != null) {
        return null;
    }
    String errMessage = Collect.getInstance().getString(R.string.access_error, manifestUrl);
    if (!result.isOpenRosaResponse) {
        errMessage += Collect.getInstance().getString(R.string.manifest_server_error);
        Timber.e(errMessage);
        return null;
    }
    // Attempt OpenRosa 1.0 parsing
    Element manifestElement = result.doc.getRootElement();
    if (!manifestElement.getName().equals("manifest")) {
        errMessage += Collect.getInstance().getString(R.string.root_element_error, manifestElement.getName());
        Timber.e(errMessage);
        return null;
    }
    String namespace = manifestElement.getNamespace();
    if (!DownloadFormsTask.isXformsManifestNamespacedElement(manifestElement)) {
        errMessage += Collect.getInstance().getString(R.string.root_namespace_error, namespace);
        Timber.e(errMessage);
        return null;
    }
    int elements = manifestElement.getChildCount();
    List<MediaFile> files = new ArrayList<>();
    for (int i = 0; i < elements; ++i) {
        if (manifestElement.getType(i) != Element.ELEMENT) {
            // e.g., whitespace (text)
            continue;
        }
        Element mediaFileElement = manifestElement.getElement(i);
        if (!DownloadFormsTask.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 (!DownloadFormsTask.isXformsManifestNamespacedElement(child)) {
                    // someone else's extension?
                    continue;
                }
                String tag = child.getName();
                switch(tag) {
                    case "filename":
                        filename = XFormParser.getXMLText(child, true);
                        if (filename != null && filename.length() == 0) {
                            filename = null;
                        }
                        break;
                    case "hash":
                        hash = XFormParser.getXMLText(child, true);
                        if (hash != null && hash.length() == 0) {
                            hash = null;
                        }
                        break;
                    case "downloadUrl":
                        downloadUrl = XFormParser.getXMLText(child, true);
                        if (downloadUrl != null && downloadUrl.length() == 0) {
                            downloadUrl = null;
                        }
                        break;
                }
            }
            if (filename == null || downloadUrl == null || hash == null) {
                errMessage += Collect.getInstance().getString(R.string.manifest_tag_error, Integer.toString(i));
                Timber.e(errMessage);
                return null;
            }
            files.add(new MediaFile(filename, hash, downloadUrl));
        }
    }
    return files;
}
Also used : MediaFile(org.odk.collect.android.logic.MediaFile) DocumentFetchResult(org.odk.collect.android.utilities.DocumentFetchResult) HttpClient(org.opendatakit.httpclientandroidlib.client.HttpClient) Element(org.kxml2.kdom.Element) HttpContext(org.opendatakit.httpclientandroidlib.protocol.HttpContext) ArrayList(java.util.ArrayList)

Example 8 with Element

use of org.kxml2.kdom.Element in project collect by opendatakit.

the class DownloadFormsTask method downloadManifestAndMediaFiles.

private String downloadManifestAndMediaFiles(String tempMediaPath, String finalMediaPath, FormDetails fd, int count, int total) throws Exception {
    if (fd.getManifestUrl() == null) {
        return null;
    }
    publishProgress(Collect.getInstance().getString(R.string.fetching_manifest, fd.getFormName()), String.valueOf(count), String.valueOf(total));
    List<MediaFile> files = new ArrayList<MediaFile>();
    // get shared HttpContext so that authentication and cookies are retained.
    HttpContext localContext = Collect.getInstance().getHttpContext();
    HttpClient httpclient = WebUtils.createHttpClient(WebUtils.CONNECTION_TIMEOUT);
    DocumentFetchResult result = WebUtils.getXmlDocument(fd.getManifestUrl(), localContext, httpclient);
    if (result.errorMessage != null) {
        return result.errorMessage;
    }
    String errMessage = Collect.getInstance().getString(R.string.access_error, fd.getManifestUrl());
    if (!result.isOpenRosaResponse) {
        errMessage += Collect.getInstance().getString(R.string.manifest_server_error);
        Timber.e(errMessage);
        return errMessage;
    }
    // Attempt OpenRosa 1.0 parsing
    Element manifestElement = result.doc.getRootElement();
    if (!manifestElement.getName().equals("manifest")) {
        errMessage += Collect.getInstance().getString(R.string.root_element_error, manifestElement.getName());
        Timber.e(errMessage);
        return errMessage;
    }
    String namespace = manifestElement.getNamespace();
    if (!isXformsManifestNamespacedElement(manifestElement)) {
        errMessage += Collect.getInstance().getString(R.string.root_namespace_error, namespace);
        Timber.e(errMessage);
        return errMessage;
    }
    int elements = manifestElement.getChildCount();
    for (int i = 0; i < elements; ++i) {
        if (manifestElement.getType(i) != Element.ELEMENT) {
            // e.g., whitespace (text)
            continue;
        }
        Element mediaFileElement = 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();
                switch(tag) {
                    case "filename":
                        filename = XFormParser.getXMLText(child, true);
                        if (filename != null && filename.length() == 0) {
                            filename = null;
                        }
                        break;
                    case "hash":
                        hash = XFormParser.getXMLText(child, true);
                        if (hash != null && hash.length() == 0) {
                            hash = null;
                        }
                        break;
                    case "downloadUrl":
                        downloadUrl = XFormParser.getXMLText(child, true);
                        if (downloadUrl != null && downloadUrl.length() == 0) {
                            downloadUrl = null;
                        }
                        break;
                }
            }
            if (filename == null || downloadUrl == null || hash == null) {
                errMessage += Collect.getInstance().getString(R.string.manifest_tag_error, Integer.toString(i));
                Timber.e(errMessage);
                return errMessage;
            }
            files.add(new MediaFile(filename, hash, downloadUrl));
        }
    }
    // OK we now have the full set of files to download...
    Timber.i("Downloading %d media files.", files.size());
    int mediaCount = 0;
    if (files.size() > 0) {
        File tempMediaDir = new File(tempMediaPath);
        File finalMediaDir = new File(finalMediaPath);
        FileUtils.checkMediaPath(tempMediaDir);
        FileUtils.checkMediaPath(finalMediaDir);
        for (MediaFile toDownload : files) {
            ++mediaCount;
            publishProgress(Collect.getInstance().getString(R.string.form_download_progress, fd.getFormName(), String.valueOf(mediaCount), String.valueOf(files.size())), String.valueOf(count), String.valueOf(total));
            // try {
            File finalMediaFile = new File(finalMediaDir, toDownload.getFilename());
            File tempMediaFile = new File(tempMediaDir, toDownload.getFilename());
            if (!finalMediaFile.exists()) {
                downloadFile(tempMediaFile, toDownload.getDownloadUrl());
            } else {
                String currentFileHash = FileUtils.getMd5Hash(finalMediaFile);
                String downloadFileHash = getMd5Hash(toDownload.getHash());
                if (currentFileHash != null && downloadFileHash != null && !currentFileHash.contentEquals(downloadFileHash)) {
                    // if the hashes match, it's the same file
                    // otherwise delete our current one and replace it with the new one
                    FileUtils.deleteAndReport(finalMediaFile);
                    downloadFile(tempMediaFile, toDownload.getDownloadUrl());
                } else {
                    // exists, and the hash is the same
                    // no need to download it again
                    Timber.i("Skipping media file fetch -- file hashes identical: %s", finalMediaFile.getAbsolutePath());
                }
            }
        // } catch (Exception e) {
        // return e.getLocalizedMessage();
        // }
        }
    }
    return null;
}
Also used : MediaFile(org.odk.collect.android.logic.MediaFile) DocumentFetchResult(org.odk.collect.android.utilities.DocumentFetchResult) HttpClient(org.opendatakit.httpclientandroidlib.client.HttpClient) Element(org.kxml2.kdom.Element) ArrayList(java.util.ArrayList) HttpContext(org.opendatakit.httpclientandroidlib.protocol.HttpContext) MediaFile(org.odk.collect.android.logic.MediaFile) File(java.io.File)

Example 9 with Element

use of org.kxml2.kdom.Element in project javarosa by opendatakit.

the class ChildProcessingTest method recognizesThatOneChildIsATemplate.

@Test
public void recognizesThatOneChildIsATemplate() {
    Element el = new Element();
    Element child1 = el.createElement(null, "Child Name");
    child1.setAttribute(NAMESPACE_JAVAROSA, "template", "");
    el.addChild(ELEMENT, child1);
    el.addChild(ELEMENT, el.createElement(null, "Child Name"));
    assertFalse(XFormParser.childOptimizationsOk(el));
}
Also used : Element(org.kxml2.kdom.Element) Test(org.junit.Test)

Example 10 with Element

use of org.kxml2.kdom.Element in project javarosa by opendatakit.

the class ChildProcessingTest method worksWithTwoMatchingChildren.

@Test
public void worksWithTwoMatchingChildren() {
    Element el = new Element();
    el.addChild(ELEMENT, el.createElement(null, "Child Name"));
    el.addChild(ELEMENT, el.createElement(null, "Child Name"));
    assertTrue(XFormParser.childOptimizationsOk(el));
}
Also used : Element(org.kxml2.kdom.Element) Test(org.junit.Test)

Aggregations

Element (org.kxml2.kdom.Element)50 TreeElement (org.javarosa.core.model.instance.TreeElement)25 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)23 ArrayList (java.util.ArrayList)21 IOException (java.io.IOException)17 IFormElement (org.javarosa.core.model.IFormElement)17 KXmlParser (org.kxml2.io.KXmlParser)12 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)12 File (java.io.File)8 Test (org.junit.Test)7 ParsingException (org.opendatakit.briefcase.model.ParsingException)7 XmlPullParser (org.xmlpull.v1.XmlPullParser)7 Document (org.kxml2.kdom.Document)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 FileInputStream (java.io.FileInputStream)3 FileNotFoundException (java.io.FileNotFoundException)3 OutputStreamWriter (java.io.OutputStreamWriter)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 HashMap (java.util.HashMap)3 KXmlSerializer (org.kxml2.io.KXmlSerializer)3