Search in sources :

Example 1 with XPathMissingInstanceException

use of org.javarosa.xpath.XPathMissingInstanceException in project javarosa by opendatakit.

the class XPathPathExpr method eval.

public XPathNodeset eval(DataInstance m, EvaluationContext ec) {
    TreeReference genericRef = getReference();
    TreeReference ref;
    if (genericRef.getContext() == TreeReference.CONTEXT_ORIGINAL) {
        ref = genericRef.contextualize(ec.getOriginalContext());
    } else {
        ref = genericRef.contextualize(ec.getContextRef());
    }
    // check if this nodeset refers to a non-main instance
    if (ref.getInstanceName() != null && ref.isAbsolute()) {
        DataInstance nonMain = ec.getInstance(ref.getInstanceName());
        if (nonMain != null) {
            m = nonMain;
        } else {
            throw new XPathMissingInstanceException(ref.getInstanceName(), "Instance referenced by " + ref.toString(true) + " does not exist");
        }
    } else {
        // TODO: We should really stop passing 'm' around and start just getting the right instance from ec
        // at a more central level
        m = ec.getMainInstance();
        if (m == null) {
            String refStr = ref == null ? "" : ref.toString(true);
            throw new XPathException("Cannot evaluate the reference [" + refStr + "] in the current evaluation context. No default instance has been declared!");
        }
    }
    // regardless of the above, we want to ensure there is a definition
    if (m.getRoot() == null) {
        // This instance is _declared_, but doesn't actually have any data in it.
        throw new XPathMissingInstanceException(ref.getInstanceName(), "Instance referenced by " + ref.toString(true) + " has not been loaded");
    }
    // this makes no sense...
    // if (ref.isAbsolute() && m.getTemplatePath(ref) == null) {
    // List<TreeReference> nodesetRefs = new List<TreeReference>();
    // return new XPathNodeset(nodesetRefs, m, ec);
    // }
    List<TreeReference> nodesetRefs = ec.expandReference(ref);
    // to fix conditions based on non-relevant data, filter the nodeset by relevancy
    for (int i = 0; i < nodesetRefs.size(); i++) {
        if (!m.resolveReference(nodesetRefs.get(i)).isRelevant()) {
            nodesetRefs.remove(i);
            i--;
        }
    }
    return new XPathNodeset(nodesetRefs, m, ec);
}
Also used : DataInstance(org.javarosa.core.model.instance.DataInstance) XPathException(org.javarosa.xpath.XPathException) TreeReference(org.javarosa.core.model.instance.TreeReference) XPathNodeset(org.javarosa.xpath.XPathNodeset) XPathMissingInstanceException(org.javarosa.xpath.XPathMissingInstanceException)

Aggregations

DataInstance (org.javarosa.core.model.instance.DataInstance)1 TreeReference (org.javarosa.core.model.instance.TreeReference)1 XPathException (org.javarosa.xpath.XPathException)1 XPathMissingInstanceException (org.javarosa.xpath.XPathMissingInstanceException)1 XPathNodeset (org.javarosa.xpath.XPathNodeset)1