BitwiseExpression.java
3.63 KB
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
/*
* Decompiled with CFR 0_118.
*/
package com.adobe.internal.util;
import com.adobe.internal.util.ArrayListStack;
import com.adobe.internal.util.BitwiseOperandEvaluator;
import com.adobe.internal.util.InvalidOperandException;
import com.adobe.internal.util.MalformedExpressionException;
import java.util.EmptyStackException;
import java.util.StringTokenizer;
public class BitwiseExpression {
private BitwiseOperandEvaluator m_OperandEvaluator;
private ArrayListStack m_OperandStack = new ArrayListStack();
private ArrayListStack m_OperatorStack = new ArrayListStack();
public BitwiseExpression(BitwiseOperandEvaluator operandEval) {
this.m_OperandEvaluator = operandEval;
}
public boolean evaluateExpression(String expression) throws MalformedExpressionException, InvalidOperandException {
StringTokenizer value = new StringTokenizer(expression, "|&()", true);
int paranthesisCount = 0;
int result = 48;
while (value.hasMoreTokens()) {
String token = value.nextToken();
if (" ".equals(token)) continue;
if ("|".equals(token)) {
this.m_OperatorStack.push(Character.valueOf('|'));
continue;
}
if ("&".equals(token)) {
this.m_OperatorStack.push(Character.valueOf('&'));
continue;
}
if ("(".equals(token)) {
++paranthesisCount;
continue;
}
if (")".equals(token)) {
result = this.evaluteExp();
--paranthesisCount;
continue;
}
boolean bRet = this.m_OperandEvaluator.evaluate(token);
if (bRet) {
this.m_OperandStack.push(Character.valueOf('1'));
continue;
}
this.m_OperandStack.push(Character.valueOf('0'));
}
if (paranthesisCount != 0) {
throw new MalformedExpressionException("Invalid expression. Not well formed.");
}
while (this.m_OperatorStack.size() > 0) {
result = this.evaluteExp();
}
if (this.m_OperandStack.size() > 0) {
result = ((Character)this.m_OperandStack.pop()).charValue();
}
return result != 48;
}
private char evaluteExp() throws MalformedExpressionException {
Character operand1 = null;
Character operand2 = null;
Character operator = null;
char result = '0';
try {
operand1 = (Character)this.m_OperandStack.pop();
}
catch (EmptyStackException e) {
throw new MalformedExpressionException("Invalid expression. Not well formed.", e);
}
try {
operator = (Character)this.m_OperatorStack.pop();
try {
operand2 = (Character)this.m_OperandStack.pop();
}
catch (EmptyStackException e) {
throw new MalformedExpressionException("Invalid expression. Not well formed.", e);
}
if (operator.charValue() == '|') {
result = (char)(operand1.charValue() | operand2.charValue());
}
if (operator.charValue() == '&') {
result = (char)(operand1.charValue() & operand2.charValue());
}
}
catch (EmptyStackException e) {
result = ((Character)this.m_OperandStack.pop()).charValue();
}
if (result == '0') {
this.m_OperandStack.push(Character.valueOf('0'));
} else {
this.m_OperandStack.push(Character.valueOf('1'));
}
return result;
}
}