View Javadoc
1   /*
2    * Copyright 2000-2002 bob mcwhirter & James Strachan.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are
7    * met:
8    * 
9    *   * Redistributions of source code must retain the above copyright
10   *     notice, this list of conditions and the following disclaimer.
11   * 
12   *   * Redistributions in binary form must reproduce the above copyright
13   *     notice, this list of conditions and the following disclaimer in the
14   *     documentation and/or other materials provided with the distribution.
15   * 
16   *   * Neither the name of the Jaxen Project nor the names of its
17   *     contributors may be used to endorse or promote products derived 
18   *     from this software without specific prior written permission.
19   * 
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21   * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22   * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23   * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
24   * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31   *
32   * ====================================================================
33   * This software consists of voluntary contributions made by many 
34   * individuals on behalf of the Jaxen Project and was originally 
35   * created by bob mcwhirter <bob@werken.com> and 
36   * James Strachan <jstrachan@apache.org>.  For more information on the 
37   * Jaxen Project, please see <https://github.com/jaxen-xpath/jaxen/>.
38   */
39  
40  package org.jaxen.expr;
41  
42  import java.util.Iterator;
43  import java.util.List;
44  import org.jaxen.Context;
45  import org.jaxen.JaxenException;
46  import org.jaxen.Navigator;
47  import org.jaxen.function.BooleanFunction;
48  import org.jaxen.function.NumberFunction;
49  import org.jaxen.function.StringFunction;
50  
51  abstract class DefaultEqualityExpr extends DefaultTruthExpr implements EqualityExpr 
52    {
53    DefaultEqualityExpr( Expr lhs, Expr rhs )
54      {
55      super( lhs, rhs );
56      }
57  
58    Object evaluateChain( List<Object> values, Context context ) throws JaxenException
59      {
60      Navigator nav = context.getNavigator();
61      Object lhsValue = values.get(values.size() - 1);
62      for (int i = values.size() - 2; i >= 0; i--)
63      {
64        lhsValue = evaluateComparison( lhsValue, values.get(i), nav );
65      }
66      return lhsValue;
67    }
68    
69    private Boolean evaluateSetSet( List lhsSet, List rhsSet, Navigator nav )
70      {
71        /* If both objects to be compared are node-sets, then the comparison will be 
72         * true if and only if there is a node in the first node-set and a node in 
73         * the second node-set such that the result of performing the comparison on 
74         * the string-values of the two nodes is true */
75        if( setIsEmpty( lhsSet ) || setIsEmpty( rhsSet ) ) {
76              return Boolean.FALSE;
77        }
78      
79      for( Iterator lhsIterator = lhsSet.iterator(); lhsIterator.hasNext(); )
80        {
81        Object lhs = lhsIterator.next();
82        
83        for( Iterator rhsIterator = rhsSet.iterator(); rhsIterator.hasNext(); )
84          {
85          Object rhs = rhsIterator.next();
86          
87          if( evaluateObjectObject( lhs, rhs, nav ) )
88            {
89            return Boolean.TRUE;
90            }
91          }
92        }
93      
94      return Boolean.FALSE;
95      }
96    
97    private boolean evaluateObjectObject( Object lhs, Object rhs, Navigator nav )
98      {
99      if( eitherIsBoolean( lhs, rhs ) )
100       {            
101       return evaluateObjectObject( BooleanFunction.evaluate( lhs, nav ),
102                                    BooleanFunction.evaluate( rhs, nav ) );
103       }
104     else if( eitherIsNumber( lhs, rhs ) )
105       {
106       return evaluateObjectObject( NumberFunction.evaluate( lhs,
107                                                             nav ),
108                                    NumberFunction.evaluate( rhs,
109                                                             nav ) );                                              
110       }
111     else
112       {
113       return evaluateObjectObject( StringFunction.evaluate( lhs,
114                                                             nav ),
115                                    StringFunction.evaluate( rhs,
116                                                             nav ) );
117       }
118     }
119   
120   private Boolean evaluateComparison( Object lhsValue, Object rhsValue, Navigator nav )
121     {
122     if( lhsValue == null || rhsValue == null ) {
123       return Boolean.FALSE;
124     }
125     
126     if( bothAreSets(lhsValue, rhsValue) ) {
127       return evaluateSetSet( (List) lhsValue, (List) rhsValue, nav );
128     }
129     else if (isSet(lhsValue ) && isBoolean(rhsValue)) {
130         Boolean lhsBoolean = ((List) lhsValue).isEmpty() ? Boolean.FALSE : Boolean.TRUE;
131         Boolean rhsBoolean = (Boolean) rhsValue;
132         return Boolean.valueOf(evaluateObjectObject( lhsBoolean, rhsBoolean, nav ) );
133     }
134     else if (isBoolean(lhsValue ) && isSet(rhsValue)) {
135         Boolean lhsBoolean = (Boolean) lhsValue;
136         Boolean rhsBoolean = ((List) rhsValue).isEmpty() ? Boolean.FALSE : Boolean.TRUE;
137         return Boolean.valueOf(evaluateObjectObject( lhsBoolean, rhsBoolean, nav ) );
138     }
139     else if (eitherIsSet(lhsValue, rhsValue) ) {
140       if (isSet(lhsValue)) {
141         return evaluateSetSet( (List) lhsValue, convertToList(rhsValue), nav );                
142       }
143       else {
144         return evaluateSetSet( convertToList(lhsValue), (List) rhsValue, nav );                                
145       }
146     }  
147     else {
148       return Boolean.valueOf(evaluateObjectObject( lhsValue, rhsValue, nav ) );
149     }
150   }
151   
152   protected abstract boolean evaluateObjectObject( Object lhs, Object rhs );
153   }