Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to execute the suffixes Ognl, SpEL, Groovy, Jexl3 before dynamic expression statements in Java

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to implement the suffixes Ognl, SpEL, Groovy, Jexl3 before the dynamic expression statement in Java, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.

Ognl 、 SpEL 、 Groovy 、 Jexl3

In some rule sets or workflow projects, it is common to encounter dynamic parsing expressions and performing the function of producing results.

The rule engine is a component embedded in the application, which can separate the business rules from the business code and use pre-defined semantic specifications to implement the stripped business rules; by accepting the input data, the rule engine evaluates the business rules and makes business decisions.

Workflow (Workflow) is an abstract and general description of the business rules between the workflow and its operating steps. Workflow modeling, that is, the logic and rules of how the work in the workflow is organized together, expressed in the appropriate model in the computer and calculated. The main problem to be solved in workflow is to use computers to automatically transfer documents, information or tasks among multiple participants according to some predetermined rules in order to achieve a business goal.

1. Brief description of prefix, suffix, prefix, suffix expression (inverse Polish expression)

The earliest contact with expression parsing is when it comes to the data structure, when the lesson assignment is to "do a simple four mixed operation statement parsing and calculate the result", which is simply a calculator.

2. Infix expression

An expression that writes an operator between two operands is called an infix expression.

Infix expressions are the ones we are most familiar with and the easiest to read

For example: 12 + 34 + 5 * 6-30 / 5

That is, our commonly used mathematical expressions are expressed by infix expressions.

3. Suffix expression

An expression that writes an operator after two operands is called a suffix expression.

12 34 + 56 * + 30 5 /-

The prefix expression needs to be read from left to right. If you encounter an algorithm, take 2 operands from the left to perform the operation.

Reading from left to right can be divided into ((12 34 +) (56 *) +) (30 / 5)-

Note: parentheses are only auxiliary, in fact, there is no

4. Prefix expression

A prefix expression is an expression that writes an operator before two operands.

The prefix expression needs to be read from right to left. If an algorithm is encountered, two operands will be taken from the right to operate.

12 + 34 + 5 * 6-30 / 5

-+ + 12 34 * 5 6 / 30 5

Infix: 12 + 34 + 5 * 6-30 / 5

Suffix: 12 34 + 5 6 * + 30 5 /-

Prefix:-+ + 12 34 * 5 6 / 30 5

II. OGNL

OGNL (abbreviation of Object-Graph Navigation Language), an object graph navigation language, is an expression language that is not only used to set and obtain properties of Java objects, but also provides things such as projection and filtering of collections and lambda expressions.

Introduce dependencies:

Ognl ognl 3.2.18MemberAccess memberAccess = new AbstractMemberAccess () {@ Override public boolean isAccessible (Map context, Object target, Member member, String propertyName) {int modifiers = member.getModifiers (); return Modifier.isPublic (modifiers);}}; OgnlContext context = (OgnlContext) Ognl.createDefaultContext (this, memberAccess, new DefaultClassResolver (), new DefaultTypeConverter ()); context.put ("verifyStatus", 1); Object expression = Ognl.parse_Expression ("# verifyStatus = = 1") Boolean result = (boolean) Ognl.getValue (expression, context, context.getRoot ()); Assert.assertTrue (result); III. SpEL

SpEL (Spring Expression Language), the Spring expression language. It is an expression language similar to JSP's EL expression, but more powerful and useful than the latter.

ExpressionParser parser = new SpelExpressionParser (); Expression expression = parser.parse_Expression ("# verifyStatus = = 1"); EvaluationContext context = new StandardEvaluationContext (); context.setVariable ("verifyStatus", 1); boolean result = (boolean) expression.getValue (context); Assert.assertTrue (result); IV. Jexl/Jexl3

Introduce dependencies:

Org.apache.commons commons-jexl3 3.1

Execute a simple expression:

JexlEngine jexl = new JexlBuilder (). Create (); JexlContext jc = new MapContext (); jc.set ("verifyStatus", 1); JexlExpression expression = jexl.create_Expression ("verifyStatus = = 1"); boolean result = (boolean) expression.evaluate (jc); Assert.assertTrue (result); V. Groovy

