Search in sources :

Example 1 with IDataPointer

use of org.javarosa.core.data.IDataPointer in project javarosa by opendatakit.

the class XFormSerializingVisitor method createSerializedPayload.

public IDataPayload createSerializedPayload(FormInstance model, IDataReference ref) throws IOException {
    init();
    rootRef = FormInstance.unpackReference(ref);
    if (this.serializer == null) {
        this.setAnswerDataSerializer(new XFormAnswerDataSerializer());
    }
    model.accept(this);
    if (theXmlDoc != null) {
        // TODO: Did this strip necessary data?
        byte[] form = XFormSerializer.getUtfBytes(theXmlDoc);
        if (dataPointers.size() == 0) {
            return new ByteArrayPayload(form, null, IDataPayload.PAYLOAD_TYPE_XML);
        }
        MultiMessagePayload payload = new MultiMessagePayload();
        payload.addPayload(new ByteArrayPayload(form, "xml_submission_file", IDataPayload.PAYLOAD_TYPE_XML));
        for (IDataPointer pointer : dataPointers) {
            payload.addPayload(new DataPointerPayload(pointer));
        }
        return payload;
    } else {
        return null;
    }
}
Also used : XFormAnswerDataSerializer(org.javarosa.xform.util.XFormAnswerDataSerializer) DataPointerPayload(org.javarosa.core.services.transport.payload.DataPointerPayload) ByteArrayPayload(org.javarosa.core.services.transport.payload.ByteArrayPayload) MultiMessagePayload(org.javarosa.core.services.transport.payload.MultiMessagePayload) IDataPointer(org.javarosa.core.data.IDataPointer)

Example 2 with IDataPointer

use of org.javarosa.core.data.IDataPointer in project javarosa by opendatakit.

the class XFormSerializingVisitor method serializeNode.

public Element serializeNode(TreeElement instanceNode) {
    // don't set anything on this element yet, as it might get overwritten
    Element e = new Element();
    // don't serialize template nodes or non-relevant nodes
    if ((respectRelevance && !instanceNode.isRelevant()) || instanceNode.getMult() == TreeReference.INDEX_TEMPLATE) {
        return null;
    }
    if (instanceNode.getValue() != null) {
        Object serializedAnswer;
        try {
            serializedAnswer = serializer.serializeAnswerData(instanceNode.getValue(), instanceNode.getDataType());
        } catch (RuntimeException ex) {
            throw new RuntimeException("Unable to serialize " + instanceNode.getValue().toString() + ". Exception: " + ex.toString());
        }
        if (serializedAnswer instanceof Element) {
            e = (Element) serializedAnswer;
        } else if (serializedAnswer instanceof String) {
            e = new Element();
            e.addChild(Node.TEXT, (String) serializedAnswer);
        } else {
            throw new RuntimeException("Can't handle serialized output for" + instanceNode.getValue().toString() + ", " + serializedAnswer);
        }
        if (serializer.containsExternalData(instanceNode.getValue()).booleanValue()) {
            IDataPointer[] pointer = serializer.retrieveExternalDataPointer(instanceNode.getValue());
            for (int i = 0; i < pointer.length; ++i) {
                dataPointers.add(pointer[i]);
            }
        }
    } else {
        // make sure all children of the same tag name are written en bloc
        List<String> childNames = new ArrayList<String>(instanceNode.getNumChildren());
        for (int i = 0; i < instanceNode.getNumChildren(); i++) {
            String childName = instanceNode.getChildAt(i).getName();
            if (!childNames.contains(childName))
                childNames.add(childName);
        }
        for (int i = 0; i < childNames.size(); i++) {
            String childName = (String) childNames.get(i);
            int mult = instanceNode.getChildMultiplicity(childName);
            for (int j = 0; j < mult; j++) {
                Element child = serializeNode(instanceNode.getChild(childName, j));
                if (child != null) {
                    e.addChild(Node.ELEMENT, child);
                }
            }
        }
    }
    e.setName(instanceNode.getName());
    // add hard-coded attributes
    for (int i = 0; i < instanceNode.getAttributeCount(); i++) {
        String namespace = instanceNode.getAttributeNamespace(i);
        String name = instanceNode.getAttributeName(i);
        String val = instanceNode.getAttributeValue(i);
        // is it legal for getAttributeValue() to return null? playing it safe for now and assuming yes
        if (val == null) {
            val = "";
        }
        e.setAttribute(namespace, name, val);
    }
    if (instanceNode.getNamespace() != null) {
        e.setNamespace(instanceNode.getNamespace());
    }
    return e;
}
Also used : TreeElement(org.javarosa.core.model.instance.TreeElement) Element(org.kxml2.kdom.Element) ArrayList(java.util.ArrayList) IDataPointer(org.javarosa.core.data.IDataPointer)

