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 package org.jaxen.expr;
49
50 import java.util.Iterator;
51 import java.util.List;
52 import org.jaxen.Context;
53 import org.jaxen.JaxenException;
54 import org.jaxen.Navigator;
55 import org.jaxen.function.BooleanFunction;
56 import org.jaxen.function.NumberFunction;
57 import org.jaxen.function.StringFunction;
58
59 abstract class DefaultEqualityExpr extends DefaultTruthExpr implements EqualityExpr
60 {
61 DefaultEqualityExpr( Expr lhs, Expr rhs )
62 {
63 super( lhs, rhs );
64 }
65
66 public String toString()
67 {
68 return "[(DefaultEqualityExpr): " + getLHS() + ", " + getRHS() + "]";
69 }
70
71 public Object evaluate( Context context ) throws JaxenException
72 {
73 Object lhsValue = getLHS().evaluate( context );
74 Object rhsValue = getRHS().evaluate( context );
75
76 if( lhsValue == null || rhsValue == null ) {
77 return Boolean.FALSE;
78 }
79
80 Navigator nav = context.getNavigator();
81
82 if( bothAreSets(lhsValue, rhsValue) ) {
83 return evaluateSetSet( (List) lhsValue, (List) rhsValue, nav );
84 }
85 else if (isSet(lhsValue ) && isBoolean(rhsValue)) {
86 Boolean lhsBoolean = ((List) lhsValue).isEmpty() ? Boolean.FALSE : Boolean.TRUE;
87 Boolean rhsBoolean = (Boolean) rhsValue;
88 return Boolean.valueOf(evaluateObjectObject( lhsBoolean, rhsBoolean, nav ) );
89 }
90 else if (isBoolean(lhsValue ) && isSet(rhsValue)) {
91 Boolean lhsBoolean = (Boolean) lhsValue;
92 Boolean rhsBoolean = ((List) rhsValue).isEmpty() ? Boolean.FALSE : Boolean.TRUE;
93 return Boolean.valueOf(evaluateObjectObject( lhsBoolean, rhsBoolean, nav ) );
94 }
95 else if (eitherIsSet(lhsValue, rhsValue) ) {
96 if (isSet(lhsValue)) {
97 return evaluateSetSet( (List) lhsValue, convertToList(rhsValue), nav );
98 }
99 else {
100 return evaluateSetSet( convertToList(lhsValue), (List) rhsValue, nav );
101 }
102 }
103 else {
104 return Boolean.valueOf(evaluateObjectObject( lhsValue, rhsValue, nav ) );
105 }
106 }
107
108 private Boolean evaluateSetSet( List lhsSet, List rhsSet, Navigator nav )
109 {
110
111
112
113
114 if( setIsEmpty( lhsSet ) || setIsEmpty( rhsSet ) ) {
115 return Boolean.FALSE;
116 }
117
118 for( Iterator lhsIterator = lhsSet.iterator(); lhsIterator.hasNext(); )
119 {
120 Object lhs = lhsIterator.next();
121
122 for( Iterator rhsIterator = rhsSet.iterator(); rhsIterator.hasNext(); )
123 {
124 Object rhs = rhsIterator.next();
125
126 if( evaluateObjectObject( lhs, rhs, nav ) )
127 {
128 return Boolean.TRUE;
129 }
130 }
131 }
132
133 return Boolean.FALSE;
134 }
135
136 private boolean evaluateObjectObject( Object lhs, Object rhs, Navigator nav )
137 {
138 if( eitherIsBoolean( lhs, rhs ) )
139 {
140 return evaluateObjectObject( BooleanFunction.evaluate( lhs, nav ),
141 BooleanFunction.evaluate( rhs, nav ) );
142 }
143 else if( eitherIsNumber( lhs, rhs ) )
144 {
145 return evaluateObjectObject( NumberFunction.evaluate( lhs,
146 nav ),
147 NumberFunction.evaluate( rhs,
148 nav ) );
149 }
150 else
151 {
152 return evaluateObjectObject( StringFunction.evaluate( lhs,
153 nav ),
154 StringFunction.evaluate( rhs,
155 nav ) );
156 }
157 }
158
159 protected abstract boolean evaluateObjectObject( Object lhs, Object rhs );
160 }