1 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 { 82 this.predicates = Collections.EMPTY_LIST; 83 } 84 85 public void addPredicate(Predicate predicate) 86 { 87 if ( this.predicates == Collections.EMPTY_LIST ) 88 { 89 this.predicates = new ArrayList (); 90 } 91 92 this.predicates.add( predicate ); 93 } 94 95 public List getPredicates() 96 { 97 return this.predicates; 98 } 99 100 public void simplify() 101 { 102 Iterator predIter = this.predicates.iterator(); 103 Predicate eachPred = null; 104 105 while ( predIter.hasNext() ) 106 { 107 eachPred = (Predicate) predIter.next(); 108 eachPred.simplify(); 109 } 110 } 111 112 public String getText() 113 { 114 StringBuffer buf = new StringBuffer (); 115 116 Iterator predIter = this.predicates.iterator(); 117 Predicate eachPred = null; 118 119 while ( predIter.hasNext() ) 120 { 121 eachPred = (Predicate) predIter.next(); 122 buf.append( eachPred.getText() ); 123 } 124 125 return buf.toString(); 126 } 127 128 protected boolean evaluateAsBoolean(List contextNodeSet, 132 ContextSupport support) throws JaxenException 133 { 134 List result = evaluatePredicates( contextNodeSet, support ); 135 136 return ! result.isEmpty(); 137 } 138 139 protected List evaluatePredicates(List contextNodeSet, ContextSupport support) 140 throws JaxenException { 141 if (predicates.size() == 0) { 143 return contextNodeSet; 144 } 145 Iterator predIter = predicates.iterator(); 146 147 List nodes2Filter = contextNodeSet; 149 while(predIter.hasNext()) { 151 nodes2Filter = 152 applyPredicate((Predicate)predIter.next(), nodes2Filter, support); 153 } 154 155 return nodes2Filter; 156 } 157 158 public List applyPredicate(Predicate predicate, List nodes2Filter, ContextSupport support) 159 throws JaxenException { 160 final int nodes2FilterSize = nodes2Filter.size(); 161 List filteredNodes = new ArrayList (nodes2FilterSize); 162 Context predContext = new Context(support); 164 List tempList = new ArrayList (1); 165 predContext.setNodeSet(tempList); 166 for (int i = 0; i < nodes2FilterSize; ++i) { 169 Object contextNode = nodes2Filter.get(i); 170 tempList.clear(); 171 tempList.add(contextNode); 172 predContext.setNodeSet(tempList); 173 predContext.setPosition(i + 1); 175 predContext.setSize(nodes2FilterSize); 176 Object predResult = predicate.evaluate(predContext); 177 if (predResult instanceof Number ) { 178 int proximity = ((Number ) predResult).intValue(); 181 if (proximity == (i + 1)) { 182 filteredNodes.add(contextNode); 183 } 184 } 185 else { 186 Boolean includes = 187 BooleanFunction.evaluate(predResult, 188 predContext.getNavigator()); 189 if (includes.booleanValue()) { 190 filteredNodes.add(contextNode); 191 } 192 } 193 } 194 return filteredNodes; 195 } 196 } 197 | Popular Tags |