KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > rt > CoreForTokensTag


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.rt;
30
31 import com.caucho.util.CharBuffer;
32 import com.caucho.util.L10N;
33
34 import javax.servlet.jsp.JspTagException JavaDoc;
35 import javax.servlet.jsp.jstl.core.LoopTagSupport;
36 import java.util.Iterator JavaDoc;
37
38 public class CoreForTokensTag extends LoopTagSupport {
39   private static L10N L = new L10N(CoreForTokensTag.class);
40
41   protected String JavaDoc _items;
42   protected String JavaDoc _delims;
43   protected Iterator JavaDoc _iterator;
44
45   /**
46    * Sets the collection expression.
47    */

48   public void setItems(String JavaDoc items)
49   {
50     _items = items;
51   }
52
53   /**
54    * Sets the delimiters expression.
55    */

56   public void setDelims(String JavaDoc delims)
57   {
58     _delims = delims;
59   }
60
61   /**
62    * Sets the beginning value
63    */

64   public void setBegin(int begin)
65   {
66     this.begin = begin;
67     this.beginSpecified = true;
68   }
69
70   /**
71    * Sets the ending value
72    */

73   public void setEnd(int end)
74   {
75     this.end = end;
76     this.endSpecified = true;
77   }
78
79   /**
80    * Sets the step value
81    */

82   public void setStep(int step)
83   {
84     this.step = step;
85     this.stepSpecified = true;
86   }
87
88   /**
89    * Prepares the iterator.
90    */

91   public void prepare()
92     throws JspTagException JavaDoc
93   {
94     _iterator = new TokenIterator(_items, _delims);
95   }
96
97   /**
98    * Returns true if there are more items.
99    */

100   public boolean hasNext()
101   {
102     return _iterator.hasNext();
103   }
104
105   /**
106    * Returns the next item
107    */

108   public Object JavaDoc next()
109   {
110     return _iterator.next();
111   }
112
113   public static class TokenIterator implements Iterator JavaDoc {
114     private String JavaDoc _value;
115     private char []_delims;
116     private int _length;
117     private int _i;
118     private CharBuffer _cb = new CharBuffer();
119
120     TokenIterator(String JavaDoc value, String JavaDoc 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 JavaDoc 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 JavaDoc();
158     }
159   }
160 }
161
Popular Tags