Coverage Report - org.jaxen.expr.DefaultEqualityExpr
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultEqualityExpr
93%
52/56
96%
22/23
4.667
 
 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  2548
     super( lhs, rhs );
 64  2548
     }
 65  627
 
 66  627
   public String toString()
 67  
     {
 68  0
     return "[(DefaultEqualityExpr): " + getLHS() + ", " + getRHS() + "]";
 69  
     }
 70  0
   
 71  
   public Object evaluate( Context context ) throws JaxenException
 72  
     {
 73  11824
     Object lhsValue = getLHS().evaluate( context );
 74  11824
     Object rhsValue = getRHS().evaluate( context );
 75  2946
     
 76  14762
     if( lhsValue == null || rhsValue == null ) {
 77  0
       return Boolean.FALSE;
 78  2944
     }
 79  
     
 80  11816
     Navigator nav = context.getNavigator();
 81  
 
 82  11816
     if( bothAreSets(lhsValue, rhsValue) ) {
 83  2996
       return evaluateSetSet( (List) lhsValue, (List) rhsValue, nav );
 84  
     }
 85  14708
     else if (isSet(lhsValue ) && isBoolean(rhsValue)) {
 86  16
         Boolean lhsBoolean = ((List) lhsValue).isEmpty() ? Boolean.FALSE : Boolean.TRUE;
 87  27
         Boolean rhsBoolean = (Boolean) rhsValue;
 88  16
         return Boolean.valueOf(evaluateObjectObject( lhsBoolean, rhsBoolean, nav ) );
 89  2933
     }
 90  11748
     else if (isBoolean(lhsValue ) && isSet(rhsValue)) {
 91  1821
         Boolean lhsBoolean = (Boolean) lhsValue;
 92  0
         Boolean rhsBoolean = ((List) rhsValue).isEmpty() ? Boolean.FALSE : Boolean.TRUE;
 93  1793
         return Boolean.valueOf(evaluateObjectObject( lhsBoolean, rhsBoolean, nav ) );
 94  
     }
 95  11748
     else if (eitherIsSet(lhsValue, rhsValue) ) {
 96  7300
       if (isSet(lhsValue)) {
 97  7216
         return evaluateSetSet( (List) lhsValue, convertToList(rhsValue), nav );                
 98  
       }
 99  
       else {
 100  112
         return evaluateSetSet( convertToList(lhsValue), (List) rhsValue, nav );                                
 101  
       }
 102  1112
     }  
 103  
     else {
 104  4448
       return Boolean.valueOf(evaluateObjectObject( lhsValue, rhsValue, nav ) );
 105  
     }    
 106  
   }
 107  
   
 108  1832
   private Boolean evaluateSetSet( List lhsSet, List rhsSet, Navigator nav )
 109  
     {
 110  890
       /* 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  942
        * the string-values of the two nodes is true */
 114  7352
       if( setIsEmpty( lhsSet ) || setIsEmpty( rhsSet ) ) {
 115  4578
             return Boolean.FALSE;
 116  
       }
 117  1018
     
 118  3792
     for( Iterator lhsIterator = lhsSet.iterator(); lhsIterator.hasNext(); )
 119  1018
       {
 120  4112
       Object lhs = lhsIterator.next();
 121  1018
       
 122  4112
       for( Iterator rhsIterator = rhsSet.iterator(); rhsIterator.hasNext(); )
 123  186
         {
 124  4128
         Object rhs = rhsIterator.next();
 125  832
         
 126  4960
         if( evaluateObjectObject( lhs, rhs, nav ) )
 127  
           {
 128  1524
           return Boolean.TRUE;
 129  
           }
 130  3360
         }
 131  3344
       }
 132  
     
 133  5154
     return Boolean.FALSE;
 134  
     }
 135  40
   
 136  
   private boolean evaluateObjectObject( Object lhs, Object rhs, Navigator nav )
 137  
     {
 138  10682
     if( eitherIsBoolean( lhs, rhs ) )
 139  
       {            
 140  311
       return evaluateObjectObject( BooleanFunction.evaluate( lhs, nav ),
 141  
                                    BooleanFunction.evaluate( rhs, nav ) );
 142  
       }
 143  8416
     else if( eitherIsNumber( lhs, rhs ) )
 144  
       {
 145  552
       return evaluateObjectObject( NumberFunction.evaluate( lhs,
 146  
                                                             nav ),
 147  1955
                                    NumberFunction.evaluate( rhs,
 148  
                                                             nav ) );                                              
 149  
       }
 150  
     else
 151  
       {
 152  7864
       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  
   }