Search in sources :

Example 1 with DeserializationException

use of org.javarosa.core.util.externalizable.DeserializationException in project javarosa by opendatakit.

the class CompactInstanceWrapper method readTreeElement.

/**
 * recursively read in a node of the instance, by filling out the template instance
 * @param e
 * @param ref
 * @param in
 * @param pf
 * @throws IOException
 * @throws DeserializationException
 */
private void readTreeElement(TreeElement e, DataInputStream in, PrototypeFactory pf) throws IOException, DeserializationException {
    TreeElement templ = instance.getTemplatePath(e.getRef());
    boolean isGroup = !templ.isLeaf();
    if (isGroup) {
        List<String> childTypes = new ArrayList<String>(templ.getNumChildren());
        for (int i = 0; i < templ.getNumChildren(); i++) {
            String childName = templ.getChildAt(i).getName();
            if (!childTypes.contains(childName)) {
                childTypes.add(childName);
            }
        }
        for (int i = 0; i < childTypes.size(); i++) {
            String childName = childTypes.get(i);
            TreeReference childTemplRef = e.getRef().extendRef(childName, 0);
            TreeElement childTempl = instance.getTemplatePath(childTemplRef);
            boolean repeatable = childTempl.isRepeatable();
            int n = ExtUtil.readInt(in);
            boolean relevant = (n > 0);
            if (!repeatable && n > 1) {
                throw new DeserializationException("Detected repeated instances of a non-repeatable node");
            }
            if (repeatable) {
                int mult = e.getChildMultiplicity(childName);
                for (int j = mult - 1; j >= 0; j--) {
                    e.removeChild(childName, j);
                }
                for (int j = 0; j < n; j++) {
                    TreeReference dstRef = e.getRef().extendRef(childName, j);
                    try {
                        instance.copyNode(childTempl, dstRef);
                    } catch (InvalidReferenceException ire) {
                        // If there is an invalid reference, this is a malformed instance,
                        // so we'll throw a Deserialization exception.
                        TreeReference r = ire.getInvalidReference();
                        if (r == null) {
                            throw new DeserializationException("Null Reference while attempting to deserialize! " + ire.getMessage());
                        } else {
                            throw new DeserializationException("Invalid Reference while attemtping to deserialize! Reference: " + r.toString(true) + " | " + ire.getMessage());
                        }
                    }
                    TreeElement child = e.getChild(childName, j);
                    child.setRelevant(true);
                    readTreeElement(child, in, pf);
                }
            } else {
                TreeElement child = e.getChild(childName, 0);
                child.setRelevant(relevant);
                if (relevant) {
                    readTreeElement(child, in, pf);
                }
            }
        }
    } else {
        e.setValue((IAnswerData) ExtUtil.read(in, new ExtWrapAnswerData(e.getDataType())));
    }
}
Also used : TreeReference(org.javarosa.core.model.instance.TreeReference) ArrayList(java.util.ArrayList) DeserializationException(org.javarosa.core.util.externalizable.DeserializationException) InvalidReferenceException(org.javarosa.core.model.instance.InvalidReferenceException) TreeElement(org.javarosa.core.model.instance.TreeElement)

Example 2 with DeserializationException

use of org.javarosa.core.util.externalizable.DeserializationException in project javarosa by opendatakit.

the class QuestionDataElementTests method setUp.

