Skip to main content
summaryrefslogtreecommitdiffstats
blob: 85bd91e52d2bcb9aa0e80f7d6d9e3bc62c72e8cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*******************************************************************************
 * Copyright (c) 2010, 2018 Cloudsmith Inc. and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Cloudsmith Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.p2.metadata.expression.parser;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import org.eclipse.equinox.internal.p2.metadata.Messages;
import org.eclipse.equinox.internal.p2.metadata.expression.IExpressionConstants;
import org.eclipse.equinox.internal.p2.metadata.expression.LDAPApproximation;
import org.eclipse.equinox.p2.metadata.expression.ExpressionParseException;
import org.eclipse.equinox.p2.metadata.expression.IExpression;
import org.eclipse.equinox.p2.metadata.expression.IExpressionFactory;
import org.eclipse.equinox.p2.metadata.expression.IFilterExpression;
import org.eclipse.equinox.p2.metadata.expression.SimplePattern;
import org.eclipse.osgi.util.NLS;

/**
 * Parser class for OSGi filter strings. This class parses the complete filter string and builds a tree of Filter
 * objects rooted at the parent.
 */
public class LDAPFilterParser {
	@SuppressWarnings("serial")
	private static final Map<String, IFilterExpression> filterCache = Collections.synchronizedMap(new LinkedHashMap<String, IFilterExpression>() {
		@Override
		public boolean removeEldestEntry(Map.Entry<String, IFilterExpression> expr) {
			return size() > 64;
		}
	});

	private final IExpressionFactory factory;

	private final IExpression self;

	private final StringBuffer sb = new StringBuffer();

	private String filterString;

	private int position;

	public LDAPFilterParser(IExpressionFactory factory) {
		this.factory = factory;
		self = factory.variable(IExpressionConstants.VARIABLE_THIS);
		position = 0;
	}

	public IFilterExpression parse(String filterStr) {
		IFilterExpression filter = filterCache.get(filterStr);
		if (filter != null)
			return filter;

		synchronized (this) {
			filterString = filterStr;
			position = 0;
			try {
				IExpression expr = parseFilter();
				if (position != filterString.length())
					throw syntaxException(Messages.filter_trailing_characters);
				filter = factory.filterExpression(expr);
				filterCache.put(filterStr, filter);
				return filter;
			} catch (StringIndexOutOfBoundsException e) {
				throw syntaxException(Messages.filter_premature_end);
			}
		}
	}

	private IExpression parseAnd() {
		skipWhiteSpace();
		char c = filterString.charAt(position);
		if (c != '(')
			throw syntaxException(Messages.filter_missing_leftparen);

		ArrayList<IExpression> operands = new ArrayList<>();
		while (c == '(') {
			IExpression child = parseFilter();
			if (!operands.contains(child))
				operands.add(child);
			c = filterString.charAt(position);
		}
		// int sz = operands.size();
		// return sz == 1 ? operands.get(0) : factory.and(operands.toArray(new IExpression[sz]));
		return factory.normalize(operands, IExpression.TYPE_AND);
	}

	private IExpression parseAttr() {
		skipWhiteSpace();

		int begin = position;
		int end = position;

		char c = filterString.charAt(begin);
		while (!(c == '~' || c == '<' || c == '>' || c == '=' || c == '(' || c == ')')) {
			position++;
			if (!Character.isWhitespace(c))
				end = position;
			c = filterString.charAt(position);
		}
		if (end == begin)
			throw syntaxException(Messages.filter_missing_attr);
		return factory.member(self, filterString.substring(begin, end));
	}

	private IExpression parseFilter() {
		IExpression filter;
		skipWhiteSpace();

		if (filterString.charAt(position) != '(')
			throw syntaxException(Messages.filter_missing_leftparen);

		position++;
		filter = parseFiltercomp();

		skipWhiteSpace();

		if (filterString.charAt(position) != ')')
			throw syntaxException(Messages.filter_missing_rightparen);

		position++;
		skipWhiteSpace();

		return filter;
	}

	private IExpression parseFiltercomp() {
		skipWhiteSpace();

		char c = filterString.charAt(position);

		switch (c) {
			case '&' : {
				position++;
				return parseAnd();
			}
			case '|' : {
				position++;
				return parseOr();
			}
			case '!' : {
				position++;
				return parseNot();
			}
		}
		return parseItem();
	}

