Coverage Report - org.jaxen.expr.PredicateSet

Classes in this Package Line Coverage Branch Coverage Complexity
PredicateSet
96% 
90% 
2.25

 1  
 /*
 2  
  * $Header: /home/projects/jaxen/scm/jaxen/src/java/main/org/jaxen/expr/PredicateSet.java,v 1.18 2005/04/15 20:24:04 elharo Exp $
 3  
  * $Revision: 1.18 $
 4  
  * $Date: 2005/04/15 20:24:04 $
 5  
  *
 6  
  * ====================================================================
 7  
  *
 8  
  * Copyright (C) 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
 13  
  * are met:
 14  
  *
 15  
  * 1. Redistributions of source code must retain the above copyright
 16  
  *    notice, this list of conditions, and the following disclaimer.
 17  
  *
 18  
  * 2. Redistributions in binary form must reproduce the above copyright
 19  
  *    notice, this list of conditions, and the disclaimer that follows
 20  
  *    these conditions in the documentation and/or other materials
 21  
  *    provided with the distribution.
 22  
  *
 23  
  * 3. The name "Jaxen" must not be used to endorse or promote products
 24  
  *    derived from this software without prior written permission.  For
 25  
  *    written permission, please contact license@jaxen.org.
 26  
  *
 27  
  * 4. Products derived from this software may not be called "Jaxen", nor
 28  
  *    may "Jaxen" appear in their name, without prior written permission
 29  
  *    from the Jaxen Project Management (pm@jaxen.org).
 30  
  *
 31  
  * In addition, we request (but do not require) that you include in the
 32  
  * end-user documentation provided with the redistribution and/or in the
 33  
  * software itself an acknowledgement equivalent to the following:
 34  
  *     "This product includes software developed by the
 35  
  *      Jaxen Project (http://www.jaxen.org/)."
 36  
  * Alternatively, the acknowledgment may be graphical using the logos
 37  
  * available at http://www.jaxen.org/
 38  
  *
 39  
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 40  
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 41  
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 42  
  * DISCLAIMED.  IN NO EVENT SHALL THE Jaxen AUTHORS OR THE PROJECT
 43  
  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 44  
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 45  
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 46  
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 47  
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 48  
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 49  
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 50  
  * SUCH DAMAGE.
 51  
  *
 52  
  * ====================================================================
 53  
  * This software consists of voluntary contributions made by many
 54  
  * individuals on behalf of the Jaxen Project and was originally
 55  
  * created by bob mcwhirter <bob@werken.com> and
 56  
  * James Strachan <jstrachan@apache.org>.  For more information on the
 57  
  * Jaxen Project, please see <http://www.jaxen.org/>.
 58  
  *
 59  
  * $Id: PredicateSet.java,v 1.18 2005/04/15 20:24:04 elharo Exp $
 60  
  */
 61  
 
 62  
 
 63  
 
 64  
 package org.jaxen.expr;
 65  
 
 66  
 import java.io.Serializable;
 67  
 import java.util.ArrayList;
 68  
 import java.util.Collections;
 69  
 import java.util.Iterator;
 70  
 import java.util.List;
 71  
 import org.jaxen.Context;
 72  
 import org.jaxen.ContextSupport;
 73  
 import org.jaxen.JaxenException;
 74  
 import org.jaxen.function.BooleanFunction;
 75  
 
 76  
 public class PredicateSet implements Serializable
 77  
 {
 78  
     private List predicates;
 79  
 
 80  
     public PredicateSet()
 81  4565
     {
 82  4565
         this.predicates = Collections.EMPTY_LIST;
 83  4565
     }
 84  
 
 85  
     public void addPredicate(Predicate predicate)
 86  
     {
 87  387
         if ( this.predicates == Collections.EMPTY_LIST )
 88  
         {
 89  374
             this.predicates = new ArrayList();
 90  
         }
 91  
 
 92  387
         this.predicates.add( predicate );
 93  387
     }
 94  
 
 95  
     public List getPredicates()
 96  
     {
 97  1965
         return this.predicates;
 98  
     }
 99  
 
 100  
     public void simplify()
 101  
     {
 102  4498
         Iterator  predIter = this.predicates.iterator();
 103  4498
         Predicate eachPred = null;
 104  
 
 105  4876
         while ( predIter.hasNext() )
 106  
         {
 107  378
             eachPred = (Predicate) predIter.next();
 108  378
             eachPred.simplify();
 109  
         }
 110  4498
     }
 111  
 
 112  
     public String getText()
 113  
     {
 114  19
         StringBuffer buf = new StringBuffer();
 115  
 
 116  19
         Iterator  predIter = this.predicates.iterator();
 117  19
         Predicate eachPred = null;
 118  
 
 119  23
         while ( predIter.hasNext() )
 120  
         {
 121  4
             eachPred = (Predicate) predIter.next();
 122  4
             buf.append( eachPred.getText() );
 123  
         }
 124  
 
 125  19
         return buf.toString();
 126  
     }
 127  
 
 128  
     // FIXME: Note - this could be *MUCH* more efficient
 129  
     // currently this creates many redundant collections and should halt
 130  
     // evaluation on the first matching item.
 131  
     protected boolean evaluateAsBoolean(List contextNodeSet,
 132  
                                       ContextSupport support) throws JaxenException
 133  
     {
 134  0
         List result = evaluatePredicates( contextNodeSet, support );
 135  
 
 136  0
         return ! result.isEmpty();
 137  
     }
 138  
 
 139  
    protected List evaluatePredicates(List contextNodeSet, ContextSupport support)
 140  
             throws JaxenException {
 141  
         // Easy way out (necessary)
 142  9010
         if (predicates.size() == 0) {
 143  6318
             return contextNodeSet;
 144  
         }
 145  2692
         Iterator predIter = predicates.iterator();
 146  
 
 147  
         // initial list to filter
 148  2692
         List nodes2Filter = contextNodeSet;
 149  
         // apply all predicates
 150  5395
         while(predIter.hasNext()) {
 151  2705
             nodes2Filter =
 152  
                 applyPredicate((Predicate)predIter.next(), nodes2Filter, support);
 153  
         }
 154  
         
 155  2690
         return nodes2Filter;
 156  
     }
 157  
    
 158  
     public List applyPredicate(Predicate predicate, List nodes2Filter, ContextSupport support)
 159  
             throws JaxenException {
 160  2705
         final int nodes2FilterSize = nodes2Filter.size();
 161  2705
         List filteredNodes = new ArrayList(nodes2FilterSize);
 162  
         // Set up a dummy context with a list to hold each node
 163  2705
         Context predContext = new Context(support);
 164  2705
         List tempList = new ArrayList(1);
 165  2705
         predContext.setNodeSet(tempList);
 166  
         // loop through the current nodes to filter and add to the
 167  
         // filtered nodes list if the predicate succeeds
 168  6623
         for (int i = 0; i < nodes2FilterSize; ++i) {
 169  3920
             Object contextNode = nodes2Filter.get(i);
 170  3920
             tempList.clear();
 171  3920
             tempList.add(contextNode);
 172  3920
             predContext.setNodeSet(tempList);
 173  
             // ????
 174  3920
             predContext.setPosition(i + 1);
 175  3920
             predContext.setSize(nodes2FilterSize);
 176  3920
             Object predResult = predicate.evaluate(predContext);
 177  3918
             if (predResult instanceof Number) {
 178  
                 // Here we assume nodes are in forward or reverse order
 179  
                 // as appropriate for axis
 180  745
                 int proximity = ((Number) predResult).intValue();
 181  745
                 if (proximity == (i + 1)) {
 182  156
                     filteredNodes.add(contextNode);
 183  
                 }
 184  
             }
 185  
             else {
 186  3173
                 Boolean includes =
 187  
                     BooleanFunction.evaluate(predResult,
 188  
                                             predContext.getNavigator());
 189  3173
                 if (includes.booleanValue()) {
 190  485
                     filteredNodes.add(contextNode);
 191  
                 }
 192  
             }
 193  
         }
 194  2703
         return filteredNodes;
 195  
     }
 196  
 }