Today, at 38, I was a little depressed when I was not looking for anything. k!
Bored, write something for urgent use, starting from using the JSP permission label.
Logical expression parser
Author: zfzheng 2008-03-08
Purpose:
Embedded logical expressions, such
JSP Tag:
1) paging information
<X: Page footable = "pagecount> 0">... </X: Page>
2) The permission value is greater than that of a common user and is not an administrator.
<X: AUTH role = "User> normal & user! = Admin ">... </X: AUTH>
Operands and operation types:
1) value (integer or floating point type): ==>=<=<> /! =
2) Boolean: & |!
Java objects: Express, Boolean, String, INT/float
Logical expression syntax:
Expx-> exp {op expx}
EXP-> tag | "(" expx ")" | op expx
Op-> ==|>|>=|<|<=|<>|! = | & |!
Tag-> Boolean | Number | {text} +
Boolean-> true | false
Number-> [0-9] + (. [0-9] + )?
Text-> charset
Expx and exp use the same Java object. The syntax extracts the following reasons: 1) continuous expressions are supported; 2) left recursion is avoided.
[Code annotation]
Analyzer: lexer and expressparser
Syntax tree objects: Express and Operation
Execution helper classes: evalcommand and valueprovider
1. expressparser: written in grammar description
2. Express:
Two-eye operation, single camera (!) The left operand of the operation is null.
(Expx) expression root node, only the left operation tree, op, right operand is empty.
Evaluate the traversal order: LTR (left-right) is used. Pay attention to the value of the Left node. Recursive operation is performed first. If an aggregation node is encountered, that is, the root node of the scratch expression, the current recursive priority is obtained.
Provides eval evaluation methods.
3. Operation: Operator object. In the mona1 mode, the operand is delayed to the method parameter, saving memory.
4. evalcommand: Evaluate command, which is created in express # eval and executed in operation # eval.
This type of function: Evaluate the latency to avoid unnecessary computation. For example, true | exp, exp is not required.
5. valueprovider: The variable provider for easy integration and use.
Public void testlogic () throws exception {
Expressparser Ep = new expressparser ();
String S = "User> normal & user! = Admin ";
Express E = ep. parse (s );
Staticvalueprovider Vp = new staticvalueprovider ();
VP. setvalue ("admin", 10 );
VP. setvalue ("normal", 1 );
VP. setvalue ("user", 5 );
Object ret = E. eval (VP );
Assertequals (true, RET );
VP. setvalue ("user", 10 );
Ret = E. eval (VP );
Assertequals (false, RET );
}