public void setUp() throws Exception {
    super.setUp();
    stringData = new StringData("Answer Value");
    integerData = new IntegerData(4);
    stringReference = new IDataReference() {

        String reference = "stringValue";

        public Object getReference() {
            return reference;
        }

        public void setReference(Object reference) {
            this.reference = (String) reference;
        }

        /*
            public boolean referenceMatches(IDataReference reference) {
                return this.reference.equals(reference.getReference());
            }


            public IDataReference clone()  {
                IDataReference newReference = null;
                try {
                    newReference = (IDataReference)this.getClass().newInstance();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                newReference.setReference(reference);
                return newReference;
            }
            */
        public void readExternal(DataInputStream in, PrototypeFactory pf) throws IOException, DeserializationException {
        }

        public void writeExternal(DataOutputStream out) throws IOException {
        }
    };
    integerReference = new IDataReference() {

        Integer intReference = new Integer(15);

        public Object getReference() {
            return intReference;
        }

        public void setReference(Object reference) {
            this.intReference = (Integer) reference;
        }

        /*
            public boolean referenceMatches(IDataReference reference) {
                return this.intReference.equals(reference.getReference());
            }


            public IDataReference clone()  {
                IDataReference newReference = null;
                try {
                    newReference = (IDataReference)this.getClass().newInstance();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                newReference.setReference(intReference);
                return newReference;
            }
            */
        public void readExternal(DataInputStream in, PrototypeFactory pf) throws IOException, DeserializationException {
        }

        public void writeExternal(DataOutputStream out) throws IOException {
        }
    };
    intElement = new TreeElement("intElement");
    intElement.setValue(integerData);
    stringElement = new TreeElement(stringElementName);
    stringElement.setValue(stringData);
}
Also used : PrototypeFactory(org.javarosa.core.util.externalizable.PrototypeFactory) IDataReference(org.javarosa.core.model.IDataReference) DataOutputStream(java.io.DataOutputStream) IntegerData(org.javarosa.core.model.data.IntegerData) IOException(java.io.IOException) StringData(org.javarosa.core.model.data.StringData) DataInputStream(java.io.DataInputStream) DeserializationException(org.javarosa.core.util.externalizable.DeserializationException) TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement)

Example 3 with DeserializationException

use of org.javarosa.core.util.externalizable.DeserializationException in project javarosa by opendatakit.

the class QuestionDataGroupTests method setUp.

public void setUp() throws Exception {
    super.setUp();
    stringData = new StringData("Answer Value");
    integerData = new IntegerData(4);
    stringReference = new IDataReference() {

        String reference = "stringValue";

        public Object getReference() {
            return reference;
        }

        public void setReference(Object reference) {
            this.reference = (String) reference;
        }

        /*
            public boolean referenceMatches(IDataReference reference) {
                return this.reference.equals(reference.getReference());
            }


            public IDataReference clone()  {
                IDataReference newReference = null;
                try {
                    newReference = (IDataReference)this.getClass().newInstance();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                newReference.setReference(reference);
                return newReference;
            }
            */
        public void readExternal(DataInputStream in, PrototypeFactory pf) throws IOException, DeserializationException {
        }

        public void writeExternal(DataOutputStream out) throws IOException {
        }
    };
    integerReference = new IDataReference() {

        Integer intReference = new Integer(15);

        public Object getReference() {
            return intReference;
        }

        public void setReference(Object reference) {
            this.intReference = (Integer) reference;
        }

        /*
            public boolean referenceMatches(IDataReference reference) {
                return this.intReference.equals(reference.getReference());
            }


            public IDataReference clone()  {
                IDataReference newReference = null;
                try {
                    newReference = (IDataReference)this.getClass().newInstance();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                newReference.setReference(intReference);
                return newReference;
            }
            */
        public void readExternal(DataInputStream in, PrototypeFactory pf) throws IOException, DeserializationException {
        }

        public void writeExternal(DataOutputStream out) throws IOException {
        }
    };
    intElement = new TreeElement("intElement");
    intElement.setValue(integerData);
    stringElement = new TreeElement(stringElementName);
    stringElement.setValue(stringData);
    group = new TreeElement(groupName);
}
Also used : PrototypeFactory(org.javarosa.core.util.externalizable.PrototypeFactory) IDataReference(org.javarosa.core.model.IDataReference) DataOutputStream(java.io.DataOutputStream) IntegerData(org.javarosa.core.model.data.IntegerData) IOException(java.io.IOException) StringData(org.javarosa.core.model.data.StringData) DataInputStream(java.io.DataInputStream) DeserializationException(org.javarosa.core.util.externalizable.DeserializationException) TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement)

Example 4 with DeserializationException

use of org.javarosa.core.util.externalizable.DeserializationException in project javarosa by opendatakit.

the class XFormUtils method getFormFromSerializedResource.

