Coverage Report - org.jaxen.expr.DefaultEqualityExpr
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultEqualityExpr
88%
37/42
86%
31/36
5.667
 
 1  
 /*
 2  
  * $Header$
 3  
  * $Revision$
 4  
  * $Date$
 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$
 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  1288
     super( lhs, rhs );
 64  1288
     }
 65  
 
 66  
   public String toString()
 67  
     {
 68  0
     return "[(DefaultEqualityExpr): " + getLHS() + ", " + getRHS() + "]";
 69  
     }
 70  
   
 71  
   public Object evaluate( Context context ) throws JaxenException
 72  
     {
 73  5988
     Object lhsValue = getLHS().evaluate( context );
 74  5988
     Object rhsValue = getRHS().evaluate( context );
 75  
     
 76  5984
     if( lhsValue == null || rhsValue == null ) {
 77  0
       return Boolean.FALSE;
 78  
     }
 79  
     
 80  5984
     Navigator nav = context.getNavigator();
 81  
 
 82  5984
     if( bothAreSets(lhsValue, rhsValue) ) {
 83  26
       return evaluateSetSet( (List) lhsValue, (List) rhsValue, nav );
 84  
     }
 85  5958
     else if (isSet(lhsValue ) && isBoolean(rhsValue)) {
 86  8
         Boolean lhsBoolean = ((List) lhsValue).isEmpty() ? Boolean.FALSE : Boolean.TRUE;
 87  8
         Boolean rhsBoolean = (Boolean) rhsValue;
 88  8
         return Boolean.valueOf(evaluateObjectObject( lhsBoolean, rhsBoolean, nav ) );
 89  
     }
 90  5950
     else if (isBoolean(lhsValue ) && isSet(rhsValue)) {
 91  0
         Boolean lhsBoolean = (Boolean) lhsValue;
 92  0
         Boolean rhsBoolean = ((List) rhsValue).isEmpty() ? Boolean.FALSE : Boolean.TRUE;
 93  0
         return Boolean.valueOf(evaluateObjectObject( lhsBoolean, rhsBoolean, nav ) );
 94  
     }
 95  5950
     else if (eitherIsSet(lhsValue, rhsValue) ) {
 96  3650
       if (isSet(lhsValue)) {
 97  3594
         return evaluateSetSet( (List) lhsValue, convertToList(rhsValue), nav );                
 98  
       }
 99  
       else {
 100  56
         return evaluateSetSet( convertToList(lhsValue), (List) rhsValue, nav );                                
 101  
       }
 102  
     }  
 103  
     else {
 104  2300
       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  3676
       if( setIsEmpty( lhsSet ) || setIsEmpty( rhsSet ) ) {
 115  1780
             return Boolean.FALSE;
 116  
       }
 117  
     
 118  1896
     for( Iterator lhsIterator = lhsSet.iterator(); lhsIterator.hasNext(); )
 119  
       {
 120  2056
       Object lhs = lhsIterator.next();
 121  
       
 122  2056
       for( Iterator rhsIterator = rhsSet.iterator(); rhsIterator.hasNext(); )
 123  
         {
 124  2064
         Object rhs = rhsIterator.next();
 125  
         
 126  2064
         if( evaluateObjectObject( lhs, rhs, nav ) )
 127  
           {
 128  384
           return Boolean.TRUE;
 129  
           }
 130  1680
         }
 131  1672
       }
 132  
     
 133  1512
     return Boolean.FALSE;
 134  
     }
 135  
   
 136  
   private boolean evaluateObjectObject( Object lhs, Object rhs, Navigator nav )
 137  
     {
 138  4372
     if( eitherIsBoolean( lhs, rhs ) )
 139  
       {            
 140  180
       return evaluateObjectObject( BooleanFunction.evaluate( lhs, nav ),
 141  90
                                    BooleanFunction.evaluate( rhs, nav ) );
 142  
       }
 143  4282
     else if( eitherIsNumber( lhs, rhs ) )
 144  
       {
 145  572
       return evaluateObjectObject( NumberFunction.evaluate( lhs,
 146  
                                                             nav ),
 147  286
                                    NumberFunction.evaluate( rhs,
 148  
                                                             nav ) );                                              
 149  
       }
 150  
     else
 151  
       {
 152  7992
       return evaluateObjectObject( StringFunction.evaluate( lhs,
 153  
                                                             nav ),
 154  3996
                                    StringFunction.evaluate( rhs,
 155  
                                                             nav ) );
 156  
       }
 157  
     }
 158  
   
 159  
   protected abstract boolean evaluateObjectObject( Object lhs, Object rhs );
 160  
   }