Search in sources :

Example 6 with XPathPathExpr

use of org.javarosa.xpath.expr.XPathPathExpr in project javarosa by opendatakit.

the class ASTNodeLocPath method build.

public XPathExpression build() throws XPathSyntaxException {
    Vector<XPathStep> steps = new Vector<XPathStep>();
    XPathExpression filtExpr = null;
    int offset = isAbsolute() ? 1 : 0;
    for (int i = 0; i < clauses.size() + offset; i++) {
        if (offset == 0 || i > 0) {
            if (clauses.elementAt(i - offset) instanceof ASTNodePathStep) {
                steps.addElement(((ASTNodePathStep) clauses.elementAt(i - offset)).getStep());
            } else {
                filtExpr = clauses.elementAt(i - offset).build();
            }
        }
        if (i < separators.size()) {
            if (Parser.vectInt(separators, i) == Token.DBL_SLASH) {
                steps.addElement(XPathStep.ABBR_DESCENDANTS());
            }
        }
    }
    XPathStep[] stepArr = new XPathStep[steps.size()];
    for (int i = 0; i < stepArr.length; i++) stepArr[i] = steps.elementAt(i);
    if (filtExpr == null) {
        return new XPathPathExpr(isAbsolute() ? XPathPathExpr.INIT_CONTEXT_ROOT : XPathPathExpr.INIT_CONTEXT_RELATIVE, stepArr);
    } else {
        if (filtExpr instanceof XPathFilterExpr) {
            return new XPathPathExpr((XPathFilterExpr) filtExpr, stepArr);
        } else {
            return new XPathPathExpr(new XPathFilterExpr(filtExpr, new XPathExpression[0]), stepArr);
        }
    }
}
Also used : XPathExpression(org.javarosa.xpath.expr.XPathExpression) XPathPathExpr(org.javarosa.xpath.expr.XPathPathExpr) XPathStep(org.javarosa.xpath.expr.XPathStep) XPathFilterExpr(org.javarosa.xpath.expr.XPathFilterExpr) Vector(java.util.Vector)

Example 7 with XPathPathExpr

use of org.javarosa.xpath.expr.XPathPathExpr in project javarosa by opendatakit.

the class XFormParserTest method parsesExternalSecondaryInstanceForm.

@Test
public void parsesExternalSecondaryInstanceForm() throws IOException, XPathSyntaxException {
    FormDef formDef = parse(EXTERNAL_SECONDARY_INSTANCE_XML).formDef;
    assertEquals("Form with external secondary instance", formDef.getTitle());
    TreeReference treeReference = ((XPathPathExpr) parseXPath("instance('towns')/data_set")).getReference();
    EvaluationContext evaluationContext = formDef.getEvaluationContext();
    List<TreeReference> treeReferences = evaluationContext.expandReference(treeReference);
    assertEquals(1, treeReferences.size());
    DataInstance townInstance = formDef.getNonMainInstance("towns");
    AbstractTreeElement tiRoot = townInstance.getRoot();
    assertEquals("towndata", tiRoot.getName());
    assertEquals(1, tiRoot.getNumChildren());
    AbstractTreeElement dataSetChild = tiRoot.getChild("data_set", 0);
    assertEquals("us_east", dataSetChild.getValue().getDisplayText());
}
Also used : AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) DataInstance(org.javarosa.core.model.instance.DataInstance) FormDef(org.javarosa.core.model.FormDef) TreeReference(org.javarosa.core.model.instance.TreeReference) XPathPathExpr(org.javarosa.xpath.expr.XPathPathExpr) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext) Test(org.junit.Test)

Aggregations

XPathPathExpr (org.javarosa.xpath.expr.XPathPathExpr)7 XPathExpression (org.javarosa.xpath.expr.XPathExpression)5 ArrayList (java.util.ArrayList)2 FormDef (org.javarosa.core.model.FormDef)2 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)2 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)2 TreeReference (org.javarosa.core.model.instance.TreeReference)2 Cursor (android.database.Cursor)1 Serializable (java.io.Serializable)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Vector (java.util.Vector)1 IFormElement (org.javarosa.core.model.IFormElement)1 ItemsetBinding (org.javarosa.core.model.ItemsetBinding)1 Constraint (org.javarosa.core.model.condition.Constraint)1 DataInstance (org.javarosa.core.model.instance.DataInstance)1 FormInstance (org.javarosa.core.model.instance.FormInstance)1 TreeElement (org.javarosa.core.model.instance.TreeElement)1 XPathConditional (org.javarosa.xpath.XPathConditional)1