1 61 package org.jaxen.expr; 62 63 import java.util.ArrayList ; 64 import java.util.Collections ; 65 import java.util.Iterator ; 66 import java.util.LinkedList ; 67 import java.util.List ; 68 69 import org.jaxen.Context; 70 import org.jaxen.ContextSupport; 71 import org.jaxen.JaxenException; 72 73 abstract class DefaultLocationPath extends DefaultExpr implements LocationPath 74 { 75 private List steps; 76 77 80 DefaultLocationPath() 81 { 82 this.steps = new LinkedList (); 83 } 84 85 public void addStep(Step step) 86 { 87 getSteps().add(step); 88 } 89 90 public List getSteps() 91 { 92 return this.steps; 93 } 94 95 public Expr simplify() 96 { 97 Iterator stepIter = getSteps().iterator(); 98 Step eachStep = null; 99 while (stepIter.hasNext()) 100 { 101 eachStep = (Step) stepIter.next(); 102 eachStep.simplify(); 103 } 104 return this; 105 } 106 107 public String getText() 108 { 109 StringBuffer buf = new StringBuffer (); 110 Iterator stepIter = getSteps().iterator(); 111 while (stepIter.hasNext()) 112 { 113 buf.append(((Step) stepIter.next()).getText()); 114 if (stepIter.hasNext()) 115 { 116 buf.append("/"); 117 } 118 } 119 return buf.toString(); 120 } 121 122 public String toString() 123 { 124 StringBuffer buf = new StringBuffer (); 125 Iterator stepIter = getSteps().iterator(); 126 while (stepIter.hasNext()) 127 { 128 buf.append(stepIter.next().toString()); 129 if (stepIter.hasNext()) 130 { 131 buf.append("/"); 132 } 133 } 134 return buf.toString(); 135 } 136 137 public boolean isAbsolute() 138 { 139 return false; 140 } 141 142 public Object evaluate(Context context) throws JaxenException 143 { 144 List nodeSet = context.getNodeSet(); 145 List contextNodeSet = new ArrayList (nodeSet.size()); 146 contextNodeSet.addAll(nodeSet); 147 ContextSupport support = context.getContextSupport(); 148 Context stepContext = new Context(support); 149 Iterator stepIter = getSteps().iterator(); 150 while ( stepIter.hasNext() ) 151 { 152 Step eachStep = (Step) stepIter.next(); 153 stepContext.setNodeSet(contextNodeSet); 154 contextNodeSet = eachStep.evaluate(stepContext); 155 if (isReverseAxis(eachStep)) { 157 Collections.reverse(contextNodeSet); 158 } 159 } 160 161 if (getSteps().size() > 1) { 162 Collections.sort(contextNodeSet, new NodeComparator(support.getNavigator())); 163 } 164 165 return contextNodeSet; 166 } 167 168 private boolean isReverseAxis(Step step) { 169 170 int axis = step.getAxis(); 171 return axis == org.jaxen.saxpath.Axis.PRECEDING 172 || axis == org.jaxen.saxpath.Axis.PRECEDING_SIBLING 173 || axis == org.jaxen.saxpath.Axis.ANCESTOR 174 || axis == org.jaxen.saxpath.Axis.ANCESTOR_OR_SELF; 175 } 176 177 } 178 179 | Popular Tags |