1 16 17 package org.apache.taglibs.xtags.xpath; 18 19 import java.io.IOException ; 20 import java.util.Collections ; 21 import java.util.Comparator ; 22 import java.util.HashMap ; 23 import java.util.HashSet ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 import javax.servlet.jsp.JspException ; 29 30 import org.dom4j.DocumentFactory; 31 import org.dom4j.Node; 32 import org.dom4j.XPath; 33 34 import org.apache.taglibs.xtags.util.JspVariableContext; 35 36 37 41 public class ForEachTag extends AbstractBodyTag implements ContextNodeTag { 42 43 44 private XPath xpath; 45 46 47 private String id; 48 49 50 private String type; 51 52 53 private Iterator iterator; 54 55 56 private Object contextNode; 57 58 59 private XPath sortXPath; 60 61 private boolean distinct; 62 63 64 private Object context; 65 66 67 private Object originalContext; 68 69 70 private boolean ascending = true; 71 72 public ForEachTag() { 73 } 74 75 77 public void breakLoop() throws JspException { 78 finishLoop(); 79 } 80 81 public Object getContext() { 84 return contextNode; 85 } 86 87 public int doStartTag() throws JspException { 90 originalContext = TagHelper.getInputNodes( pageContext ); 91 iterator = null; 92 if ( xpath != null ) { 93 List list = selectNodes(); 94 iterator = list.iterator(); 95 if ( hasNext() ) { 96 return EVAL_BODY_TAG; 97 } 98 } 99 reset(); 100 return SKIP_BODY; 101 } 102 103 public int doAfterBody() throws JspException { 104 if ( iterator != null ) { 105 if ( hasNext() ) { 106 return EVAL_BODY_TAG; 107 } 108 else { 109 finishLoop(); 110 } 111 } 112 TagHelper.setInputNodes( pageContext, originalContext ); 115 reset(); 116 return SKIP_BODY; 117 } 118 119 120 public void release() { 121 reset(); 122 xpath = null; 123 sortXPath = null; 124 distinct = false; 125 ascending = true; 126 } 127 128 129 public void setContext(Object context) { 132 this.context = context; 133 } 134 135 137 public void setSelect(String select) { 138 this.xpath = createXPath( select ); 139 } 140 141 143 public void setSelectXPath(XPath xpath) { 144 this.xpath = xpath; 145 } 146 147 150 public void setSort(String sort) { 151 if ( sort == null ) { 152 this.sortXPath = null; 153 } 154 else { 155 this.sortXPath = createXPath( sort ); 156 } 157 } 158 159 public void setSortXPath(XPath sortXPath) { 160 this.sortXPath = sortXPath; 161 } 162 163 166 public boolean isDistinct() { 167 return distinct; 168 } 169 172 public void setDistinct(boolean distinct) { 173 this.distinct = distinct; 174 } 175 176 179 public String getId() { 180 return id; 181 } 182 183 186 public void setId(String id) { 187 this.id = id; 188 } 189 190 193 public String getType() { 194 return type; 195 } 196 197 200 public void setType(String type) { 201 this.type = type; 202 } 203 204 205 208 public boolean isAscending() { 209 return ascending; 210 } 211 212 215 public void setAscending(boolean ascending) { 216 this.ascending = ascending; 217 } 218 219 protected List selectNodes() { 222 if ( xpath != null ) { 223 Object input = getInputNodes(); 224 if ( sortXPath != null ) { 225 List answer = xpath.selectNodes( input, sortXPath, distinct ); 226 if ( ! ascending ) { 227 Collections.reverse( answer ); 228 } 229 return answer; 230 } 231 else { 232 return xpath.selectNodes( input ); 233 } 234 } 235 return Collections.EMPTY_LIST; 236 } 237 238 240 public Object getInputNodes() { 241 if ( context == null ) { 242 return TagHelper.getInputNodes( pageContext, this, true ); 243 } 244 return context; 245 } 246 247 248 249 protected void finishLoop() throws JspException { 250 iterator = null; 251 contextNode = null; 252 try { 253 bodyContent.writeOut(bodyContent.getEnclosingWriter()); 254 } 255 catch (IOException e) { 256 handleException(e); 257 } 258 bodyContent.clearBody(); 259 } 260 261 262 protected boolean hasNext() { 263 if ( iterator.hasNext() ) { 264 contextNode = iterator.next(); 265 TagHelper.defineVariable( pageContext, getId(), contextNode ); 266 if ( contextNode != null ) { 267 return true; 268 } 269 } 270 return false; 271 } 272 273 274 protected XPath createXPath(String xpathExpression) { 275 XPath xpath = getDocumentFactory().createXPath( xpathExpression ); 276 xpath.setVariableContext( JspVariableContext.getInstance( pageContext ) ); 277 return xpath; 278 } 279 280 281 protected DocumentFactory getDocumentFactory() { 282 return DocumentFactory.getInstance(); 283 } 284 285 private void reset() { 286 iterator = null; 287 contextNode = null; 288 } 289 290 } 291 | Popular Tags |