View Javadoc

1   /*
2    * $Header$
3    * $Revision: 1332 $
4    * $Date: 2008-10-18 06:29:28 -0700 (Sat, 18 Oct 2008) $
5    *
6    * ====================================================================
7    *
8    * Copyright 2000-2002 bob mcwhirter & James Strachan.
9    * All rights reserved.
10   *
11   * Redistribution and use in source and binary forms, with or without
12   * modification, are permitted provided that the following conditions are
13   * met:
14   * 
15   *   * Redistributions of source code must retain the above copyright
16   *     notice, this list of conditions and the following disclaimer.
17   * 
18   *   * Redistributions in binary form must reproduce the above copyright
19   *     notice, this list of conditions and the following disclaimer in the
20   *     documentation and/or other materials provided with the distribution.
21   * 
22   *   * Neither the name of the Jaxen Project nor the names of its
23   *     contributors may be used to endorse or promote products derived 
24   *     from this software without specific prior written permission.
25   * 
26   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27   * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28   * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
29   * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
30   * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37   *
38   * ====================================================================
39   * This software consists of voluntary contributions made by many 
40   * individuals on behalf of the Jaxen Project and was originally 
41   * created by bob mcwhirter <bob@werken.com> and 
42   * James Strachan <jstrachan@apache.org>.  For more information on the 
43   * Jaxen Project, please see <http://www.jaxen.org/>.
44   * 
45   * $Id: DefaultEqualityExpr.java 1332 2008-10-18 13:29:28Z elharo $
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       /* If both objects to be compared are node-sets, then the comparison will be 
111        * true if and only if there is a node in the first node-set and a node in 
112        * the second node-set such that the result of performing the comparison on 
113        * the string-values of the two nodes is true */
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   }