use of org.apache.jmeter.testelement.TestStateListener in project jmeter by apache.
the class FunctionParser method makeFunction.
/**
* Compile a string into a function or SimpleVariable.
*
* Called by {@link #compileString(String)} when that has detected "${".
*
* Calls {@link CompoundVariable#getNamedFunction(String)} if it detects:
* '(' - start of parameter list
* '}' - end of function call
*
* @param reader points to input after the "${"
* @return the function or variable object (or a String)
* @throws InvalidVariableException when evaluation of variables fail
*/
Object makeFunction(StringReader reader) throws InvalidVariableException {
char[] current = new char[1];
// TODO - why use space?
char previous = ' ';
StringBuilder buffer = new StringBuilder();
Object function;
try {
while (reader.read(current) == 1) {
if (current[0] == '\\') {
if (reader.read(current) == 0) {
break;
}
previous = ' ';
buffer.append(current[0]);
} else if (current[0] == '(' && previous != ' ') {
String funcName = buffer.toString();
function = CompoundVariable.getNamedFunction(funcName);
if (function instanceof Function) {
((Function) function).setParameters(parseParams(reader));
if (reader.read(current) == 0 || current[0] != '}') {
// set to start of string
reader.reset();
char[] cb = new char[100];
int nbRead = reader.read(cb);
throw new InvalidVariableException("Expected } after " + funcName + " function call in " + new String(cb, 0, nbRead));
}
if (function instanceof TestStateListener) {
StandardJMeterEngine.register((TestStateListener) function);
}
return function;
} else {
// Function does not exist, so treat as per missing variable
buffer.append(current[0]);
}
} else if (current[0] == '}') {
// variable, or function with no parameter list
function = CompoundVariable.getNamedFunction(buffer.toString());
if (function instanceof Function) {
// ensure that setParameters() is called.
((Function) function).setParameters(new LinkedList<CompoundVariable>());
}
buffer.setLength(0);
return function;
} else {
buffer.append(current[0]);
previous = current[0];
}
}
} catch (IOException e) {
log.error("Error parsing function: {}", buffer, e);
return null;
}
log.warn("Probably an invalid function string: {}", buffer);
return buffer.toString();
}
Aggregations