-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathStampExpression.java
More file actions
59 lines (50 loc) · 2.08 KB
/
StampExpression.java
File metadata and controls
59 lines (50 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package nodebox.node;
public class StampExpression {
private Parameter parameter;
private String stampKey;
private Expression expression;
/**
* Create a new stamp expression. The stamp expression should be set as the value of the given parameter,
* in the format "width = CNUM * 5"
*
* @param node the node this expression operates on.
* @param parameterName the name of the parameter for this expression.
*/
public StampExpression(Node node, String parameterName) {
if (!node.hasParameter(parameterName)) {
throw new IllegalArgumentException("The node \"" + node.getName() + "\" has no parameter \"" + parameterName + "\".");
}
parameter = node.getParameter(parameterName);
String stampExpression = parameter.asString();
if (stampExpression.trim().length() == 0) return;
// Split the stamp expression into the key and the actual expression.
int equalsPos = stampExpression.indexOf('=');
if (equalsPos < 0) {
throw new IllegalArgumentException("The stamp expression \"" + stampExpression + "\" is not in the format \"width = CNUM * 5\"");
}
stampKey = stampExpression.substring(0, equalsPos);
String expressionString = stampExpression.substring(equalsPos + 1);
// Convert the expression string to an Expression object.
expression = new Expression(parameter, expressionString);
}
public Parameter getParameter() {
return parameter;
}
public String getStampKey() {
return stampKey;
}
public Expression getExpression() {
return expression;
}
/**
* Evaluate the expression and store the result under the stamp expression key.
*
* @param context the current processing context
* @throws ExpressionError if an error occurs in the expression
*/
public void evaluate(ProcessingContext context) throws ExpressionError {
if (expression == null) return;
Object result = expression.evaluate(context);
context.put(stampKey, result);
}
}