| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2009 - 2010 Cloudsmith Inc. and others. |
| 3 |
* |
| 4 |
* This program and the accompanying materials |
| 5 |
* are made available under the terms of the Eclipse Public License 2.0 |
| 6 |
* which accompanies this distribution, and is available at |
| 7 |
* https://www.eclipse.org/legal/epl-2.0/ |
| 8 |
* |
| 9 |
* SPDX-License-Identifier: EPL-2.0 |
| 10 |
* |
| 11 |
* Contributors: |
| 12 |
* Cloudsmith Inc. - initial API and implementation |
| 13 |
*******************************************************************************/ |
| 14 |
package org.eclipse.equinox.p2.metadata.expression; |
| 15 |
|
| 16 |
import org.eclipse.equinox.internal.p2.metadata.expression.*; |
| 17 |
import org.eclipse.equinox.internal.p2.metadata.expression.parser.LDAPFilterParser; |
| 18 |
import org.eclipse.equinox.internal.p2.metadata.expression.parser.QLParser; |
| 19 |
|
| 20 |
/** |
| 21 |
* Global access to factory, parser, and methods for introspection |
| 22 |
* @noextend This class is not intended to be subclassed by clients. |
| 23 |
* @noinstantiate This class is not intended to be instantiated by clients. |
| 24 |
* @since 2.0 |
| 25 |
*/ |
| 26 |
public final class ExpressionUtil { |
| 27 |
private static final IExpressionParser expressionParser = new QLParser(ExpressionFactory.INSTANCE); |
| 28 |
private static final LDAPFilterParser ldapFilterParser = new LDAPFilterParser(ExpressionFactory.INSTANCE); |
| 29 |
public static final IExpression TRUE_EXPRESSION = ExpressionFactory.INSTANCE.constant(Boolean.TRUE); |
| 30 |
public static final IExpression FALSE_EXPRESSION = ExpressionFactory.INSTANCE.constant(Boolean.FALSE); |
| 31 |
|
| 32 |
private ExpressionUtil() { |
| 33 |
//We don't want to ppl to instantiate this class |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Returns the global expression factory |
| 38 |
* @return The global expression factory. |
| 39 |
*/ |
| 40 |
public static IExpressionFactory getFactory() { |
| 41 |
return ExpressionFactory.INSTANCE; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Creates and returns a new expression parser |
| 46 |
* @return The new parser |
| 47 |
*/ |
| 48 |
public static IExpressionParser getParser() { |
| 49 |
return expressionParser; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Parse an LDAP filter from the <code>filter</code> string. If <code>filter</code> is <code>null</code> |
| 54 |
* or a string that is empty or only consists of whitespace, then this method returns <code>null</code>. |
| 55 |
* @param filter The filter to parse. Can be <code>null</code> or empty. |
| 56 |
* @return An expression that corresponds to the LDAP filter or <code>null</code>. |
| 57 |
* @throws ExpressionParseException If the syntax was invalid |
| 58 |
*/ |
| 59 |
public static IFilterExpression parseLDAP(String filter) throws IllegalArgumentException { |
| 60 |
filter = trimmedOrNull(filter); |
| 61 |
return filter == null ? null : ldapFilterParser.parse(filter); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Create a new expression. The expression will have access to the global |
| 66 |
* variable 'this' and to the context parameters. |
| 67 |
* @param expression The string representing the boolean expression. |
| 68 |
* @return The resulting expression tree. |
| 69 |
* @throws ExpressionParseException If the syntax was invalid |
| 70 |
*/ |
| 71 |
public static IExpression parse(String expression) { |
| 72 |
expression = trimmedOrNull(expression); |
| 73 |
return expression == null ? null : getParser().parse(expression); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Create an arbitrary expression. The expression will have access to the global |
| 78 |
* variable 'everything' and to the context parameters. |
| 79 |
* @param expression The string representing the boolean expression. |
| 80 |
* @return The resulting expression tree. |
| 81 |
* @throws ExpressionParseException If the syntax was invalid |
| 82 |
*/ |
| 83 |
public static IExpression parseQuery(String expression) { |
| 84 |
expression = trimmedOrNull(expression); |
| 85 |
return expression == null ? null : getParser().parseQuery(expression); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* If <code>str</code> is <code>null</code>, then this method returns <code>null</code>. |
| 90 |
* Otherwise <code>str</code> is trimmed from whitespace at both ends. If the result |
| 91 |
* of the trim is an empty string, then <code>null</code> is returned, otherwise the |
| 92 |
* result of the trim is returned. |
| 93 |
* @param str The string to trim. Can be <code>null</code>. |
| 94 |
* @return The trimmed string or <code>null</code>. |
| 95 |
*/ |
| 96 |
public static String trimmedOrNull(String str) { |
| 97 |
if (str != null) { |
| 98 |
str = str.trim(); |
| 99 |
if (str.length() == 0) |
| 100 |
str = null; |
| 101 |
} |
| 102 |
return str; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Obtains the Left Hand Side (LHS) of a binary expression. |
| 107 |
* @param expression The expression to introspect |
| 108 |
* @return The left hand side operator |
| 109 |
* @throws IllegalArgumentException if the expression is not a binary expression |
| 110 |
* @see IExpression#TYPE_AT |
| 111 |
* @see IExpression#TYPE_EQUALS |
| 112 |
* @see IExpression#TYPE_GREATER |
| 113 |
* @see IExpression#TYPE_GREATER_EQUAL |
| 114 |
* @see IExpression#TYPE_LESS |
| 115 |
* @see IExpression#TYPE_LESS_EQUAL |
| 116 |
* @see IExpression#TYPE_MATCHES |
| 117 |
* @see IExpression#TYPE_NOT_EQUALS |
| 118 |
*/ |
| 119 |
public static IExpression getLHS(IExpression expression) { |
| 120 |
if (expression instanceof Binary) |
| 121 |
return ((Binary) expression).lhs; |
| 122 |
throw new IllegalArgumentException(); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Obtains the name of a variable or member expression. |
| 127 |
* @param expression The expression to introspect |
| 128 |
* @return The name of the expression |
| 129 |
* @throws IllegalArgumentException if the expression is not a variable or a member |
| 130 |
* @see IExpression#TYPE_MEMBER |
| 131 |
* @see IExpression#TYPE_VARIABLE |
| 132 |
*/ |
| 133 |
public static String getName(IExpression expression) { |
| 134 |
if (expression instanceof Member) |
| 135 |
return ((Member) expression).getName(); |
| 136 |
if (expression instanceof Variable) |
| 137 |
return ((Variable) expression).getName(); |
| 138 |
throw new IllegalArgumentException(); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Obtains the operand of an unary expression |
| 143 |
* @param expression The expression to introspect |
| 144 |
* @return The expression operand |
| 145 |
* @throws IllegalArgumentException if the expression is not an unary expression |
| 146 |
* @see IExpression#TYPE_ALL |
| 147 |
* @see IExpression#TYPE_EXISTS |
| 148 |
* @see IExpression#TYPE_LAMBDA |
| 149 |
* @see IExpression#TYPE_NOT |
| 150 |
*/ |
| 151 |
public static IExpression getOperand(IExpression expression) { |
| 152 |
if (expression instanceof Unary) |
| 153 |
return ((Unary) expression).operand; |
| 154 |
throw new IllegalArgumentException(); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Obtains the operands of an n-ary expression |
| 159 |
* @param expression The expression to introspect |
| 160 |
* @return The expression operand |
| 161 |
* @throws IllegalArgumentException if the expression is not a n-ary expression |
| 162 |
* @see IExpression#TYPE_AND |
| 163 |
* @see IExpression#TYPE_OR |
| 164 |
*/ |
| 165 |
public static IExpression[] getOperands(IExpression expression) { |
| 166 |
if (expression instanceof NAry) |
| 167 |
return ((NAry) expression).operands; |
| 168 |
throw new IllegalArgumentException(); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Obtains the Right Hand Side (RHS) of a binary expression. |
| 173 |
* @param expression The expression to introspect |
| 174 |
* @return The right hand side operator |
| 175 |
* @throws IllegalArgumentException if the expression is not a binary expression |
| 176 |
* @see IExpression#TYPE_AT |
| 177 |
* @see IExpression#TYPE_EQUALS |
| 178 |
* @see IExpression#TYPE_GREATER |
| 179 |
* @see IExpression#TYPE_GREATER_EQUAL |
| 180 |
* @see IExpression#TYPE_LESS |
| 181 |
* @see IExpression#TYPE_LESS_EQUAL |
| 182 |
* @see IExpression#TYPE_MATCHES |
| 183 |
* @see IExpression#TYPE_NOT_EQUALS |
| 184 |
*/ |
| 185 |
public static IExpression getRHS(IExpression expression) { |
| 186 |
if (expression instanceof Binary) |
| 187 |
return ((Binary) expression).rhs; |
| 188 |
throw new IllegalArgumentException(); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Obtains the value of a literal expression |
| 193 |
* @param expression The expression to introspect |
| 194 |
* @return The literal value |
| 195 |
* @throws IllegalArgumentException if the expression is not a literal |
| 196 |
* @see IExpression#TYPE_LITERAL |
| 197 |
*/ |
| 198 |
public static Object getValue(IExpression expression) { |
| 199 |
if (expression instanceof Literal) |
| 200 |
return ((Literal) expression).value; |
| 201 |
throw new IllegalArgumentException(); |
| 202 |
} |
| 203 |
} |