Java Reference
In-Depth Information
The Expression Class
The Expression class contains the main logic for parsing and evaluating an arithmetic
expression. Listing 8-1 contains the complete code for the Expression class.
Listing 8-1. The Expression Class That Parses and Evaluates an Arithmetic Expression
// Expression.java
package com.jdojo.script;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.script.ScriptContext;
public class Expression {
private String exp;
private ScriptContext context;
private String op1;
private char op1Sign = '+';
private String op2;
private char op2Sign = '+';
private char operation;
private boolean parsed;
public Expression(String exp, ScriptContext context) {
if (exp == null || exp.trim().equals("")) {
throw new IllegalArgumentException(this.
getErrorString());
}
this.exp = exp.trim();
if (context == null) {
throw new IllegalArgumentException(
"ScriptContext cannot be null.");
}
this.context = context;
}
public String getExpression() {
return exp;
}
 
Search WWH ::




Custom Search