1 28 29 package com.caucho.jstl.rt; 30 31 import com.caucho.util.CharBuffer; 32 import com.caucho.util.L10N; 33 34 import javax.servlet.jsp.JspTagException ; 35 import javax.servlet.jsp.jstl.core.LoopTagSupport; 36 import java.util.Iterator ; 37 38 public class CoreForTokensTag extends LoopTagSupport { 39 private static L10N L = new L10N(CoreForTokensTag.class); 40 41 protected String _items; 42 protected String _delims; 43 protected Iterator _iterator; 44 45 48 public void setItems(String items) 49 { 50 _items = items; 51 } 52 53 56 public void setDelims(String delims) 57 { 58 _delims = delims; 59 } 60 61 64 public void setBegin(int begin) 65 { 66 this.begin = begin; 67 this.beginSpecified = true; 68 } 69 70 73 public void setEnd(int end) 74 { 75 this.end = end; 76 this.endSpecified = true; 77 } 78 79 82 public void setStep(int step) 83 { 84 this.step = step; 85 this.stepSpecified = true; 86 } 87 88 91 public void prepare() 92 throws JspTagException 93 { 94 _iterator = new TokenIterator(_items, _delims); 95 } 96 97 100 public boolean hasNext() 101 { 102 return _iterator.hasNext(); 103 } 104 105 108 public Object next() 109 { 110 return _iterator.next(); 111 } 112 113 public static class TokenIterator implements Iterator { 114 private String _value; 115 private char []_delims; 116 private int _length; 117 private int _i; 118 private CharBuffer _cb = new CharBuffer(); 119 120 TokenIterator(String value, String delims) 121 { 122 _value = value; 123 _delims = delims.toCharArray(); 124 _length = value.length(); 125 } 126 127 public boolean hasNext() 128 { 129 return _i < _length; 130 } 131 132 public Object next() 133 { 134 _cb.clear(); 135 136 char ch = 0; 137 int startDelims = _delims.length - 1; 138 loop: 139 for (; _i < _length; _i++) { 140 ch = _value.charAt(_i); 141 142 for (int j = startDelims; j >= 0; j--) { 143 if (_delims[j] == ch) 144 break loop; 145 } 146 147 _cb.append(ch); 148 } 149 150 _i++; 151 152 return _cb.toString(); 153 } 154 155 public void remove() 156 { 157 throw new UnsupportedOperationException (); 158 } 159 } 160 } 161 | Popular Tags |