Groovy is a good choice because it has complete parsing and execution functions of Groovy and Java syntax.

Introduce dependencies, which can introduce the latest version as needed

Org.codehaus.groovy groovy 2.5.6

Execute the expression:

Binding binding = new Binding (); binding.setVariable ("verifyStatus", 1); GroovyShell shell = new GroovyShell (binding); boolean result = (boolean) shell.evaluate ("verifyStatus = = 1"); Assert.assertTrue (result); VI. Extension

Those who often use MyBatis must have used dynamic statements

Select id, invite_code, phone, name from user where status = 1 and invite_code = # {inviteCode}

Let's simplify here.

This example is mainly for illustration and may not be easy to use, where @ if is equivalent to the above

Select id, invite_code, phone, name from user where status = 1 @ if (: inviteCode! = null) {and invite_code =: inviteCode}

In dealing with this kind of SQL, we can first use regularity to separate @ if from normal statements.

List results = StringUtil.matches (sql, "@ if ([\\ s\ S] *?)}")

Match to @ if (: inviteCode! = null) {and invite_code =: inviteCode} in this way

Then separate the expression that needs to be evaluated from the SQL to be spliced

String text = "@ if (: inviteCode! = null) {and invite_code =: inviteCode}"; List sqlFragment = StringUtil.matches (text, "\\ (([\\ s\ S] *?))\\) |\ {([\\ s\ S] *?)\}")

Separate out

: inviteCode! = null

And invite_code =: inviteCode

Where: inviteCode! = null is a statement that needs to be processed dynamically. For: inviteCode! = null, we need to identify those variable names that need to be copied.

List sqlFragmentParam = StringUtil.matches (": inviteCode! = null", "\\?\\ d + (\. [A-Za-z] +)? |: [A-Za-z0-9] + (\. [A-Za-z] +)?")

Get the inviteCode and find the corresponding value in some way

Specific code, for reference only:

JexlEngine jexl = new JexlBuilder (). Create (); JexlContext jc = new MapContext (); jc.set (": inviteCode", "ddddsdfa"); JexlExpression expression = jexl.create_Expression (sqlExp); boolean needAppendSQL = (boolean) expression.evaluate (jc)

Through needAppendSQL to decide whether to splice SQL, such a simple dynamic SQL is implemented. The above is written in Jexl, and you can change it to any of the above schemes. Here is only a demonstration.

@ Testpublic void testSQL () {String sql = "select id, invite_code, phone, name\ n" + "from user\ n" + "where status = 1\ n" + "@ if (: inviteCode! = null) {and invite_code =: inviteCode}"; Map params = new HashMap (); params.put ("inviteCode", "dd"); System.out.println (parseJexl (sql, params)) } public String parseJexl (String jexlSql, Map params) {/ / determine whether it contains @ if List results = StringUtil.matches (jexlSql, "@ if ([\\ s\ S] *?)}"); if (results.isEmpty ()) {return jexlSql;} JexlEngine jexl = new JexlBuilder (). Create (); JexlContext jc = new MapContext () For (String e: results) {List sqlFragment = StringUtil.matches (e, "\\ (([\\ s\ S] *?)\) |\ {([\\ s\ S] *?)\}"); String sqlExp = sqlFragment.get (0). Trim (). Substring (1, sqlFragment.get (0). Length ()-1) List sqlFragmentParam = StringUtil.matches (sqlExp, "\?\ d + (\\. [A-Za-z] +)? |: [A-Za-z0-9] + (\. [A-Za-z] +)?"); for (String param: sqlFragmentParam) {String newSQLExp = "_" + param.substring (1); sqlExp = sqlExp.replace (param, newSQLExp); jc.set (newSQLExp, params.get (param.substring (1) } JexlExpression expression = jexl.create_Expression (sqlExp); Boolean needAppendSQL = (Boolean) expression.evaluate (jc); if (needAppendSQL) {jexlSql = jexlSql.replace (e, sqlFragment.get (1). Trim (). Substring (1, sqlFragment.get (1). Length ()-1);} else {jexlSql = jexlSql.replace (e, ");}} return jexlSql } Thank you for reading this article carefully. I hope the article "how to execute the suffixes Ognl, SpEL, Groovy, Jexl3 in Java" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report