Search in sources :

Example 11 with Document

use of org.kxml2.kdom.Document in project briefcase by opendatakit.

the class XmlManipulationUtils method parseSubmissionDownloadListResponse.

public static final SubmissionChunk parseSubmissionDownloadListResponse(Document doc) throws ParsingException {
    List<String> uriList = new ArrayList<>();
    String websafeCursorString = "";
    // Attempt parsing
    Element idChunkElement = doc.getRootElement();
    if (!idChunkElement.getName().equals("idChunk")) {
        String msg = "Parsing submissionList reply -- root element is not <idChunk> :" + idChunkElement.getName();
        log.error(msg);
        throw new ParsingException(msg);
    }
    String namespace = idChunkElement.getNamespace();
    if (!namespace.equalsIgnoreCase(NAMESPACE_OPENDATAKIT_ORG_SUBMISSIONS)) {
        String msg = "Parsing submissionList reply -- root element namespace is incorrect:" + namespace;
        log.error(msg);
        throw new ParsingException(msg);
    }
    int nElements = idChunkElement.getChildCount();
    for (int i = 0; i < nElements; ++i) {
        if (idChunkElement.getType(i) != Element.ELEMENT) {
            // e.g., whitespace (text)
            continue;
        }
        Element subElement = (Element) idChunkElement.getElement(i);
        namespace = subElement.getNamespace();
        if (!namespace.equalsIgnoreCase(NAMESPACE_OPENDATAKIT_ORG_SUBMISSIONS)) {
            // someone else's extension?
            continue;
        }
        String name = subElement.getName();
        if (name.equalsIgnoreCase("idList")) {
            // parse the idList
            int nIdElements = subElement.getChildCount();
            for (int j = 0; j < nIdElements; ++j) {
                if (subElement.getType(j) != Element.ELEMENT) {
                    // e.g., whitespace (text)
                    continue;
                }
                Element idElement = (Element) subElement.getElement(j);
                namespace = idElement.getNamespace();
                if (!namespace.equalsIgnoreCase(NAMESPACE_OPENDATAKIT_ORG_SUBMISSIONS)) {
                    // someone else's extension?
                    continue;
                }
                name = idElement.getName();
                if (name.equalsIgnoreCase("id")) {
                    // gather the uri
                    String uri = XFormParser.getXMLText(idElement, true);
                    if (uri != null) {
                        uriList.add(uri);
                    }
                } else {
                    log.warn("Unrecognized tag inside idList: " + name);
                }
            }
        } else if (name.equalsIgnoreCase("resumptionCursor")) {
            // gather the resumptionCursor
            websafeCursorString = XFormParser.getXMLText(subElement, true);
            if (websafeCursorString == null) {
                websafeCursorString = "";
            }
        } else {
            log.warn("Unrecognized tag inside idChunk: " + name);
        }
    }
    return new SubmissionChunk(uriList, websafeCursorString);
}
Also used : Element(org.kxml2.kdom.Element) ParsingException(org.opendatakit.briefcase.model.ParsingException) ArrayList(java.util.ArrayList) SubmissionChunk(org.opendatakit.briefcase.util.ServerFetcher.SubmissionChunk)

Example 12 with Document

use of org.kxml2.kdom.Document in project android by JetBrains.

the class DomPullParserTest method compareParsers.

