1 28 29 package com.caucho.jstl.el; 30 31 import com.caucho.el.Expr; 32 import com.caucho.jsp.PageContextImpl; 33 import com.caucho.util.CharBuffer; 34 import com.caucho.util.L10N; 35 36 import javax.el.ELContext; 37 import javax.servlet.jsp.JspException ; 38 import java.util.Iterator ; 39 40 public class ForTokensTag extends ForEachTag { 41 private static L10N L = new L10N(ForTokensTag.class); 42 43 private Expr _delimsExpr; 44 45 48 public void setDelims(Expr delims) 49 { 50 _delimsExpr = delims; 51 } 52 53 56 public int doStartTag() 57 throws JspException 58 { 59 try { 60 _iterator = null; 61 _index = 0; 62 _count = 0; 63 64 PageContextImpl pageContext = (PageContextImpl) this.pageContext; 65 ELContext env = pageContext.getELContext(); 66 67 if (_beginExpr != null) 68 _begin = (int) _beginExpr.evalLong(env); 69 else 70 _begin = -1; 71 72 if (_endExpr != null) 73 _end = (int) _endExpr.evalLong(env); 74 else 75 _end = Integer.MAX_VALUE; 76 77 if (_stepExpr != null) 78 _step = (int) _stepExpr.evalLong(env); 79 else 80 _step = 0; 81 82 String items = _itemsExpr.evalString(env); 83 String delims = _delimsExpr.evalString(env); 84 85 _iterator = new TokenIterator(items, delims); 86 87 while (_index < _begin && _iterator.hasNext()) { 88 _index++; 89 _iterator.next(); 90 } 91 92 if (_varStatus != null) 93 pageContext.setAttribute(_varStatus, this); 94 95 return doAfterBody(); 96 } catch (Exception e) { 97 throw new JspException (e); 98 } 99 } 100 101 public class TokenIterator implements Iterator { 102 private String value; 103 private char []delims; 104 private int length; 105 private int i; 106 private CharBuffer cb = new CharBuffer(); 107 108 TokenIterator(String value, String delims) 109 { 110 this.value = value; 111 this.delims = delims.toCharArray(); 112 this.length = value.length(); 113 } 114 115 public boolean hasNext() 116 { 117 return i < length; 118 } 119 120 public Object next() 121 { 122 cb.clear(); 123 124 char ch = 0; 125 int startDelims = delims.length - 1; 126 loop: 127 for (; i < length; i++) { 128 ch = value.charAt(i); 129 130 for (int j = startDelims; j >= 0; j--) { 131 if (delims[j] == ch) 132 break loop; 133 } 134 135 cb.append(ch); 136 } 137 138 i++; 139 140 return cb.toString(); 141 } 142 143 public void remove() 144 { 145 throw new UnsupportedOperationException (); 146 } 147 } 148 } 149 | Popular Tags |