KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > el > ForTokensTag


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

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 JavaDoc;
38 import java.util.Iterator JavaDoc;
39
40 public class ForTokensTag extends ForEachTag {
41   private static L10N L = new L10N(ForTokensTag.class);
42   
43   private Expr _delimsExpr;
44
45   /**
46    * Sets the delimiter expression.
47    */

48   public void setDelims(Expr delims)
49   {
50     _delimsExpr = delims;
51   }
52
53   /**
54    * Process the tag.
55    */

56   public int doStartTag()
57     throws JspException JavaDoc
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 JavaDoc items = _itemsExpr.evalString(env);
83       String JavaDoc 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 JavaDoc e) {
97       throw new JspException JavaDoc(e);
98     }
99   }
100
101   public class TokenIterator implements Iterator JavaDoc {
102     private String JavaDoc value;
103     private char []delims;
104     private int length;
105     private int i;
106     private CharBuffer cb = new CharBuffer();
107
108     TokenIterator(String JavaDoc value, String JavaDoc 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 JavaDoc 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 JavaDoc();
146     }
147   }
148 }
149
Popular Tags