1 28 29 package com.caucho.jstl.el; 30 31 import com.caucho.jsp.PageContextImpl; 32 import com.caucho.util.L10N; 33 import com.caucho.xpath.Env; 34 import com.caucho.xpath.Expr; 35 import com.caucho.xpath.XPath; 36 37 import org.w3c.dom.Node ; 38 import org.w3c.dom.NodeList ; 39 40 import javax.servlet.jsp.JspException ; 41 import javax.servlet.jsp.tagext.IterationTag ; 42 import javax.servlet.jsp.tagext.TagSupport ; 43 import java.util.ArrayList ; 44 import java.util.Collection ; 45 import java.util.Iterator ; 46 47 50 public class XmlForEachTag extends TagSupport implements IterationTag { 51 private static L10N L = new L10N(XmlIfTag.class); 52 53 private Expr _select; 54 private String _var; 55 private String _varStatus; 56 private Node _oldEnv; 57 58 private int _begin; 59 private int _end = Integer.MAX_VALUE / 2; 60 private int _step = 1; 61 62 private Iterator _iterator; 63 64 67 public void setSelect(Expr select) 68 { 69 _select = select; 70 } 71 72 75 public void setVar(String var) 76 { 77 _var = var; 78 } 79 80 83 public void setVarStatus(String varStatus) 84 { 85 _varStatus = varStatus; 86 } 87 88 91 public void setBegin(int begin) 92 { 93 _begin = begin; 94 } 95 96 99 public void setEnd(int end) 100 { 101 _end = end; 102 } 103 104 107 public void setStep(int step) 108 { 109 _step = step; 110 } 111 112 115 public int doStartTag() 116 throws JspException 117 { 118 try { 119 PageContextImpl pageContext = (PageContextImpl) this.pageContext; 120 Env env = XPath.createEnv(); 121 env.setVarEnv(pageContext.getVarEnv()); 122 123 _oldEnv = pageContext.getNodeEnv(); 124 Object obj = _select.evalObject(_oldEnv, env); 125 126 env.free(); 127 128 if (obj == null) 129 return SKIP_BODY; 130 131 if (obj instanceof Iterator ) 132 _iterator = (Iterator ) obj; 133 else if (obj instanceof Collection ) 134 _iterator = ((Collection ) obj).iterator(); 135 else if (obj instanceof NodeList ) { 136 NodeList nodeList = (NodeList ) obj; 137 ArrayList <Object > list = new ArrayList <Object >(); 138 139 for (int i = 0; i < nodeList.getLength(); i++) 140 list.add(nodeList.item(i)); 141 142 _iterator = list.iterator(); 143 } 144 else { 145 ArrayList <Object > list = new ArrayList <Object >(); 146 list.add(obj); 147 148 _iterator = list.iterator(); 149 } 150 151 if (! _iterator.hasNext()) 152 return SKIP_BODY; 153 154 Object value = _iterator.next(); 155 156 if (_var != null) 157 pageContext.setAttribute(_var, value); 158 159 if (value instanceof Node ) 160 pageContext.setNodeEnv((Node ) value); 161 162 return EVAL_BODY_INCLUDE; 163 } catch (Exception e) { 164 throw new JspException (e); 165 } 166 } 167 168 171 public int doAfterBody() 172 throws JspException 173 { 174 PageContextImpl pageContext = (PageContextImpl) this.pageContext; 175 176 if (! _iterator.hasNext()) { 177 pageContext.setNodeEnv(_oldEnv); 178 return SKIP_BODY; 179 } 180 181 Object value = _iterator.next(); 182 183 if (_var != null) 184 pageContext.setAttribute(_var, value); 185 if (value instanceof Node ) 186 pageContext.setNodeEnv((Node ) value); 187 188 return EVAL_BODY_AGAIN; 189 } 190 } 191 | Popular Tags |