Skip to main content
summaryrefslogtreecommitdiffstats
blob: f4f7da504ddcdddb6dc278fb96606bd214e8a56d (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
/*******************************************************************************
 * Copyright (c) 2010 Cloudsmith Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Cloudsmith Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.p2.metadata.expression;

import org.eclipse.equinox.internal.p2.metadata.expression.Member.DynamicMember;
import org.eclipse.equinox.p2.metadata.Version;
import org.eclipse.equinox.p2.metadata.expression.IExpressionVisitor;

/**
 * The abstract base class for all binary operations
 */
public abstract class Binary extends Expression {
	public final Expression lhs;

	public final Expression rhs;

	protected Binary(Expression lhs, Expression rhs) {
		this.lhs = lhs;
		this.rhs = rhs;
	}

	public boolean accept(IExpressionVisitor visitor) {
		return super.accept(visitor) && lhs.accept(visitor) && rhs.accept(visitor);
	}

	public int compareTo(Expression e) {
		int cmp = super.compareTo(e);
		if (cmp == 0) {
			Binary be = (Binary) e;
			cmp = lhs.compareTo(be.lhs);
			if (cmp == 0)
				cmp = rhs.compareTo(be.rhs);
		}
		return cmp;
	}

	public boolean equals(Object o) {
		if (super.equals(o)) {
			Binary bo = (Binary) o;
			return lhs.equals(bo.lhs) && rhs.equals(bo.rhs);
		}
		return false;
	}

	public int getPriority() {
		return PRIORITY_BINARY; // Default priority
	}

	public int hashCode() {
		int result = 31 + lhs.hashCode();
		return 31 * result + rhs.hashCode();
	}

	public void toString(StringBuffer bld, Variable rootVariable) {
		appendOperand(bld, rootVariable, lhs, getPriority());
		bld.append(' ');
		bld.append(getOperator());
		bld.append(' ');
		appendOperand(bld, rootVariable, rhs, getPriority());
	}

	/**
	 * Appends the LDAP filter attribute name from the lhs expression if
	 * possible. 
	 * @throws UnsupportedOperationException when this expression does not conform to an
	 * LDAP filter binary expression
	 */
	void appendLDAPAttribute(StringBuffer buf) {
		if (lhs instanceof DynamicMember) {
			DynamicMember attr = (DynamicMember) lhs;
			if (attr.operand instanceof Variable) {
				buf.append(attr.getName());
				return;
			}
		}
		throw new UnsupportedOperationException();
	}

	private static char hexChar(int value) {
		return (char) (value < 10 ? ('0' + value) : ('a' + (value - 10)));
	}

	static void appendLDAPEscaped(StringBuffer bld, String str) {
		appendLDAPEscaped(bld, str, true);
	}

	static void appendLDAPEscaped(StringBuffer bld, String str, boolean escapeWild) {
		int top = str.length();
		for (int idx = 0; idx < top; ++idx) {
			char c = str.charAt(idx);
			if (!escapeWild) {
				if (c == '*') {
					bld.append(c);
					continue;
				} else if (c == '\\' && idx + 1 < top && str.charAt(idx + 1) == '*') {
					bld.append("\\2a"); //$NON-NLS-1$
					++idx;
					continue;
				}
			}
			if (c == '(' || c == ')' || c == '*' || c == '\\' || c < ' ' || c > 127) {
				short cs = (short) c;
				bld.append('\\');
				bld.append(hexChar((cs & 0x00f0) >> 4));
				bld.append(hexChar(cs & 0x000f));
			} else
				bld.append(c);
		}
	}

	/**
	 * Appends the LDAP filter value from the rhs expression if
	 * possible. 
	 * @throws UnsupportedOperationException when this expression does not conform to an
	 * LDAP filter binary expression
	 */
	void appendLDAPValue(StringBuffer buf) {
		if (rhs instanceof Literal) {
			Object value = rhs.evaluate(null);
			if (value instanceof String || value instanceof Version) {
				appendLDAPEscaped(buf, value.toString());
				return;
			}
		}
		throw new UnsupportedOperationException();
	}
}

Back to the top