private static void compareParsers(PsiFile file, NextEventType nextEventType) throws Exception {
    assertTrue(file instanceof XmlFile);
    Document document = XmlUtils.parseDocumentSilently(file.getText(), true);
    assertNotNull(document);
    KXmlParser referenceParser = createReferenceParser(file);
    DomPullParser parser = new DomPullParser(document.getDocumentElement());
    assertEquals("Expected " + name(referenceParser.getEventType()) + " but was " + name(parser.getEventType()) + " (at line:column " + describePosition(referenceParser) + ")", referenceParser.getEventType(), parser.getEventType());
    while (true) {
        int expected, next;
        switch(nextEventType) {
            case NEXT:
                expected = referenceParser.next();
                next = parser.next();
                break;
            case NEXT_TOKEN:
                expected = referenceParser.nextToken();
                next = parser.nextToken();
                break;
            case NEXT_TAG:
                {
                    try {
                        expected = referenceParser.nextTag();
                    } catch (Exception e) {
                        expected = referenceParser.getEventType();
                    }
                    try {
                        next = parser.nextTag();
                    } catch (Exception e) {
                        next = parser.getEventType();
                    }
                    break;
                }
            default:
                throw new AssertionError("Unexpected type");
        }
        Element element = null;
        if (expected == XmlPullParser.START_TAG) {
            assertNotNull(parser.getViewCookie());
            assertTrue(parser.getViewCookie() == null || parser.getViewCookie() instanceof Element);
            element = (Element) parser.getViewCookie();
        }
        if (expected == XmlPullParser.START_TAG) {
            assertEquals(referenceParser.getName(), parser.getName());
            if (element != document.getDocumentElement()) {
                // KXmlParser seems to not include xmlns: attributes on the root tag!
                SortedSet<String> referenceAttributes = new TreeSet<>();
                SortedSet<String> attributes = new TreeSet<>();
                for (int i = 0; i < referenceParser.getAttributeCount(); i++) {
                    String s = referenceParser.getAttributePrefix(i) + ':' + referenceParser.getAttributeName(i) + '=' + referenceParser.getAttributeValue(i);
                    referenceAttributes.add(s);
                }
                for (int i = 0; i < parser.getAttributeCount(); i++) {
                    String s = parser.getAttributePrefix(i) + ':' + parser.getAttributeName(i) + '=' + parser.getAttributeValue(i);
                    attributes.add(s);
                    if (parser.getAttributeNamespace(i) != null) {
                        //noinspection ConstantConditions
                        assertEquals(normalizeValue(parser.getAttributeValue(i)), normalizeValue(parser.getAttributeValue(parser.getAttributeNamespace(i), parser.getAttributeName(i))));
                    }
                }
                assertEquals(referenceAttributes, attributes);
            }
            assertEquals(referenceParser.isEmptyElementTag(), parser.isEmptyElementTag());
            if (element instanceof XmlTag) {
                XmlTag tag = (XmlTag) element;
                for (XmlAttribute attribute : tag.getAttributes()) {
                    String namespace = attribute.getNamespace();
                    String name = attribute.getLocalName();
                    assertEquals(namespace + ':' + name + " in element " + parser.getName(), normalizeValue(referenceParser.getAttributeValue(namespace, name)), normalizeValue(parser.getAttributeValue(namespace, name)));
                }
            }
        } else if (expected == XmlPullParser.TEXT || expected == XmlPullParser.COMMENT) {
            assertEquals(StringUtil.notNullize(referenceParser.getText()).trim(), StringUtil.notNullize(parser.getText()).trim());
        }
        if (expected != next) {
            assertEquals("Expected " + name(expected) + " but was " + name(next) + "(At " + describePosition(referenceParser) + ")", expected, next);
        }
        if (expected == XmlPullParser.END_DOCUMENT) {
            break;
        }
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) KXmlParser(org.kxml2.io.KXmlParser) TreeSet(java.util.TreeSet) XmlTag(com.intellij.psi.xml.XmlTag)

Example 13 with Document

use of org.kxml2.kdom.Document in project robovm by robovm.

the class DocumentBuilderImpl method parse.

@Override
public Document parse(InputSource source) throws SAXException, IOException {
    if (source == null) {
        throw new IllegalArgumentException("source == null");
    }
    String namespaceURI = null;
    String qualifiedName = null;
    DocumentType doctype = null;
    String inputEncoding = source.getEncoding();
    String systemId = source.getSystemId();
    DocumentImpl document = new DocumentImpl(dom, namespaceURI, qualifiedName, doctype, inputEncoding);
    document.setDocumentURI(systemId);
    KXmlParser parser = new KXmlParser();
    try {
        parser.keepNamespaceAttributes();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, namespaceAware);
        if (source.getByteStream() != null) {
            parser.setInput(source.getByteStream(), inputEncoding);
        } else if (source.getCharacterStream() != null) {
            parser.setInput(source.getCharacterStream());
        } else if (systemId != null) {
            URL url = new URL(systemId);
            URLConnection urlConnection = url.openConnection();
            urlConnection.connect();
            // TODO: if null, extract the inputEncoding from the Content-Type header?
            parser.setInput(urlConnection.getInputStream(), inputEncoding);
        } else {
            throw new SAXParseException("InputSource needs a stream, reader or URI", null);
        }
        if (parser.nextToken() == XmlPullParser.END_DOCUMENT) {
            throw new SAXParseException("Unexpected end of document", null);
        }
        parse(parser, document, document, XmlPullParser.END_DOCUMENT);
        parser.require(XmlPullParser.END_DOCUMENT, null, null);
    } catch (XmlPullParserException ex) {
        if (ex.getDetail() instanceof IOException) {
            throw (IOException) ex.getDetail();
        }
        if (ex.getDetail() instanceof RuntimeException) {
            throw (RuntimeException) ex.getDetail();
        }
        LocatorImpl locator = new LocatorImpl();
        locator.setPublicId(source.getPublicId());
        locator.setSystemId(systemId);
        locator.setLineNumber(ex.getLineNumber());
        locator.setColumnNumber(ex.getColumnNumber());
        SAXParseException newEx = new SAXParseException(ex.getMessage(), locator);
        if (errorHandler != null) {
            errorHandler.error(newEx);
        }
        throw newEx;
    } finally {
        IoUtils.closeQuietly(parser);
    }
    return document;
}
Also used : KXmlParser(org.kxml2.io.KXmlParser) SAXParseException(org.xml.sax.SAXParseException) DocumentType(org.w3c.dom.DocumentType) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) LocatorImpl(org.xml.sax.helpers.LocatorImpl) IOException(java.io.IOException) DocumentImpl(org.apache.harmony.xml.dom.DocumentImpl) URL(java.net.URL) URLConnection(java.net.URLConnection)