public static FormDef getFormFromSerializedResource(String resource) {
    FormDef returnForm = null;
    InputStream is = System.class.getResourceAsStream(resource);
    DataInputStream dis = null;
    try {
        if (is != null) {
            dis = new DataInputStream(is);
            returnForm = (FormDef) ExtUtil.read(dis, FormDef.class);
        } else {
            // #if debug.output==verbose
            Std.out.println("ResourceStream NULL");
        // #endif
        }
    } catch (IOException e) {
        Std.printStack(e);
    } catch (DeserializationException e) {
        Std.printStack(e);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                Std.printStack(e);
            }
        }
        if (dis != null) {
            try {
                dis.close();
            } catch (IOException e) {
                Std.printStack(e);
            }
        }
    }
    return returnForm;
}
Also used : FormDef(org.javarosa.core.model.FormDef) DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) DeserializationException(org.javarosa.core.util.externalizable.DeserializationException)

Example 5 with DeserializationException

use of org.javarosa.core.util.externalizable.DeserializationException in project javarosa by opendatakit.

the class GroupDef method readExternal.

/**
 * Reads a group definition object from the supplied stream.
 */
public void readExternal(DataInputStream dis, PrototypeFactory pf) throws IOException, DeserializationException {
    try {
        setID(ExtUtil.readInt(dis));
        setAppearanceAttr((String) ExtUtil.read(dis, new ExtWrapNullable(String.class), pf));
        setBind((IDataReference) ExtUtil.read(dis, new ExtWrapTagged(), pf));
        setTextID((String) ExtUtil.read(dis, new ExtWrapNullable(String.class), pf));
        setLabelInnerText((String) ExtUtil.read(dis, new ExtWrapNullable(String.class), pf));
        setRepeat(ExtUtil.readBool(dis));
        setChildren((List<IFormElement>) ExtUtil.read(dis, new ExtWrapListPoly(), pf));
        noAddRemove = ExtUtil.readBool(dis);
        count = (IDataReference) ExtUtil.read(dis, new ExtWrapNullable(new ExtWrapTagged()), pf);
        chooseCaption = ExtUtil.nullIfEmpty(ExtUtil.readString(dis));
        addCaption = ExtUtil.nullIfEmpty(ExtUtil.readString(dis));
        delCaption = ExtUtil.nullIfEmpty(ExtUtil.readString(dis));
        doneCaption = ExtUtil.nullIfEmpty(ExtUtil.readString(dis));
        addEmptyCaption = ExtUtil.nullIfEmpty(ExtUtil.readString(dis));
        doneEmptyCaption = ExtUtil.nullIfEmpty(ExtUtil.readString(dis));
        entryHeader = ExtUtil.nullIfEmpty(ExtUtil.readString(dis));
        delHeader = ExtUtil.nullIfEmpty(ExtUtil.readString(dis));
        mainHeader = ExtUtil.nullIfEmpty(ExtUtil.readString(dis));
        additionalAttributes = ExtUtil.readAttributes(dis, null);
    } catch (OutOfMemoryError e) {
        throw new DeserializationException("serialization format change caused misalignment and out-of-memory error");
    }
}
Also used : ExtWrapListPoly(org.javarosa.core.util.externalizable.ExtWrapListPoly) ExtWrapTagged(org.javarosa.core.util.externalizable.ExtWrapTagged) DeserializationException(org.javarosa.core.util.externalizable.DeserializationException) ExtWrapNullable(org.javarosa.core.util.externalizable.ExtWrapNullable)

Aggregations

DeserializationException (org.javarosa.core.util.externalizable.DeserializationException)6 DataInputStream (java.io.DataInputStream)3 IOException (java.io.IOException)3 TreeElement (org.javarosa.core.model.instance.TreeElement)3 DataOutputStream (java.io.DataOutputStream)2 IDataReference (org.javarosa.core.model.IDataReference)2 IntegerData (org.javarosa.core.model.data.IntegerData)2 StringData (org.javarosa.core.model.data.StringData)2 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)2 ExtWrapNullable (org.javarosa.core.util.externalizable.ExtWrapNullable)2 ExtWrapTagged (org.javarosa.core.util.externalizable.ExtWrapTagged)2 PrototypeFactory (org.javarosa.core.util.externalizable.PrototypeFactory)2 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 FormDef (org.javarosa.core.model.FormDef)1 InvalidReferenceException (org.javarosa.core.model.instance.InvalidReferenceException)1 TreeReference (org.javarosa.core.model.instance.TreeReference)1 OSMTag (org.javarosa.core.model.osm.OSMTag)1 ExtWrapList (org.javarosa.core.util.externalizable.ExtWrapList)1 ExtWrapListPoly (org.javarosa.core.util.externalizable.ExtWrapListPoly)1