use of org.javarosa.core.model.instance.FormInstance in project javarosa by opendatakit.
the class FormInstanceParser method parseInstance.
FormInstance parseInstance(Element e, boolean isMainInstance, String name, Map<String, String> namespacePrefixesByUri) {
TreeElement root = buildInstanceStructure(e, null, !isMainInstance ? name : null, e.getNamespace(), namespacePrefixesByUri, null);
FormInstance instanceModel = new FormInstance(root);
instanceModel.setName(isMainInstance ? formDef.getTitle() : name);
final List<String> usedAtts = Collections.unmodifiableList(Arrays.asList("id", "version", "uiVersion", "name"));
String schema = e.getNamespace();
if (schema != null && schema.length() > 0 && !schema.equals(defaultNamespace)) {
instanceModel.schema = schema;
}
instanceModel.formVersion = e.getAttributeValue(null, "version");
instanceModel.uiVersion = e.getAttributeValue(null, "uiVersion");
XFormParser.loadNamespaces(e, instanceModel);
if (isMainInstance) {
// the initialization of the references is done twice.
// The first time is here because they are needed before these
// validation steps can be performed.
// It is then done again during the call to _f.setInstance().
FormDef.updateItemsetReferences(formDef.getChildren());
processRepeats(instanceModel);
verifyBindings(instanceModel, e.getName());
verifyActions(instanceModel);
}
applyInstanceProperties(instanceModel);
// print unused attribute warning message for parent element
if (XFormUtils.showUnusedAttributeWarning(e, usedAtts)) {
reporter.warning(XFormParserReporter.TYPE_UNKNOWN_MARKUP, XFormUtils.unusedAttWarning(e, usedAtts), getVagueLocation(e));
}
return instanceModel;
}
use of org.javarosa.core.model.instance.FormInstance in project javarosa by opendatakit.
the class XPathFuncExpr method eval.
/**
* Evaluate the function call.
*
* First check if the function is a member of the built-in function suite. If not, then check
* for any custom handlers registered to handler the function. If not, throw and exception.
*
* Both function name and appropriate arguments are taken into account when finding a suitable
* handler. For built-in functions, the number of arguments must match; for custom functions,
* the supplied arguments must match one of the function prototypes defined by the handler.
*/
public Object eval(DataInstance model, EvaluationContext evalContext) {
String name = id.toString();
Object[] argVals = new Object[args.length];
HashMap<String, IFunctionHandler> funcHandlers = evalContext.getFunctionHandlers();
// TODO: Func handlers should be able to declare the desire for short circuiting as well
if (name.equals("if")) {
assertArgsCount(name, args, 3);
return ifThenElse(model, evalContext, args, argVals);
} else if (name.equals("coalesce")) {
assertArgsCount(name, args, 2);
argVals[0] = args[0].eval(model, evalContext);
if (!isNull(argVals[0])) {
return argVals[0];
} else {
// that was null, so try the other one...
argVals[1] = args[1].eval(model, evalContext);
return argVals[1];
}
} else if (name.equals("indexed-repeat")) {
if ((args.length == 3 || args.length == 5 || args.length == 7 || args.length == 9 || args.length == 11)) {
return indexedRepeat(model, evalContext, args, argVals);
} else {
throw new XPathUnhandledException("function \'" + name + "\' requires " + "3, 5, 7, 9 or 11 arguments. Only " + args.length + " provided.");
}
}
for (int i = 0; i < args.length; i++) {
argVals[i] = args[i].eval(model, evalContext);
}
// check built-in functions
if (name.equals("true")) {
assertArgsCount(name, args, 0);
return Boolean.TRUE;
} else if (name.equals("false")) {
assertArgsCount(name, args, 0);
return Boolean.FALSE;
} else if (name.equals("boolean")) {
assertArgsCount(name, args, 1);
return toBoolean(argVals[0]);
} else if (name.equals("number")) {
assertArgsCount(name, args, 1);
return toNumeric(argVals[0]);
} else if (name.equals("int")) {
// non-standard
assertArgsCount(name, args, 1);
return toInt(argVals[0]);
} else if (name.equals("round")) {
// Proximate XPath 3.0 and Excel-style round(value,decimal place)
final int places;
if (args.length == 1) {
places = 0;
} else {
assertArgsCount(name, args, 2);
places = toNumeric(argVals[1]).intValue();
}
return round(toNumeric(argVals[0]), places);
} else if (name.equals("string")) {
assertArgsCount(name, args, 1);
return toString(argVals[0]);
} else if (name.equals("date")) {
// non-standard
assertArgsCount(name, args, 1);
return toDate(argVals[0], false);
} else if (name.equals("date-time")) {
// non-standard -- convert double/int/string to Date object
assertArgsCount(name, args, 1);
return toDate(argVals[0], true);
} else if (name.equals("decimal-date-time")) {
// non-standard -- convert string/date to decimal days off 1970-01-01T00:00:00.000-000
assertArgsCount(name, args, 1);
return toDecimalDateTime(argVals[0], true);
} else if (name.equals("decimal-time")) {
// non-standard -- convert string/date to decimal days off 1970-01-01T00:00:00.000-000
assertArgsCount(name, args, 1);
return toDecimalDateTime(argVals[0], false);
} else if (name.equals("not")) {
assertArgsCount(name, args, 1);
return boolNot(argVals[0]);
} else if (name.equals("boolean-from-string")) {
assertArgsCount(name, args, 1);
return boolStr(argVals[0]);
} else if (name.equals("format-date")) {
assertArgsCount(name, args, 2);
return formatDateTime(argVals[0], argVals[1]);
} else if (name.equals("abs")) {
// XPath 3.0
checkArity(name, 1, args.length);
return Math.abs(toDouble(argVals[0]));
} else if (name.equals("acos")) {
// XPath 3.0
checkArity(name, 1, args.length);
return Math.acos(toDouble(argVals[0]));
} else if (name.equals("asin")) {
// XPath 3.0
checkArity(name, 1, args.length);
return Math.asin(toDouble(argVals[0]));
} else if (name.equals("atan")) {
// XPath 3.0
checkArity(name, 1, args.length);
return Math.atan(toDouble(argVals[0]));
} else if (name.equals("atan2")) {
// XPath 3.0
checkArity(name, 2, args.length);
return Math.atan2(toDouble(argVals[0]), toDouble(argVals[1]));
} else if (name.equals("cos")) {
// XPath 3.0
checkArity(name, 1, args.length);
return Math.cos(toDouble(argVals[0]));
} else if (name.equals("exp")) {
// XPath 3.0
checkArity(name, 1, args.length);
return Math.exp(toDouble(argVals[0]));
} else if (name.equals("exp10")) {
// XPath 3.0
checkArity(name, 1, args.length);
return Math.pow(10.0, toDouble(argVals[0]));
} else if (name.equals("log")) {
// XPath 3.0
checkArity(name, 1, args.length);
return Math.log(toDouble(argVals[0]));
} else if (name.equals("log10")) {
// XPath 3.0
checkArity(name, 1, args.length);
return Math.log10(toDouble(argVals[0]));
} else if (name.equals("pi")) {
// XPath 3.0
checkArity(name, 0, args.length);
return Math.PI;
} else if (name.equals("sin")) {
// XPath 3.0
checkArity(name, 1, args.length);
return Math.sin(toDouble(argVals[0]));
} else if (name.equals("sqrt")) {
// XPath 3.0
checkArity(name, 1, args.length);
return Math.sqrt(toDouble(argVals[0]));
} else if (name.equals("tan")) {
// XPath 3.0
checkArity(name, 1, args.length);
return Math.tan(toDouble(argVals[0]));
} else if (name.equals("format-date-time")) {
// non-standard
assertArgsCount(name, args, 2);
return formatDateTime(argVals[0], argVals[1]);
} else if ((name.equals("selected") || name.equals("is-selected"))) {
// non-standard
assertArgsCount(name, args, 2);
return multiSelected(argVals[0], argVals[1], name);
} else if (name.equals("count-selected")) {
// non-standard
assertArgsCount(name, args, 1);
return countSelected(argVals[0]);
} else if (name.equals("selected-at")) {
// non-standard
assertArgsCount(name, args, 2);
return selectedAt(argVals[0], argVals[1]);
} else if (name.equals("position")) {
// TODO: Technically, only the 0 length argument is valid here.
if (args.length == 1) {
XPathNodeset nodes = (XPathNodeset) argVals[0];
if (nodes.size() == 0) {
// Will likely cause an error downstream when used in an XPath.
return (double) (1 + TreeReference.INDEX_UNBOUND);
} else {
// if or how this might manifest into a bug... .
return position(nodes.getRefAt(0));
}
} else if (args.length == 0) {
if (evalContext.getContextPosition() != -1) {
return (double) (1 + evalContext.getContextPosition());
}
return position(evalContext.getContextRef());
} else {
throw new XPathUnhandledException("function \'" + name + "\' requires either exactly one argument or no arguments. Only " + args.length + " provided.");
}
} else if (name.equals("count")) {
assertArgsCount(name, args, 1);
return count(argVals[0]);
} else if (name.equals("count-non-empty")) {
assertArgsCount(name, args, 1);
return countNonEmpty(argVals[0]);
} else if (name.equals("sum")) {
assertArgsCount(name, args, 1);
if (argVals[0] instanceof XPathNodeset) {
return sum(((XPathNodeset) argVals[0]).toArgList());
} else {
throw new XPathTypeMismatchException("not a nodeset");
}
} else if (name.equals("max")) {
if (args.length == 1 && argVals[0] instanceof XPathNodeset) {
return max(((XPathNodeset) argVals[0]).toArgList());
} else {
return max(argVals);
}
} else if (name.equals("min")) {
if (args.length == 1 && argVals[0] instanceof XPathNodeset) {
return min(((XPathNodeset) argVals[0]).toArgList());
} else {
return min(argVals);
}
} else if (name.equals("today")) {
assertArgsCount(name, args, 0);
return DateUtils.roundDate(new Date());
} else if (name.equals("now")) {
assertArgsCount(name, args, 0);
return new Date();
} else if (name.equals("concat")) {
if (args.length == 1 && argVals[0] instanceof XPathNodeset) {
return join("", ((XPathNodeset) argVals[0]).toArgList());
} else {
return join("", argVals);
}
} else if (name.equals("join") && args.length >= 1) {
if (args.length == 2 && argVals[1] instanceof XPathNodeset) {
return join(argVals[0], ((XPathNodeset) argVals[1]).toArgList());
} else {
return join(argVals[0], subsetArgList(argVals, 1));
}
} else if (name.equals("substr") && (args.length == 2 || args.length == 3)) {
return substring(argVals[0], argVals[1], args.length == 3 ? argVals[2] : null);
} else if (name.equals("contains") && args.length == 2) {
return toString(argVals[0]).contains(toString(argVals[1]));
} else if (name.equals("starts-with") && args.length == 2) {
return toString(argVals[0]).startsWith(toString(argVals[1]));
} else if (name.equals("ends-with") && args.length == 2) {
return toString(argVals[0]).endsWith(toString(argVals[1]));
} else if (name.equals("string-length")) {
assertArgsCount(name, args, 1);
return stringLength(argVals[0]);
} else if (name.equals("checklist") && args.length >= 2) {
// non-standard
if (args.length == 3 && argVals[2] instanceof XPathNodeset) {
return checklist(argVals[0], argVals[1], ((XPathNodeset) argVals[2]).toArgList());
} else {
return checklist(argVals[0], argVals[1], subsetArgList(argVals, 2));
}
} else if (name.equals("weighted-checklist") && args.length >= 2 && args.length % 2 == 0) {
// non-standard
if (args.length == 4 && argVals[2] instanceof XPathNodeset && argVals[3] instanceof XPathNodeset) {
Object[] factors = ((XPathNodeset) argVals[2]).toArgList();
Object[] weights = ((XPathNodeset) argVals[3]).toArgList();
if (factors.length != weights.length) {
throw new XPathTypeMismatchException("weighted-checklist: nodesets not same length");
}
return checklistWeighted(argVals[0], argVals[1], factors, weights);
} else {
return checklistWeighted(argVals[0], argVals[1], subsetArgList(argVals, 2, 2), subsetArgList(argVals, 3, 2));
}
} else if (name.equals("regex")) {
// non-standard
assertArgsCount(name, args, 2);
return regex(argVals[0], argVals[1]);
} else if (name.equals("depend") && args.length >= 1) {
// non-standard
return argVals[0];
} else if (name.equals("random")) {
// non-standard
assertArgsCount(name, args, 0);
// calculated expressions may be recomputed w/o warning! use with caution!!
return MathUtils.getRand().nextDouble();
} else if (name.equals("once")) {
assertArgsCount(name, args, 1);
XPathPathExpr currentFieldPathExpr = XPathPathExpr.fromRef(evalContext.getContextRef());
Object currValue = currentFieldPathExpr.eval(model, evalContext).unpack();
if (currValue == null || toString(currValue).length() == 0) {
// this is the "once" case
return argVals[0];
} else {
return currValue;
}
} else if (name.equals("uuid") && (args.length == 0 || args.length == 1)) {
// calculated expressions may be recomputed w/o warning! use with caution!!
if (args.length == 0) {
return PropertyUtils.genUUID();
}
int len = toInt(argVals[0]).intValue();
return PropertyUtils.genGUID(len);
} else if (name.equals("version")) {
// non-standard
assertArgsCount(name, args, 0);
final String formVersion = (model instanceof FormInstance) ? ((FormInstance) model).formVersion : "";
return formVersion == null ? "" : formVersion;
} else if (name.equals("property")) {
// non-standard
// return a property defined by the property manager.
// NOTE: Property should be immutable.
// i.e., does not work with 'start' or 'end' property.
assertArgsCount(name, args, 1);
String s = toString(argVals[0]);
return PropertyManager._().getSingularProperty(s);
} else if (name.equals("pow") && (args.length == 2)) {
// XPath 3.0
double a = toDouble(argVals[0]);
double b = toDouble(argVals[1]);
return Math.pow(a, b);
} else if (name.equals("enclosed-area") || name.equals("area")) {
assertArgsCount(name, args, 1);
Object argVal = argVals[0];
if (!(argVal instanceof XPathNodeset)) {
throw new XPathUnhandledException("function \'" + name + "\' requires a field as the parameter.");
}
Object[] argList = ((XPathNodeset) argVal).toArgList();
int repeatSize = argList.length;
List<GeoUtils.GPSCoordinates> gpsCoordinatesList;
if (repeatSize == 1) {
// Try to determine if the argument is of type GeoShapeData
try {
GeoShapeData geoShapeData = new GeoShapeData().cast(new UncastData(toString(argList[0])));
if (geoShapeData.points.size() <= 2) {
return 0d;
} else {
gpsCoordinatesList = new ArrayList<GeoUtils.GPSCoordinates>();
for (GeoPointData point : geoShapeData.points) {
gpsCoordinatesList.add(new GeoUtils.GPSCoordinates(point.getPart(0), point.getPart(1)));
}
}
} catch (Exception e) {
throw new XPathTypeMismatchException("The function \'" + name + "\' received a value that does not represent GPS coordinates: " + argList[0]);
}
} else {
if (repeatSize <= 2) {
return 0d;
} else {
// treat the input as a series of GeoPointData
gpsCoordinatesList = new ArrayList<GeoUtils.GPSCoordinates>();
for (Object arg : argList) {
try {
GeoPointData geoPointData = new GeoPointData().cast(new UncastData(toString(arg)));
gpsCoordinatesList.add(new GeoUtils.GPSCoordinates(geoPointData.getPart(0), geoPointData.getPart(1)));
} catch (Exception e) {
throw new XPathTypeMismatchException("The function \'" + name + "\' received a value that does not represent GPS coordinates: " + arg);
}
}
}
}
return GeoUtils.calculateAreaOfGPSPolygonOnEarthInSquareMeters(gpsCoordinatesList);
} else if (name.equals("digest") && (args.length == 2 || args.length == 3)) {
return DigestAlgorithm.from((String) argVals[1]).digest((String) argVals[0], args.length == 3 ? Encoding.from((String) argVals[2]) : Encoding.BASE64);
} else {
// check for custom handler
IFunctionHandler handler = funcHandlers.get(name);
if (handler != null) {
return evalCustomFunction(handler, argVals, evalContext);
} else {
throw new XPathUnhandledException("function \'" + name + "\'");
}
}
}
use of org.javarosa.core.model.instance.FormInstance in project javarosa by opendatakit.
the class CachingInstanceTemplateManager method getTemplateInstance.
/**
* Return the template model for the given form type. Serves the template out of the cache, if cached; fetches it
* fresh and caches it otherwise. If form types are restricted and the given form type is not allowed, throw an error
*/
public FormInstance getTemplateInstance(int formID) {
if (restrictFormTypes && !allowedFormTypes.contains(Integer.valueOf(formID))) {
throw new RuntimeException("form ID [" + formID + "] is not an allowed form type!");
}
FormInstance template = templateCache.get(Integer.valueOf(formID));
if (template == null) {
template = CompactInstanceWrapper.loadTemplateInstance(formID);
if (template == null) {
throw new RuntimeException("no formdef found for form id [" + formID + "]");
}
templateCache.put(Integer.valueOf(formID), template);
}
return template;
}
use of org.javarosa.core.model.instance.FormInstance in project javarosa by opendatakit.
the class ModelReferencePayload method memoize.
private void memoize() {
if (payload == null) {
IStorageUtility<FormInstance> instances = (IStorageUtility<FormInstance>) StorageManager.getStorage(FormInstance.STORAGE_KEY);
try {
FormInstance tree = instances.read(recordId);
payload = serializer.createSerializedPayload(tree);
} catch (IOException e) {
// Assertion, do not catch!
Std.printStack(e);
throw new RuntimeException("ModelReferencePayload failed to retrieve its model from rms [" + e.getMessage() + "]");
}
}
}
use of org.javarosa.core.model.instance.FormInstance in project javarosa by opendatakit.
the class XFormParserTest method serializeAndRestoreMetaNamespaceFormInstance.
@Test
public void serializeAndRestoreMetaNamespaceFormInstance() throws IOException {
// Given
ParseResult parseResult = parse(r("meta-namespace-form.xml"));
assertEquals(parseResult.formDef.getTitle(), "Namespace for Metadata");
assertNoParseErrors(parseResult);
FormDef formDef = parseResult.formDef;
TreeElement audit = findDepthFirst(formDef.getInstance().getRoot(), AUDIT_NODE);
TreeElement audit2 = findDepthFirst(formDef.getInstance().getRoot(), AUDIT_2_NODE);
TreeElement audit3 = findDepthFirst(formDef.getInstance().getRoot(), AUDIT_3_NODE);
assertNotNull(audit);
assertEquals(ORX_2_NAMESPACE_PREFIX, audit.getNamespacePrefix());
assertEquals(ORX_2_NAMESPACE_URI, audit.getNamespace());
assertNotNull(audit2);
assertEquals(ORX_2_NAMESPACE_PREFIX, audit2.getNamespacePrefix());
assertEquals(ORX_2_NAMESPACE_URI, audit2.getNamespace());
assertNotNull(audit3);
assertEquals(null, audit3.getNamespacePrefix());
assertEquals(null, audit3.getNamespace());
audit.setAnswer(new StringData(AUDIT_ANSWER));
audit2.setAnswer(new StringData(AUDIT_2_ANSWER));
audit3.setAnswer(new StringData(AUDIT_3_ANSWER));
// When
// serialize the form instance
XFormSerializingVisitor serializer = new XFormSerializingVisitor();
ByteArrayPayload xml = (ByteArrayPayload) serializer.createSerializedPayload(formDef.getInstance());
copy(xml.getPayloadStream(), FORM_INSTANCE_XML_FILE_NAME, REPLACE_EXISTING);
// restore (deserialize) the form instance
byte[] formInstanceBytes = readAllBytes(FORM_INSTANCE_XML_FILE_NAME);
FormInstance formInstance = XFormParser.restoreDataModel(formInstanceBytes, null);
// Then
audit = findDepthFirst(formInstance.getRoot(), AUDIT_NODE);
audit2 = findDepthFirst(formInstance.getRoot(), AUDIT_2_NODE);
audit3 = findDepthFirst(formInstance.getRoot(), AUDIT_3_NODE);
assertNotNull(audit);
assertEquals(ORX_2_NAMESPACE_PREFIX, audit.getNamespacePrefix());
assertEquals(ORX_2_NAMESPACE_URI, audit.getNamespace());
assertEquals(AUDIT_ANSWER, audit.getValue().getValue());
assertNotNull(audit2);
assertEquals(ORX_2_NAMESPACE_PREFIX, audit2.getNamespacePrefix());
assertEquals(ORX_2_NAMESPACE_URI, audit2.getNamespace());
assertEquals(AUDIT_2_ANSWER, audit2.getValue().getValue());
assertNotNull(audit3);
assertEquals(null, audit3.getNamespacePrefix());
assertEquals(null, audit3.getNamespace());
assertEquals(AUDIT_3_ANSWER, audit3.getValue().getValue());
}
Aggregations