package org.javarosa.xpath.analysis;

import org.javarosa.core.model.condition.EvaluationContext;
import org.javarosa.core.model.instance.TreeReference;

/**
 * Analyzes an XPath expression to determine whether it contains a reference to the main data
 * instance.
 *
 * @author Aliza Stone
 */
public class ReferencesMainInstanceAnalyzer extends XPathBooleanAnalyzer {

    public ReferencesMainInstanceAnalyzer(EvaluationContext ec) {
        this();
        setContext(ec);
    }

    public ReferencesMainInstanceAnalyzer() {
        super();
    }

    @Override
    public void doNormalTreeRefAnalysis(TreeReference treeRef) throws AnalysisInvalidException {
        if (treeRef.getContextType() == TreeReference.CONTEXT_ABSOLUTE &&
                treeRef.getInstanceName() == null) {
            this.result = true;
            this.shortCircuit = true;
        }
    }

    @Override
    protected boolean getDefaultValue() {
        return false;
    }

    @Override
    protected boolean aggregateResults() {
        return orResults();
    }

    XPathAnalyzer initSameTypeAnalyzer() {
        return new ReferencesMainInstanceAnalyzer();
    }
}

@@ -670,16 +670,235 @@
     if (
+referenceRefersToMainInstance(treeRef)) %7B%0A            this.result = true;%0A            this.shortCircuit = true;%0A        %7D%0A    %7D%0A%0A    private boolean referenceRefersToMainInstance(TreeReference treeRef) %7B%0A        return 
 treeRef.
@@ -1004,91 +1004,9 @@
 null
-) %7B%0A            this.result = true;%0A            this.shortCircuit = true;%0A        %7D
+;
 %0A   

use named helper