Example 14 with Document

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

the class DownloadFormListTask method doInBackground.

@Override
protected HashMap<String, FormDetails> doInBackground(Void... values) {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(Collect.getInstance().getBaseContext());
    String downloadListUrl = settings.getString(PreferenceKeys.KEY_SERVER_URL, Collect.getInstance().getString(R.string.default_server_url));
    // NOTE: /formlist must not be translated! It is the well-known path on the server.
    String formListUrl = Collect.getInstance().getApplicationContext().getString(R.string.default_odk_formlist);
    String downloadPath = settings.getString(PreferenceKeys.KEY_FORMLIST_URL, formListUrl);
    downloadListUrl += downloadPath;
    Collect.getInstance().getActivityLogger().logAction(this, formListUrl, downloadListUrl);
    // We populate this with available forms from the specified server.
    // <formname, details>
    HashMap<String, FormDetails> formList = new HashMap<String, FormDetails>();
    // 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(downloadListUrl, localContext, httpclient);
    // If we can't get the document, return the error, cancel the task
    if (result.errorMessage != null) {
        if (result.responseCode == 401) {
            formList.put(DL_AUTH_REQUIRED, new FormDetails(result.errorMessage));
        } else {
            formList.put(DL_ERROR_MSG, new FormDetails(result.errorMessage));
        }
        return formList;
    }
    if (result.isOpenRosaResponse) {
        // Attempt OpenRosa 1.0 parsing
        Element xformsElement = result.doc.getRootElement();
        if (!xformsElement.getName().equals("xforms")) {
            String error = "root element is not <xforms> : " + xformsElement.getName();
            Timber.e("Parsing OpenRosa reply -- %s", error);
            formList.put(DL_ERROR_MSG, new FormDetails(Collect.getInstance().getString(R.string.parse_openrosa_formlist_failed, error)));
            return formList;
        }
        String namespace = xformsElement.getNamespace();
        if (!isXformsListNamespacedElement(xformsElement)) {
            String error = "root element namespace is incorrect:" + namespace;
            Timber.e("Parsing OpenRosa reply -- %s", error);
            formList.put(DL_ERROR_MSG, new FormDetails(Collect.getInstance().getString(R.string.parse_openrosa_formlist_failed, error)));
            return formList;
        }
        int elements = xformsElement.getChildCount();
        for (int i = 0; i < elements; ++i) {
            if (xformsElement.getType(i) != Element.ELEMENT) {
                // e.g., whitespace (text)
                continue;
            }
            Element xformElement = xformsElement.getElement(i);
            if (!isXformsListNamespacedElement(xformElement)) {
                // someone else's extension?
                continue;
            }
            String name = xformElement.getName();
            if (!name.equalsIgnoreCase("xform")) {
                // someone else's extension?
                continue;
            }
            // this is something we know how to interpret
            String formId = null;
            String formName = null;
            String version = null;
            String majorMinorVersion = null;
            String description = null;
            String downloadUrl = null;
            String manifestUrl = null;
            String hash = null;
            // don't process descriptionUrl
            int fieldCount = xformElement.getChildCount();
            for (int j = 0; j < fieldCount; ++j) {
                if (xformElement.getType(j) != Element.ELEMENT) {
                    // whitespace
                    continue;
                }
                Element child = xformElement.getElement(j);
                if (!isXformsListNamespacedElement(child)) {
                    // someone else's extension?
                    continue;
                }
                String tag = child.getName();
                switch(tag) {
                    case "formID":
                        formId = XFormParser.getXMLText(child, true);
                        if (formId != null && formId.length() == 0) {
                            formId = null;
                        }
                        break;
                    case "name":
                        formName = XFormParser.getXMLText(child, true);
                        if (formName != null && formName.length() == 0) {
                            formName = null;
                        }
                        break;
                    case "version":
                        version = XFormParser.getXMLText(child, true);
                        if (version != null && version.length() == 0) {
                            version = null;
                        }
                        break;
                    case "majorMinorVersion":
                        majorMinorVersion = XFormParser.getXMLText(child, true);
                        if (majorMinorVersion != null && majorMinorVersion.length() == 0) {
                            majorMinorVersion = null;
                        }
                        break;
                    case "descriptionText":
                        description = XFormParser.getXMLText(child, true);
                        if (description != null && description.length() == 0) {
                            description = null;
                        }
                        break;
                    case "downloadUrl":
                        downloadUrl = XFormParser.getXMLText(child, true);
                        if (downloadUrl != null && downloadUrl.length() == 0) {
                            downloadUrl = null;
                        }
                        break;
                    case "manifestUrl":
                        manifestUrl = XFormParser.getXMLText(child, true);
                        if (manifestUrl != null && manifestUrl.length() == 0) {
                            manifestUrl = null;
                        }
                        break;
                    case "hash":
                        hash = XFormParser.getXMLText(child, true);
                        if (hash != null && hash.length() == 0) {
                            hash = null;
                        }
                        break;
                }
            }
            if (formId == null || downloadUrl == null || formName == null) {
                String error = "Forms list entry " + Integer.toString(i) + " has missing or empty tags: formID, name, or downloadUrl";
                Timber.e("Parsing OpenRosa reply -- %s", error);
                formList.clear();
                formList.put(DL_ERROR_MSG, new FormDetails(Collect.getInstance().getString(R.string.parse_openrosa_formlist_failed, error)));
                return formList;
            }
            boolean isNewerFormVersionAvailable = false;
            boolean areNewerMediaFilesAvailable = false;
            if (isThisFormAlreadyDownloaded(formId)) {
                isNewerFormVersionAvailable = isNewerFormVersionAvailable(DownloadFormsTask.getMd5Hash(hash));
                if (!isNewerFormVersionAvailable && manifestUrl != null) {
                    List<MediaFile> newMediaFiles = downloadMediaFileList(manifestUrl);
                    if (newMediaFiles != null && newMediaFiles.size() > 0) {
                        areNewerMediaFilesAvailable = areNewerMediaFilesAvailable(formId, version, newMediaFiles);
                    }
                }
            }
            formList.put(formId, new FormDetails(formName, downloadUrl, manifestUrl, formId, (version != null) ? version : majorMinorVersion, isNewerFormVersionAvailable, areNewerMediaFilesAvailable));
        }
    } else {
        // Aggregate 0.9.x mode...
        // populate HashMap with form names and urls
        Element formsElement = result.doc.getRootElement();
        int formsCount = formsElement.getChildCount();
        String formId = null;
        for (int i = 0; i < formsCount; ++i) {
            if (formsElement.getType(i) != Element.ELEMENT) {
                // whitespace
                continue;
            }
            Element child = formsElement.getElement(i);
            String tag = child.getName();
            if (tag.equals("formID")) {
                formId = XFormParser.getXMLText(child, true);
                if (formId != null && formId.length() == 0) {
                    formId = null;
                }
            }
            if (tag.equalsIgnoreCase("form")) {
                String formName = XFormParser.getXMLText(child, true);
                if (formName != null && formName.length() == 0) {
                    formName = null;
                }
                String downloadUrl = child.getAttributeValue(null, "url");
                downloadUrl = downloadUrl.trim();
                if (downloadUrl != null && downloadUrl.length() == 0) {
                    downloadUrl = null;
                }
                if (downloadUrl == null || formName == null) {
                    String error = "Forms list entry " + Integer.toString(i) + " is missing form name or url attribute";
                    Timber.e("Parsing OpenRosa reply -- %s", error);
                    formList.clear();
                    formList.put(DL_ERROR_MSG, new FormDetails(Collect.getInstance().getString(R.string.parse_legacy_formlist_failed, error)));
                    return formList;
                }
                formList.put(formName, new FormDetails(formName, downloadUrl, null, formId, null, false, false));
                formId = null;
            }
        }
    }
    return formList;
}
Also used : MediaFile(org.odk.collect.android.logic.MediaFile) SharedPreferences(android.content.SharedPreferences) HashMap(java.util.HashMap) Element(org.kxml2.kdom.Element) HttpContext(org.opendatakit.httpclientandroidlib.protocol.HttpContext) FormDetails(org.odk.collect.android.logic.FormDetails) DocumentFetchResult(org.odk.collect.android.utilities.DocumentFetchResult) HttpClient(org.opendatakit.httpclientandroidlib.client.HttpClient)

