Class XPathFuncExpr
- All Implemented Interfaces:
Serializable,Externalizable
public class XPathFuncExpr extends XPathExpression
All of the built-in xpath functions are included here, as well as the xpath type conversion logic
Evaluation of functions can delegate out to custom function handlers that must be registered at runtime.
- Author:
- Drew Roos
- See Also:
- Serialized Form
-
Field Summary
Fields Modifier and Type Field Description XPathExpression[]argsXPathQNameid -
Constructor Summary
Constructors Constructor Description XPathFuncExpr()XPathFuncExpr(XPathQName id, XPathExpression[] args) -
Method Summary
Modifier and Type Method Description static BooleanboolNot(Object o)static BooleanboolStr(Object o)static Booleanchecklist(Object oMin, Object oMax, Object[] factors)perform a 'checklist' computation, enabling expressions like 'if there are at least 3 risk factors active'static BooleanchecklistWeighted(Object oMin, Object oMax, Object[] flags, Object[] weights)very similar to checklist, only each factor is assigned a real-number 'weight'.static Doublecount(Object o)count the number of nodes in a nodesetstatic DoublecountSelected(Object o)return the number of choices in a multi-select answerbooleanequals(Object o)Objecteval(DataInstance model, EvaluationContext evalContext)Evaluate the function call.static StringformatDateTime(Object inputValue, Object format)static ObjectifThenElse(DataInstance model, EvaluationContext ec, XPathExpression[] args, Object[] argVals)static ObjectindexedRepeat(DataInstance model, EvaluationContext ec, XPathExpression[] args, Object[] argVals)This provides a method of indexing fields stored in prior repeat groups.static booleanisNull(Object o)HANDLERS FOR BUILT-IN FUNCTIONS ******** the functions below are the handlers for the built-in xpath function suite if you add a function to the suite, it should adhere to the following pattern: * the function takes in its arguments as objects (DO NOT cast the arguments when calling the handler up in eval() (i.e., return stringLength((String)argVals[0]) <--- NO!) * the function converts the generic argument(s) to the desired type using the built-in xpath type conversion functions (toBoolean(), toNumeric(), toString(), toDate()) * the function MUST return an object of type Boolean, Double, String, or Date; it may never return null (instead return the empty string or NaN) * the function may throw exceptions, but should try as hard as possible not to, and if it must, strive to make it an XPathExceptionstatic Stringjoin(Object oSep, Object[] argVals)concatenate an arbitrary-length argument list of string values togetherstatic BooleanmultiSelected(Object o1, Object o2, String functionName)return whether a particular choice of a multi-select is selectedstatic StringnormalizeSpace(Object o)Objectpivot(DataInstance model, EvaluationContext evalContext, List<Object> pivots, Object sentinal)Pivot this expression, returning values if appropriate, and adding any pivots to the list.voidreadExternal(DataInputStream in, PrototypeFactory pf)static Booleanregex(Object o1, Object o2)determine if a string matches a regular expression.static StringselectedAt(Object o1, Object o2)Get the Nth item in a selected liststatic DoublestringLength(Object o)static Stringsubstring(Object o1, Object o2, Object o3)static Doublesum(Object[] argVals)sum the values in a nodeset; each element is coerced to a numeric valuestatic BooleantoBoolean(Object o)convert a value to a boolean using xpath's type conversion rulesstatic ObjecttoDate(Object input, boolean preserveTime)Convert a value to aDate.static ObjecttoDecimalDateTime(Object o, boolean keepDate)static DoubletoDouble(Object o)static DoubletoInt(Object o)convert a number to an integer by truncating the fractional part.static DoubletoNumeric(Object o)convert a value to a number using xpath's type conversion rules (note that xpath itself makes no distinction between integer and floating point numbers)StringtoString()static StringtoString(Object o)convert a value to a string using xpath's type conversion rulesstatic Objectunpack(Object o)voidwriteExternal(DataOutputStream out)Methods inherited from class org.javarosa.xpath.expr.XPathExpression
eval, hashCode, pivot, print, printParseTree
-
Field Details
-
Constructor Details
-
XPathFuncExpr
public XPathFuncExpr() -
XPathFuncExpr
-
-
Method Details
-
toString
-
equals
-
readExternal
public void readExternal(DataInputStream in, PrototypeFactory pf) throws IOException, DeserializationException- Throws:
IOExceptionDeserializationException
-
writeExternal
- Throws:
IOException
-
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.
- Specified by:
evalin classXPathExpression
-
isNull
HANDLERS FOR BUILT-IN FUNCTIONS ******** the functions below are the handlers for the built-in xpath function suite if you add a function to the suite, it should adhere to the following pattern: * the function takes in its arguments as objects (DO NOT cast the arguments when calling the handler up in eval() (i.e., return stringLength((String)argVals[0]) <--- NO!) * the function converts the generic argument(s) to the desired type using the built-in xpath type conversion functions (toBoolean(), toNumeric(), toString(), toDate()) * the function MUST return an object of type Boolean, Double, String, or Date; it may never return null (instead return the empty string or NaN) * the function may throw exceptions, but should try as hard as possible not to, and if it must, strive to make it an XPathException -
stringLength
-
normalizeSpace
-
toBoolean
convert a value to a boolean using xpath's type conversion rules -
toDouble
-
toNumeric
convert a value to a number using xpath's type conversion rules (note that xpath itself makes no distinction between integer and floating point numbers) -
toInt
convert a number to an integer by truncating the fractional part. if non-numeric, coerce the value to a number first. note that the resulting return value is still a Double, as required by the xpath engine -
toString
convert a value to a string using xpath's type conversion rules -
toDate
Convert a value to aDate. note that xpath has no intrinsic representation of dates, so this is off-spec.Dates convert to strings as 'yyyy-mm-dd', convert to numbers as # of days since the unix epoch, and convert to booleans always as 'true'.
This function parses input values to dates. Input values can be: * A well formatted ISO8601 string representation of a date, with or without time. Examples: '2018-01-01', '2018-01-01T10:20:30.400', '2018-01-01T10:20:30.400+02' * An epoch integer measured in days (Days since 1970-01-01, negative values are allowed)
Some values won't get parsed and will be returned as is without throwing an exception: * Empty string * Double.NaN (used by xpath as 'null value') * A value that is already a Date
Any other value will throw an exception. Specifically: * Double.POSITIVE_INFINITY and Double.NEGATIVE_INFINITY * A double value less than Integer.MIN_VALUE or greater than Integer.MAX_VALUE * A non parseable string
-
toDecimalDateTime
-
boolNot
-
boolStr
-
formatDateTime
-
ifThenElse
public static Object ifThenElse(DataInstance model, EvaluationContext ec, XPathExpression[] args, Object[] argVals) -
indexedRepeat
public static Object indexedRepeat(DataInstance model, EvaluationContext ec, XPathExpression[] args, Object[] argVals) throws XPathTypeMismatchExceptionThis provides a method of indexing fields stored in prior repeat groups.args[0] = generic XPath expression to index args[1] = generic XPath expression for group to index args[2] = index number for group args[3] = generic XPath expression for add'l group to index (if 5 or 7 parameters passed) args[4] = index number for group (if 5 or 7 parameters passed) args[5] = generic XPath expression for add'l group to index (if 7 parameters passed) args[6] = index number for group (if 7 parameters passed)
- Throws:
XPathTypeMismatchException
-
multiSelected
return whether a particular choice of a multi-select is selected- Parameters:
o1- XML-serialized answer to multi-select question (i.e, space-delimited choice values)o2- choice to look for
-
countSelected
return the number of choices in a multi-select answer- Parameters:
o- XML-serialized answer to multi-select question (i.e, space-delimited choice values)
-
selectedAt
Get the Nth item in a selected list- Parameters:
o1- XML-serialized answer to multi-select question (i.e, space-delimited choice values)o2- the integer index into the list to return
-
count
count the number of nodes in a nodeset -
sum
sum the values in a nodeset; each element is coerced to a numeric value -
join
concatenate an arbitrary-length argument list of string values together -
substring
-
checklist
perform a 'checklist' computation, enabling expressions like 'if there are at least 3 risk factors active'the first argument is a numeric value expressing the minimum number of factors required. if -1, no minimum is applicable the second argument is a numeric value expressing the maximum number of allowed factors. if -1, no maximum is applicalbe arguments 3 through the end are the individual factors, each coerced to a boolean value
- Returns:
- true if the count of 'true' factors is between the applicable minimum and maximum, inclusive
-
checklistWeighted
public static Boolean checklistWeighted(Object oMin, Object oMax, Object[] flags, Object[] weights)very similar to checklist, only each factor is assigned a real-number 'weight'.the first and second args are again the minimum and maximum, but -1 no longer means 'not applicable'.
subsequent arguments come in pairs: first the boolean value, then the floating-point weight for that value
the weights of all the 'true' factors are summed, and the function returns whether this sum is between the min and max
-
regex
determine if a string matches a regular expression.- Parameters:
o1- string being matchedo2- regular expression
-
unpack
-
pivot
public Object pivot(DataInstance model, EvaluationContext evalContext, List<Object> pivots, Object sentinal) throws UnpivotableExpressionExceptionDescription copied from class:XPathExpressionPivot this expression, returning values if appropriate, and adding any pivots to the list.- Overrides:
pivotin classXPathExpression- Parameters:
model- The model to evaluate the current expression againstevalContext- The evaluation context to evaluate againstpivots- The list of pivot points in the xpath being evaluated. Pivots should be added to this list.sentinal- The value which is being pivoted around.- Returns:
- null - If a pivot was identified in this expression sentinal - If the current expression represents the sentinal being pivoted any other value - The result of the expression if no pivots are detected
- Throws:
UnpivotableExpressionException- If the expression is too complex to pivot
-