Search in sources :

Example 61 with Element

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

the class ChildProcessingTest method worksWithOneChild.

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

Example 62 with Element

use of org.kxml2.kdom.Element in project ETSMobile-Android2 by ApplETS.

the class Helper method convertToSoapObject.

public static Object convertToSoapObject(Element element) {
    if (element.getChildCount() == 0 || (element.getChildCount() == 1 && !(element.getChild(0) instanceof Element))) {
        SoapPrimitive primitive = new SoapPrimitive(element.getNamespace(), element.getName(), element.getChildCount() == 1 ? element.getText(0) : null);
        return primitive;
    } else {
        SoapObject obj = new SoapObject(element.getNamespace(), element.getName());
        for (int i = 0; i < element.getChildCount(); i++) {
            Element childElement = element.getElement(i);
            Object childObject = convertToSoapObject(childElement);
            if (childObject instanceof SoapObject) {
                SoapObject soapObj = (SoapObject) childObject;
                obj.addProperty(soapObj.getName(), childObject);
            } else {
                SoapPrimitive primitive = (SoapPrimitive) childObject;
                obj.addProperty(primitive.getName(), primitive);
            }
        }
        return obj;
    }
}
Also used : Element(org.kxml2.kdom.Element) SoapObject(org.ksoap2.serialization.SoapObject) SoapObject(org.ksoap2.serialization.SoapObject) SoapPrimitive(org.ksoap2.serialization.SoapPrimitive)

Example 63 with Element

use of org.kxml2.kdom.Element in project ETSMobile-Android2 by ApplETS.

the class ExtendedSoapSerializationEnvelope method GetSoapObject.

public SoapObject GetSoapObject(Element detailElement) {
    try {
        XmlSerializer xmlSerializer = XmlPullParserFactory.newInstance().newSerializer();
        StringWriter writer = new StringWriter();
        xmlSerializer.setOutput(writer);
        detailElement.write(xmlSerializer);
        xmlSerializer.flush();
        XmlPullParser xpp = new KXmlParser();
        xpp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
        xpp.setInput(new StringReader(writer.toString()));
        xpp.nextTag();
        SoapObject soapObj = new SoapObject(detailElement.getNamespace(), detailElement.getName());
        readSerializable(xpp, soapObj);
        return soapObj;
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}
Also used : KXmlParser(org.kxml2.io.KXmlParser) StringWriter(java.io.StringWriter) SoapObject(org.ksoap2.serialization.SoapObject) XmlPullParser(org.xmlpull.v1.XmlPullParser) StringReader(java.io.StringReader) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) XmlSerializer(org.xmlpull.v1.XmlSerializer)

Example 64 with Element

use of org.kxml2.kdom.Element 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) LocalizedApplicationKt.getLocalizedString(org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString) 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

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