package fr.pandacube.lib.permissions; import java.util.Iterator; import java.util.function.Function; import com.fathzer.soft.javaluator.AbstractEvaluator; import com.fathzer.soft.javaluator.BracketPair; import com.fathzer.soft.javaluator.Constant; import com.fathzer.soft.javaluator.Operator; import com.fathzer.soft.javaluator.Operator.Associativity; import com.fathzer.soft.javaluator.Parameters; /** * Class that evaluates a permission string as if it was a boolean expression with permission nodes as variables. *
* A permission expression contains permission nodes, boolean operators ({@code "||"}, {@code "&&"} and {@code "!"}) and * literal values {@code "true"} and {@code "false"}. * Here are some example of permission expressions: *
{@code * "p1.cmd" * "!p1.toto" * "p1.cmd!" * "p1.cmd || p1.toto" * "p1.cmd && p1.toto" * "p1.cmd && !p1.toto " * "p1.cmd && true" * "false || p2.cmd" * }* Notice that spaces around permission nodes and operators does not affect the results of the parsing. */ public class PermissionExpressionParser { private static final PermissionEvaluator PERMISSION_EVALUATOR = new PermissionEvaluator(); /** * Evaluate the provided permission expression, testing each permission with the provided permTester. * * @param permString the permission expression to evaluate. * @param permTester a function that gives the value of the provided permission node. It is usually a method * reference to the {@code hasPermission(String)} method the player we want to test the * permissions. * @throws IllegalArgumentException if the expression is not correct. * @return the result of the evaluation of the permission expression. */ public static boolean evaluate(String permString, LiteralPermissionTester permTester) { try { return PERMISSION_EVALUATOR.evaluate(permString, permTester); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Can’t evaluate the provided permission expression: '" + permString + "'", e); } } /** * Functional interface that converts a string into a boolean. */ public interface LiteralPermissionTester extends Function