Example 3 with IDataPointer

use of org.javarosa.core.data.IDataPointer in project javarosa by opendatakit.

the class XFormAnswerDataSerializer method retrieveExternalDataPointer.

public IDataPointer[] retrieveExternalDataPointer(IAnswerData data) {
    for (IAnswerDataSerializer serializer : additionalSerializers) {
        Boolean contains = serializer.containsExternalData(data);
        if (contains != null) {
            return serializer.retrieveExternalDataPointer(data);
        }
    }
    if (data instanceof PointerAnswerData) {
        IDataPointer[] pointer = new IDataPointer[1];
        pointer[0] = (IDataPointer) ((PointerAnswerData) data).getValue();
        return pointer;
    } else if (data instanceof MultiPointerAnswerData) {
        return (IDataPointer[]) ((MultiPointerAnswerData) data).getValue();
    }
    // This shouldn't have been called.
    return null;
}
Also used : MultiPointerAnswerData(org.javarosa.core.model.data.MultiPointerAnswerData) PointerAnswerData(org.javarosa.core.model.data.PointerAnswerData) MultiPointerAnswerData(org.javarosa.core.model.data.MultiPointerAnswerData) IAnswerDataSerializer(org.javarosa.core.model.IAnswerDataSerializer) IDataPointer(org.javarosa.core.data.IDataPointer)

Example 4 with IDataPointer

use of org.javarosa.core.data.IDataPointer in project javarosa by opendatakit.

the class XFormAnswerDataSerializer method serializeAnswerData.

/**
 * @param data The AnswerDataObject to be serialized
 * @return A String which contains a reference to the
 * data
 */
public Object serializeAnswerData(MultiPointerAnswerData data) {
    // Note: In order to override this default behavior, a
    // new serializer should be used, and then registered
    // with this serializer
    IDataPointer[] pointers = (IDataPointer[]) data.getValue();
    if (pointers.length == 1) {
        return pointers[0].getDisplayText();
    }
    Element parent = new Element();
    for (int i = 0; i < pointers.length; ++i) {
        Element datael = new Element();
        datael.setName("data");
        datael.addChild(Element.TEXT, pointers[i].getDisplayText());
        parent.addChild(Element.ELEMENT, datael);
    }
    return parent;
}
Also used : Element(org.kxml2.kdom.Element) IDataPointer(org.javarosa.core.data.IDataPointer)

Example 5 with IDataPointer

use of org.javarosa.core.data.IDataPointer in project javarosa by opendatakit.

the class MultiPointerAnswerData method uncast.

@Override
public UncastData uncast() {
    StringBuilder b = new StringBuilder();
    for (IDataPointer datum : data) {
        b.append(datum.getDisplayText());
        b.append(" ");
    }
    return new UncastData(b.toString().trim());
}
Also used : IDataPointer(org.javarosa.core.data.IDataPointer)

Aggregations

IDataPointer (org.javarosa.core.data.IDataPointer)5 Element (org.kxml2.kdom.Element)2 ArrayList (java.util.ArrayList)1 IAnswerDataSerializer (org.javarosa.core.model.IAnswerDataSerializer)1 MultiPointerAnswerData (org.javarosa.core.model.data.MultiPointerAnswerData)1 PointerAnswerData (org.javarosa.core.model.data.PointerAnswerData)1 TreeElement (org.javarosa.core.model.instance.TreeElement)1 ByteArrayPayload (org.javarosa.core.services.transport.payload.ByteArrayPayload)1 DataPointerPayload (org.javarosa.core.services.transport.payload.DataPointerPayload)1 MultiMessagePayload (org.javarosa.core.services.transport.payload.MultiMessagePayload)1 XFormAnswerDataSerializer (org.javarosa.xform.util.XFormAnswerDataSerializer)1