Example 15 with Document

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

the class EncryptionUtils method writeSubmissionManifest.

private static void writeSubmissionManifest(EncryptedFormInformation formInfo, File submissionXml, List<File> mediaFiles) throws EncryptionException {
    Document d = new Document();
    d.setStandalone(true);
    d.setEncoding(UTF_8);
    Element e = d.createElement(XML_ENCRYPTED_TAG_NAMESPACE, DATA);
    e.setPrefix(null, XML_ENCRYPTED_TAG_NAMESPACE);
    e.setAttribute(null, ID, formInfo.formId);
    if (formInfo.formVersion != null) {
        e.setAttribute(null, VERSION, formInfo.formVersion);
    }
    e.setAttribute(null, ENCRYPTED, "yes");
    d.addChild(0, Node.ELEMENT, e);
    int idx = 0;
    Element c;
    c = d.createElement(XML_ENCRYPTED_TAG_NAMESPACE, BASE64_ENCRYPTED_KEY);
    c.addChild(0, Node.TEXT, formInfo.base64RsaEncryptedSymmetricKey);
    e.addChild(idx++, Node.ELEMENT, c);
    c = d.createElement(XML_OPENROSA_NAMESPACE, META);
    c.setPrefix("orx", XML_OPENROSA_NAMESPACE);
    {
        Element instanceTag = d.createElement(XML_OPENROSA_NAMESPACE, INSTANCE_ID);
        instanceTag.addChild(0, Node.TEXT, formInfo.instanceMetadata.instanceId);
        c.addChild(0, Node.ELEMENT, instanceTag);
    }
    e.addChild(idx++, Node.ELEMENT, c);
    e.addChild(idx++, Node.IGNORABLE_WHITESPACE, NEW_LINE);
    if (mediaFiles != null) {
        for (File file : mediaFiles) {
            c = d.createElement(XML_ENCRYPTED_TAG_NAMESPACE, MEDIA);
            Element fileTag = d.createElement(XML_ENCRYPTED_TAG_NAMESPACE, FILE);
            fileTag.addChild(0, Node.TEXT, file.getName() + ".enc");
            c.addChild(0, Node.ELEMENT, fileTag);
            e.addChild(idx++, Node.ELEMENT, c);
            e.addChild(idx++, Node.IGNORABLE_WHITESPACE, NEW_LINE);
        }
    }
    c = d.createElement(XML_ENCRYPTED_TAG_NAMESPACE, ENCRYPTED_XML_FILE);
    c.addChild(0, Node.TEXT, submissionXml.getName() + ".enc");
    e.addChild(idx++, Node.ELEMENT, c);
    c = d.createElement(XML_ENCRYPTED_TAG_NAMESPACE, BASE64_ENCRYPTED_ELEMENT_SIGNATURE);
    c.addChild(0, Node.TEXT, formInfo.getBase64EncryptedElementSignature());
    e.addChild(idx++, Node.ELEMENT, c);
    FileOutputStream fout = null;
    OutputStreamWriter writer = null;
    try {
        fout = new FileOutputStream(submissionXml);
        writer = new OutputStreamWriter(fout, UTF_8);
        KXmlSerializer serializer = new KXmlSerializer();
        serializer.setOutput(writer);
        // setting the response content type emits the xml header.
        // just write the body here...
        d.writeChildren(serializer);
        serializer.flush();
        writer.flush();
        fout.getChannel().force(true);
        writer.close();
    } catch (Exception ex) {
        String msg = "Error writing submission.xml for encrypted submission: " + submissionXml.getParentFile().getName();
        Timber.e(ex, "%s due to : %s ", msg, ex.getMessage());
        throw new EncryptionException(msg, ex);
    } finally {
        IOUtils.closeQuietly(writer);
        IOUtils.closeQuietly(fout);
    }
}
Also used : Element(org.kxml2.kdom.Element) FileOutputStream(java.io.FileOutputStream) EncryptionException(org.odk.collect.android.exception.EncryptionException) OutputStreamWriter(java.io.OutputStreamWriter) Document(org.kxml2.kdom.Document) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) KXmlSerializer(org.kxml2.io.KXmlSerializer) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) EncryptionException(org.odk.collect.android.exception.EncryptionException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) IOException(java.io.IOException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchProviderException(java.security.NoSuchProviderException)

Aggregations

IOException (java.io.IOException)18 KXmlParser (org.kxml2.io.KXmlParser)12 Document (org.kxml2.kdom.Document)11 Element (org.kxml2.kdom.Element)11 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)11 ParsingException (org.opendatakit.briefcase.model.ParsingException)8 File (java.io.File)6 URL (java.net.URL)6 ArrayList (java.util.ArrayList)6 KXmlSerializer (org.kxml2.io.KXmlSerializer)6 FileNotFoundException (java.io.FileNotFoundException)5 InputStreamReader (java.io.InputStreamReader)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 InputStream (java.io.InputStream)4 MalformedURLException (java.net.MalformedURLException)4 FileSystemException (org.opendatakit.briefcase.model.FileSystemException)4 MetadataUpdateException (org.opendatakit.briefcase.model.MetadataUpdateException)4 MediaFile (org.opendatakit.briefcase.util.ServerFetcher.MediaFile)4 FileInputStream (java.io.FileInputStream)3 OutputStreamWriter (java.io.OutputStreamWriter)3