	private IExpression parseItem() {
		IExpression attr = parseAttr();

		skipWhiteSpace();
		String value;

		boolean[] hasWild = {false};
		char c = filterString.charAt(position);
		switch (c) {
			case '~' :
			case '>' :
			case '<' :
				if (filterString.charAt(position + 1) != '=')
					throw syntaxException(Messages.filter_invalid_operator);
				position += 2;
				int savePos = position;
				value = parseValue(hasWild);
				if (hasWild[0]) {
					// Unescaped wildcard found. This is not legal for the given operator
					position = savePos;
					throw syntaxException(Messages.filter_invalid_value);
				}
				switch (c) {
					case '>' :
						return factory.greaterEqual(attr, factory.constant(value));
					case '<' :
						return factory.lessEqual(attr, factory.constant(value));
				}
				return factory.matches(attr, factory.constant(new LDAPApproximation(value)));
			case '=' :
				position++;
				value = parseValue(hasWild);
				return hasWild[0] ? factory.matches(attr, factory.constant(SimplePattern.compile(value))) : factory.equals(attr, factory.constant(value));
		}
		throw syntaxException(Messages.filter_invalid_operator);
	}

	private IExpression parseNot() {
		skipWhiteSpace();

		if (filterString.charAt(position) != '(')
			throw syntaxException(Messages.filter_missing_leftparen);
		return factory.not(parseFilter());
	}

	private IExpression parseOr() {
		skipWhiteSpace();
		char c = filterString.charAt(position);
		if (c != '(')
			throw syntaxException(Messages.filter_missing_leftparen);

		ArrayList<IExpression> operands = new ArrayList<>();
		while (c == '(') {
			IExpression child = parseFilter();
			operands.add(child);
			c = filterString.charAt(position);
		}
		// int sz = operands.size();
		// return sz == 1 ? operands.get(0) : factory.or(operands.toArray(new IExpression[sz]));
		return factory.normalize(operands, IExpression.TYPE_OR);
	}

	private static int hexValue(char c) {
		int v;
		if (c <= '9')
			v = c - '0';
		else if (c <= 'F')
			v = (c - 'A') + 10;
		else
			v = (c - 'a') + 10;
		return v;
	}

	private String parseValue(boolean[] hasWildBin) {
		sb.setLength(0);
		int savePos = position;
		boolean hasEscapedWild = false;
		parseloop: while (true) {
			char c = filterString.charAt(position);
			switch (c) {
				case '*' :
					if (hasEscapedWild && !hasWildBin[0]) {
						// We must redo the parse.
						position = savePos;
						hasWildBin[0] = true;
						return parseValue(hasWildBin);
					}
					hasWildBin[0] = true;
					sb.append(c);
					position++;
					break;

				case ')' :
					break parseloop;

				case '(' :
					throw syntaxException(Messages.filter_invalid_value);

				case '\\' :
					c = filterString.charAt(++position);
					if (c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f' && position + 1 < filterString.length()) {
						char nc = filterString.charAt(position + 1);
						if (nc >= '0' && nc <= '9' || nc >= 'A' && nc <= 'F' || nc >= 'a' && nc <= 'f') {
							// Assume proper \xx escape where xx are hex digits
							++position;
							c = (char) (((hexValue(c) << 4) & 0xf0) | (hexValue(nc) & 0x0f));
							if (c == '*' && hasWildBin != null) {
								hasEscapedWild = true;
								if (hasWildBin[0])
									sb.append('\\');
							}
						}
					}
					/* fall through into default */

				default :
					sb.append(c);
					position++;
					break;
			}
		}
		if (sb.length() == 0)
			throw syntaxException(Messages.filter_missing_value);
		return sb.toString();
	}

	private void skipWhiteSpace() {
		for (int top = filterString.length(); position < top; ++position)
			if (!Character.isWhitespace(filterString.charAt(position)))
				break;
	}

	protected ExpressionParseException syntaxException(String message) {
		return new ExpressionParseException(NLS.bind(message, filterString, Integer.toString(position)));
	}
}

Back to the top