import org.javarosa.core.model.instance.TreeReference; //导入方法依赖的package包/类
public XPathNodeset eval (FormInstance 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());
}
//We don't necessarily know the model we want to be working with until we've contextualized the
//node
//check if this nodeset refers to a non-main instance
if(ref.getInstanceName() != null && ref.isAbsolute())
{
FormInstance 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 nodesetRefs = new List();
//return new XPathNodeset(nodesetRefs, m, ec);
//}
List 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);
}